Print to PDF (Not save as PDF) in Excel - excel

I was wondering if there is a way to print to pdf. My company has a software installed as a printer (ScanSoft pdf). The reason I need to print as a pdf and not save is as follows:
My default printer can only print letter documents. I have few routines that format the excel document that I am working on to a tabloid document. And the since I my default printer can't support 11x17 (Tabloid). The page breaks are defaulted to letter document settings. And the tabloid is not an option in the size options. If I go and select the print and select a pdf and exit the print dialogue without saving - the page breaks are converted to the tabloid settings. I have another routine that runs through the document and adjusts the page breaks to avoid splitting merged cells.
What I am looking for at end of the day, run the routine to format document, run a routine to print as pdf then exit dialogue (to adjust page breaks), run routine to adjust page breaks, run routine to save pdf.
enter link description here

You just need to set your activeprinter to the pdf printer
Application.ActivePrinter ="what ever your printer is called"
To find out what it's called, set it as the activeprinter manually and then in the immediate window just type
? Application.ActivePrinter

Related

Print PDF to specific folder with specific name with Excel VBA

I have a folder C:\Users\User1\FolderForPDF, and I have Excel workbooks and a Pdf printer with the name of "PdfPrinter1".
After I edit workbook as I need to, I would like to have a macro, that would print first worksheet in my workbook to PDF. I need it to have a name that is in Cell A1 on this first worksheet, and add "_archive" to that (so eg. "worksheet12_archive"). The PDF should be printed to the folder I listed above. The resolution needs to be set to 600. I want it to go through my pdf printer (not through saving as pdf or printing through Microsoft to PDF).
I would like everything to be set in that "Save as" popup window (suggested name, saving folder).
I know about the .ExportAsFixedFormat and this does the job for me for one other specific thing, but for this one I need it to actually go through printing, because this "save as pdf" makes the resolution way bellow my requirement.
Is that possible to acchive or am I looking at something to much for vba?

Export from Excel to PDF change in margins

Whenever exporting from Excel to PDF (via Microsoft print to pdf) the resulting pdf file would have different margins from the original shown in Excel print preview. The margins are wider and the size of the page is changed from A4 (as in the Excel file) to Letter. Is there a way to fix this please? thanks
Instead of 'Microsoft Print to PDF' I would recommend using 'Save As', then in the dropdown 'Save as type:' you can chose 'PDF'. This then saves a PDF that (for me) matches the presentation I expect.
Alternatively, if you do want to use 'Microsoft Print to PDF' you could try to go to printer properties (for 'Microsoft Print to PDF'), then select 'Advanced', then check that the 'paper size' dropdown is set to A4.

Can we extract coordinates of current selected text in pdf using VBA excel

Hi i am able to open a pdf document and select a specified text from the pdf document, but i want to know location of the selected text in the pdf document like page number and line number of that text.
Is there a function in vba excel which can help me get the location of the selected text in any form be it x,y coordinates or page and line number.
I think, you should use send keys option of vba for this. So whenever your file open then it's, also open pdf file. And once pdf file open then it will open find option by send keys code and serach, find, copy and paste according to your sendkeys code requirement.

Format codes for first page header/footer are different

I want to set a different header and footer with VBA for the first page of an Excel 2013 sheet that should be printed. The header and footer should contain information such as the page number, the name of the active sheet and the file name. The official Microsoft documentation about format codes (https://learn.microsoft.com/en-us/office/vba/excel/concepts/workbooks-and-worksheets/formatting-and-vba-codes-for-headers-and-footers) provides various options for this, and all of those format codes work correctly on every page except for the first one - they appear to be assigned differently (e.g. "&N" should display the number of pages, but displays the file name instead) . How can I use the correct format codes for the first page header/footer?
I tried to assign those format codes to constants, but that doesn't work because the codes are only parsed when they are included in a header or footer string. I also recorded a macro while changing the headers manually (which works correctly), but that macro doesn't produce the correct output either.
Sub formatCodesTest()
With ActiveSheet.PageSetup
.DifferentFirstPageHeaderFooter = True
.FirstPage.LeftHeader.Text = "&N"
End With
ActiveSheet.PrintPreview
End Sub
I expected the output to be the number of pages (as it is described in the Microsoft docs), but the actual output is the file name.
I have now managed to work around this problem. Instead of relying on the Microsoft documentation, enter the header/footer manually in the GUI and then use the Immediate Window (Ctrl+G) for debugging and query the contents of the relevant header (e.g. ? ActiveSheet.PageSetup.FirstPage.RightHeader.Text). This will then give you the correct format codes.
If anybody else has an idea why this happens (and why others cannot reproduce it), I'll gladly accept this as an answer.
I have the same problem in Excel 2019: I wanted to show "Page 1 of 5" in the right footer.
As it concerns only the special settings for the first page, I solved it this way:
ActiveSheet.PageSetup.FirstPage.RightFooter.Text = "Page 1 of " + Trim(Str(ActiveSheet.PageSetup.Pages.Count))
Notice that this only works if filling cells, changing page layout, adding manual page breaks done by your VBA code is completed because before Excel can't now the correct amount of pages.

Creating a new pdf document using AcroEXch in VBScript

I am looking to automate the conversion of an excel sheet into a pdf document (I do not want to manually print the report generated in excel as a pdf document every morning). For now, I would like to create a button in excel that will run the macro to automatically generate the pdf document, but this button will eventually not be used.
Im also new to VB, but have read up on the AcroEXch SDK. Seems like I should be using AcroEXch.PDDoc.Create, but this is not quite right (because I cannot specify an input file to be printed/created as a new pdf document).
Any ideas on how I can create a brand new pdf file? Thanks in advance.
I think i found the answer. Here is one solution someone at work suggested (if anyone finds it useful, then great).
There is no available method in the AcroEXch class (or set of methods that I know of) to convert a non-pdf file to a pdf file. Instead, you have to use the pdf Distiller to first convert the file to postscript and then you can write to pdf, using the PDFDistiller class. Here's a snippet of the code:
'1. open excel being converted to pdf:
xlReport.activate
xlReport.range("a1").select
dim PdfFilePath
PdfFilePath = ""
dim PsFilePath
PsFilePath = ""
'2. Print Excel file to postscript file
xlBook.activesheet.PrintOut , , 1, , "Adobe PDF on Ne01:" ,TRUE, , PsFilePath
Dim oDistiller
Set oDistiller = CreateObject("PDFDistiller.PDFDistiller.1")
oDistiller.FileToPDF sPsFilePath, sPdfFilePath, ""
' Close Excel - do not save.
'COMMENTED OUT BELOW 3 LINES FOR DEBUG
xlApp.displayalerts=false
xlApp.quit
set xlApp=nothing
I don't know exactly what your circumstance is and what tools you have access to, but reading your description, it sounds like you simply want to have an Excel file converted for you with a click.
It would be helpful if you had posted whether you have Adobo Acrobat Professional, latest version of Excel, or other converters that are available on the market.
If you have Acrobat Pro installed, your office apps (word, excel, outlook, etc) should already have a "Convert to PDF" button in the toolbars, in combination with Excel command line argument it shouldn't be too hard to cook up a Windows Scheduled Task that periodically convert excel files for you.
Have you considered CuteFTP or PDFCreator, both are free. I have sucessfully used PDFCreator with VBA and I have heard that CuteFTP is good.

Resources