SaveAs PDF and open in Adobe Reader as new Tab - excel

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.

Related

Excel vba code to save as pdf no longer works in excel 365 (windows 10)

Excel VBA code that worked previously on windows 7 (Excel 2013) no longer works on windows 10 (excel 365).
Now a run time error is generated.
Run-time error '-2147024773 (8007007b)'
Document not saved
The debugger highlights the 4 rows beginning with the first ActiveSheet.ExportAsFixedFormat all the way to False.
Is there a way to alter my code so this runs on windows 10 / excel 365?
Workbooks("Valuation Grail.xlsm").Save
Dim mydir As String
Dim mydrive As String
'Used activeworkbook.path instead of CurDir() because activeworkbook.path does not change for the same saved workbook
mydir = ActiveWorkbook.Path
mydrive = Left(mydir, 1)
'save original wmu as 2 PDFs
Dim month_end As String
Dim generic_vg As String
Dim archived_vg As String
Dim taa_packet As String
'creates saving format for archived pdf
month_end = Format(WorksheetFunction.EoMonth(Now(), -1), "yyyymmdd")
generic_vg = mydrive & ":\01\spec_folder\01 - DATA\Valuations Report\Valuations Report.pdf"
archived_vg = mydrive & ":\01\spec_folder\01 - DATA\Valuations Report\" & month_end & "-Valuations.pdf"
taa_packet = mydrive & ":\01\spec_folder2\#Packet Assembly\TAA\" & "12 - Valuation Grail.pdf"
'Saves the generic and archived version of valuation report to spce_folder data folder
ThisWorkbook.Sheets(Array("Table", "Table_SS")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
generic_vg, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
archived_vg, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
ThisWorkbook.Sheets("Summary").Select
'saves the pe tabs to the oshea packet assembly folder
ThisWorkbook.Sheets(Array("PE_Summary", "TAA_SS")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
taa_packet, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False
ThisWorkbook.Sheets("Summary").Select
ActiveWorkbokk.Path previously returned the full path where the workbook was located including the letter the drive was mapped to (Z, Y, X, etc.). I would then use mydrive to pull only that letter to use in my file path. This method no longer works.
I changed mydrive = "Z" and the code now works.
Ideally I'd like to find a new way to pull that drive letter so members of our team don't have to manually change the mydrive variable each time but this works for now.

VBA Script to Create PDF from Excel Goes to Printer and Doesn't Export File

I have the following code which worked well on a Windows machine but since I moved to a MacBook, it is causing me problems:
With ActiveSheet
Fname = "SalarySlip-" + employeeName + "-" + month + ".pdf"
.ExportAsFixedFormat Type:=xlTypePDF, fileName:= _
salarySlipsFolderPath & Fname, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False,
OpenAfterPublish:=False
End With
Rather than exporting as a PDF file (it worked like that on Windows), a request is sent to the printer to print the file. What am I missing here?
I am using MacOS Catalina 10.15.4 running MS Excel for MAC version 16.40.
If one cell value you're using as a variable includes slashes (for example the date in month as 2020/01/01), the naming process of the PDF file fails and it sends it out to the printer. I had that for several desperate hours...
And try
ChDir "/" & ActiveWorkbook.Path
as first line in the Sub and
fileName:= _
"/" & salarySlipsFolderPath & Fname, Quality:=xlQualityStandard, _
as filename. The "/" at the beginning made it working for me.

How to set a dynamic name to a PDF range from Excel VBA

I am looking at creating a PDF from an excel document. I only need part of the worksheet to become part of the PDF. The naming convention of the PDF needs to be dynamic as it is a quotation document.
I have tried the below code already. This is so we can attach the document to an email or build the code to deliver the email to a customer but currently it is failing to run.
```Sub SetEmailToPDF()
ChDir "S:\PDF Quotes"
Worksheets("Email").Range("A1:F70").ExpportAsFixedFormat Type:=xlTypePDF, Filename:= _
"S:\PDF Quotes-" & Format(Now, "DDMMYY") & Worksheets("Email").Range("c12") & ".pdf", OpenAfterPublish:=True
End Sub```
What i expect this to do is produce is a PDF with the file name of 120419Companyname.pdf and this should be saved in s:\PDF Quotes
I have also tried to simplfy to the below code and still get the 438 run time error:
Sub SetEmailToPDF()
ChDir "S:\PDF Quotes"
Worksheets("Email").Range("A1:F70").ExpportAsFixedFormat Type:=xlTypePDF, Filename:= _
"S:\PDF Quotes\test.pdf", OpenAfterPublish:=True
End Sub

Creating a filename which can be used by everyone with different paths

I have a problem with creating a PDF with a button in Excel (VBA).
We have this code:
Sub pdf_drucken2()
ChDir "C:\Users\Name"
Worksheets("LaptopReport").ExportAsFixedFormat Type:=x1TypePDF, Filename:= _
"C:\Users\Name\test213.pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
True
End Sub
I would like to create this PDF on every computer. When I send this Excel Sheet to someone, he should be able to create the PDF as well. So is there a way I can create a PDF without specifying a path like "C:\Users\Name\test.pdf"?
You can use environment variables to get the username:
Filename:= "C:\Users\" & Environ$("UserName") & "\Desktop\test213.pdf"

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,

Resources