Creating multiple PDF combined into one file from excel - excel

I am currently using the below to create individual pdf files (each time a new value is put into C4, a new pdf with different data is generated). Is there a way that all of these pdf can be saved into one file?
Thank you in advance for your help
Q
Sub moveselection()
Dim i As Integer
For i = 1 To 108
Range("C4").Value = Sheets("Sheet1").Range("A4").Offset(i, 0).Value
ThisFile = Range("C4").Value
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:="T:\Maths\QM" & _
ActiveSheet.Range("C4").Value & ".pdf", _
From:=1, _
To:=1, _
OpenAfterPublish:=False
Next i
End Sub

Rather than saving as pdf each time why not Create a new workbook before you enter the loop and then copy the active sheet to this new workbook then after the loop ends you can export the entire workbook to pdf?

Related

SaveAs PDF and open in Adobe Reader as new Tab

I´m experiencing the following problem. To save an Excel Sheet as PDF, I´m using the following code.
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
This works perfectly fine the first time I´m executing the command. The File pops up as PDF format and is displayed in Adobe Acrobat Reader DC. But when I execute it again, without closing the opened PDF file, I´m getting an error. As long as I always close the old PDF, there is no error. I´m pretty sure it is possible to open the next files in new tabs within Acrobat Reader without getting these errors in VBA. Can someone help me out here?
A file having the same name, cannot be open in the same session of most of applications...
Please, use the next way, which gives different names and opens the same sheet in three consecutive tabs:
Sub expSheetAsPDFAndOpen()
Dim strPDF As String, i As Long
strPDF = ThisWorkbook.path & "\MyPDF"
For i = 1 To 3
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
FileName:=strPDF & i & ".pdf", _
OpenAfterPublish:=True
Next i
End Sub
I suppose that you do not try exporting the same sheet. In such a case you may use the sheet names (being unique) to name the exported pdf.

VBA to print the same area from multiple worksheets as separate PDF files

Going through the stack overflow and other sources on the internet, I found a couple of VBA codes to print multiple active worksheets to separate PDF files.
I would like to use the same printing area in each sheet and save the PDFs as separate files. While trying to (re)create the code, I have now reached the following state and am stuck. More specifically, it seems that the last four lines before "Next" are erroneous (makred in red in Excel VBA Console and causing syntax error), but I am unable to pinpoint what that is so. Can someone here help me out. Thanks in advance.
Sub SetPrintAreas2()
Dim sPrintArea As String
Dim wks As Worksheet
sPrintArea = "C8:E25"
For Each wks In ActiveWindow.SelectedSheets
wks.PageSetup.PrintArea = sPrintArea
wks.PageSetup.Orientation = xlLandscape
wks.PageSetup.PaperSize = xlPaperA4
wks.PageSetup.CenterHorizontally = True
wks.PageSetup.CenterVertically = True
wks.PageSetup.FitToPagesWide = 1
wks.PageSetup.FitToPagesTall = 1
wks.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=Application.ActiveWorkbook.Path & “\” & wks.Name, _
Quality:=xlQualityStandard, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Next
Set wks = Nothing
End Sub
Thanks to all of you helped make a completely unworkable code full functional. This is how the final working code looks:
Sub SetPrintSameAreasOfActiveSheetsAsPDFs()
Dim sPrintArea As String
Dim wks As Worksheet
sPrintArea = "C8:E25"
For Each wks In ActiveWindow.SelectedSheets
wks.PageSetup.PrintArea = sPrintArea
wks.PageSetup.Orientation = xlLandscape
wks.PageSetup.PaperSize = xlPaperA4
wks.PageSetup.CenterHorizontally = True
wks.PageSetup.CenterVertically = True
wks.PageSetup.FitToPagesWide = 1
wks.PageSetup.FitToPagesTall = 1
wks.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=Application.ActiveWorkbook.Path & Application.PathSeparator & wks.Name, _
Quality:=xlQualityStandard, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Next
Set wks = Nothing
End Sub
It prints the Cells between C8:E25 for all selected worksheets in an Excel workbook as PDFs to the same directory. The PDFs take up the name of the worksheet. To use it, after inserting it as a VBA module in the excel sheet, go to the developer tab, Click on Macros, select SetPrintSameAreasOfActiveSheetsAsPDFs() and press "Run".
Change this text to below. your “\” is wrong character.
“\”
to
"\"
or
Application.PathSeparator

Excel VBA. ExportasFixedFormat not including images and print areas are off

