Excel pdf exporting - Run-time error '5" in Excel VBA - vba

I've developed code that has been functional for the last 3 years on multiple company computers, all running Windows 7 and Office 2007. A new round of lease renewals has occurred and one of my employees has been given a machine running Windows 10 and Office 2007. We are all running different versions of Acrobat Pro.
I am having a problem with a macro that's in my Excel workbook that outputs a PDF whenever a user clicks on a button embedded in the sheet. Basically, the sequence is:
User presses button.
Spreadsheet recalculates (F9).
User is prompted where to save the file (filename is already prepopulated based upon a cell in the sheet).
User selects save and pdf is saved.
Pop-up box prompts user whether the new pdf should be printed to the local printer. If "yes", print, else don't.
End sequence.
Here's the code:
'Code for creating the PDF document and subsequently displaying it
Dim fnamePDF As String
With ActiveSheet
fnamePDF = .Range("H11").Value
.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ActiveWorkbook.Path & "\" & fnamePDF, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
End With
Any help would be most appreciated.

Related

Macros formatting worksheet as xlsm instead of pdf

I have created a macro that once a button is clicked on a worksheet a new Outlook email is generated with the worksheet as an attachment.
When recording the macro and then looking at the format in the code it is saying pdf. When I trial the button the attachment is formatted as a xlsm.
Sub email()
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Users\ROANDE~1\AppData\Local\Temp\Purchase Order Turkey MASTER Version 2.pdf" _
, Quality:=xlQualityStandard, IncludeDocProperties:=False, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
Application.Dialogs(xlDialogSendMail).Show
End Sub
I'm completely new to VBA.
Nice try. :) but...
Application.Dialogs(xlDialogSendMail).Show
...is the VBA equivalent of clicking File → Send (or Share in some versions), which opens an e-mail message in Microsoft Outlook with the current workbook attached.
You have 2 lines of code there. The first line exports the Active Workbook to a PDF file located at C:\Users\ROANDE~1\AppData\Local\Temp\Purchase Order Turkey MASTER Version 2.pdf.
That is unrelated to the next line (a hint being a fact that the 2nd line doesn't mention a filename, variable, etc.).
I'm always been a fan of the beginner technique of recording a macro doing what you want VBA to do, and then adjust as required, but some things do get missed (or ignored) by the Macro Recorder, probably like in this case.
1st, can you please confirm that the PDF exists (at the path above) and has the correct data that you want to email? Also which version of Excel are you using? (next time add the tag for your version when posting your question, like excel-2016 etc.)
After that's steps verified working, we can move on to emailing the PDF...

Trying to save and print a single sheet in Excel using VBA

I have been looking into an ongoing problem for my manager and I am stumped, He is trying to get it so when the end user clicks a macro assigned button on one of the Excel sheets, it saves a single sheet as PDF and prints the sheet too.
I have got the point shown by the image provided at the very bottom and the code runs up until the 'ActiveSheet.ExportAsFixedFormat' line. Does anyone have any suggestions on what I should use in order to get the explained?
I would really appreciate it.
Here's the code that I have till now:
Public Sub SavePrint()
ChDir "Path-to-the-file"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"Path-to-the-file\ACT Form.pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub
Regards, Kieran
Note1: ChDir needs to be used appropriately; after that you changed the path, you don't need to include it in the Filename and Excel automatically save the file in the current directory.
Note2: You need PDF add-in to use ExportAsFuxedFormat. To make sure that you have it navigate to
Excel > File > Options > Add-Ins > (at the bottom) Manage: COM Add-ins
Then find Adobe PDF or any other application that you use for managing PDFs (e.g. NitroPDF or BlueBeam) and check the box. If you don't have Adobe PDF then you need to install it on your machine and activate it in excel (i.e. checking the box) prior to running this macro.
Public Sub SavePrint()
ChDir "C:\"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"ACT Form.pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
End Sub
This will save the PDF file in C:\ folder.
Read this post to learn more about ChDir.
Read this to get an insight about ExportAsFixedFormat.
You can find out how to add Adobe PDF to the excel here.

Open other Excel files with password once main worksheet is opened

I'm working with 5 different worksheets. 1 main worksheet and 4 reference worksheets(with links on the main file) which require a password to open. I made a code to open the 4 reference worksheets as soon as the main worksheet is opened but when I did that, it still prompts me to enter the password. What happens now is that the main file + the 4 reference files open right away but the prompt still shows up.
Here's my code:
Private Sub Workbook_Open()
Workbooks.Open Filename:=Worksheets("sheet1").Range("j1") _
& Worksheets("sheet1").Range("j2"), Password:="*****"
Workbooks.Open Filename:=Worksheets("sheet1").Range("j1") _
& Worksheets("sheet1").Range("j3"), Password:="*****"
Workbooks.Open Filename:=Worksheets("sheet1").Range("j1") _
& Worksheets("sheet1").Range("j4"), Password:="*****"
Workbooks.Open Filename:=Worksheets("sheet1").Range("j1") _
& Worksheets("sheet1").Range("j5"), Password:="*****"
End Sub
And I get this prompt when I open the main file:
What I would like to happen is for the prompt to go away of if there's a VB script to enter the password for the prompt, that'd be okay.
If you are attempting to open protected worksheets within the workbook your code should be something like this:
Sheets("sheet1").Unprotect "Your Password"
Otherwise I thought open workbook needs some ()
Workbooks.Open(Filename:= "file.xls", Password:="ABC")

Excel VBA code is causing my worksheets to appear as jargon

I currently am using a VBA macro that I found here on stack over flow. The problem is when I run it it saves all the data into a separate excel sheet but when i open it it appears as "jargon" in other words unreadable type. This is the "Save code"
'Save the new workbook, and close it
wb.SaveAs ThisWorkbook.Path & "\test" & WorkbookCounter
wb.Close
The way I am currently running the code is that it separates my excel sheets into different spread sheets by rows of 250. Everything works but when I open the saved documents it says that this file format is unacceptable to Excel. Then I try importing it and I get an error. Here is a snap shot of the way it appears in my screen. Also here is the file name: built.list\test.xls39
Your workbook counter always ends in a number, Windows and Excel use the file extension to determine file-type, so an '.xls39' file is unrecognisable. Try:
wb.SaveAs _
Filename:=ThisWorkbook.Path & "\test" & WorkbookCounter & ".xls" _
FileFormat:=XlFileFormat.xlExcel8
'Use xlOpenXMLWorkbook for the .xlsx format
(Use space followed by underscore to separate lines in VBA)
Or make sure WorkbookCounter ends in .xls and not a number.
(Edit: For other formats, please look in the References dialog in Excel VBA Editor)

Excell VBA export some worksheets to PDF with ExportAsFixedFormat in a specified order

I am trying to create a PDF report in Excel which consists of a TitleSheet and a ReportSheet. When I select the two sheets I need and use ExportAsFixedFormat to generate the PDF I can't seem to control the order which the sheets are added to the document. As of now, the TitleSheet ends up at the bottom of the ReportSheet... not very helpful.
Here's my relevant code and some comments for things I tried that didn't work.
Dim FilePath As String
FilePath = Application.GetSaveAsFilename(ReportSheet.Name & ".PDF", "PDF(*.pdf),*.pdf*", 1, "Save As PDF File")
If FilePath = "False" Then Exit Sub
TitleSheet.Select 'so it's the first in the DPF report, no worky
Dim PrintSheets(1) As Variant
PrintSheets(0) = TitleSheet.Name 'changed index order, no worky
PrintSheets(1) = ReportSheet.Name
Sheets(PrintSheets).Select 'select both sheets so they both print
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
FilePath, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=True
ReportSheet.Activate 'select this sheet so it's visible at the end
I looked for a few other answers (Exporting multiple pages to PDF in a specific order) but no cigar yet. I have different column widths between the TitlePage and the ReportPage so I don't want to go through the headache of merging those. I also can't use a 3rd party PDF creator because a mojority of users don't have admin rights on their machines to install any additional software.
After much searching and testing I found out that ExportAsFixedFormat exports sheets based on the tab/index order. Which means I need to index ReportSheet after TitleSheet with this line of code:
ReportSheet.Move after:=TitleSheet
Viola! Special thanks to Ken Puls: (http://www.excelguru.ca/forums/showthread.php?234-Reorder-pages-when-exporting-as-PDF&highlight=ExportAsFixedFormat)