From my main workbook, I am opening a second workbook and trying to export that file to a pdf.
I am having troubles with defining the print areas, and it will not export the graphics.
Here is my code.
Workbooks.Open Filename:=sFileDirectory & sFileName
' Now put in the signature
ThisWorkbook.Sheets("Sheet4").Range("Signature").Copy Destination:=ActiveWorkbook.Sheets("Sheet1").Range("SignatureModel")
'Now prepare the PDF setup
'ActiveSheet.PageSetup.PrintArea = ActiveSheet.UsedRange
ActiveSheet.PageSetup.Orientation = xlPortrait
ActiveSheet.PageSetup.Zoom = False
ActiveSheet.PageSetup.FitToPagesWide = 1
ActiveSheet.PageSetup.FitToPagesTall = 1
'Range(ActiveSheet.PageSetup.PrintArea).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=sFileDirectory & PDFName, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=True, OpenAfterPublish:=False
ActiveWorkbook.Close
I have tried setting the print area with pagesetup commands and/or setting the print area in the second file before opening it. Not clear on how this is supposed to work. Also, it will not export the image of my signature.
Any help would be great.
Thanks,

Using Selection.ExportAsFixedFormat to generate PDF properly

I want to export to pdf all the sheets on the workbook except the first one. To do that I used Selection.ExportAsFixedFormat instead of ActiveWorkbook.ExportAsFixedFormat.
The problem using Selection.ExportAsFixedFormat, is that for each sheet, the only part of it that will appear ond the correspondent pdf page is a manual selection instead of all the printing area as it should be (if I select only one cell it will be the only one that appears on the pdf). Using ActiveWorkbook.ExportAsFixedFormat the pdf is generated as intended.
Sub PDF()
Dim SaveAsStr As String
Dim strName As String
Dim i As Long
ReDim ArraySh(2 To Sheets.Count)
For i = 2 To Sheets.Count
ArraySh(i) = Sheets(i).Name
Next
...
'ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, fileName:=SaveAsStr & ".pdf", OpenAfterPublish:=True, IgnorePrintAreas:=False
Sheets(ArraySh).Select
Selection.ExportAsFixedFormat Type:=xlTypePDF, fileName:=SaveAsStr & ".pdf", OpenAfterPublish:=True, IgnorePrintAreas:=False
End Sub
How can I use the Selection method, to generate the PDF properly?

VB macro in excel to save an active doc to PDF with name from a Cell

I'm new to the forum and I'm happy to be part of it. I've been having this specific problem for about three weeks now.
What I'm trying to do is to create a macro that automates a function for me. I have to deal with tons of contracts all the time and imputing information (like names, adresses and ID numbers) on each .doc than saving that doc as PDF is really time consuming. So I trying creating an excel table where I can imput that information which automaticaly saves to the .doc, this copy + paste is done with the special paste function (if I change something in the excel it changes the .doc so I have the whole contract typed out with some blank spaces that get filled in as I type in excel).
After filling up the cells in excel with the appropriate information I need to "save as" that doc as a PDF with the info from one of the cells (in this case the specific cell is where the company name's typed out), then I need o copy that excel table into a new tab that will have the name of the company (the same cell where the .doc was save as).
This is what I have so far:
Sub Autocontratos()
Set appWD = CreateObject("Word.Application")
appWD.Visible = True
appWD.Documents.Open Filename:="C:\Users\lguimaraes\Dropbox\Trabalho em equipe\Laurence\Contracts.docx"
appWD.ActiveDocument.SaveAs Filename:= _
ThisWorkbook.Path & "/" & "Contratos" & Range("C2").pdf, ExportFormat:= _
wdExportFormatPDF, OpenAfterExport:=True, OptimizeFor:= _
wdExportOptimizeForPrint, Range:=wdExportAllDocument, From:=1, To:=1, _
Item:=wdExportDocumentContent, IncludeDocProps:=True, KeepIRM:=True, _
CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
BitmapMissingFonts:=True, UseISO19005_1:=False
ChangeFileOpenDirectory "C:\Users\lguimaraes\Dropbox\Trabalho em equipe\Laurence\Contratos"
appWD.ActiveDocument.Close
appWD.Quit
End Sub
Try to change the beginning of problem line into:
appWD.ActiveDocument.ExportAsFixedFormat Filename:= _
ThisWorkbook.Path & "\" & "Contratos" & Range("C2") & ".pdf", ExportFormat:= _
and keep the other part the same.
EDIT after comment:
Than back to 'save as' method and try the followings:
appWD.ActiveDocument.SaveAs _
ThisWorkbook.Path & "\" & "Contratos" & Range("C2") & ".pdf", FileFormat:=17
This time try to use new instruction instead of your complex '.SaveAs` method.

Resources