Hi, new poster here. Extremely inexperienced with VBA, no prior coding proficiency but I find myself needing to create a few bespoke tools for my work so I am learning.
I am using PK's Export all Excel worksheets in separate PDF files macro, which can be found here - Export all Excel Worksheets in separate PDF files - PK: An Excel Expert.
I have a workbook which is composed of about 20 worksheets; each worksheet is set up as a invoice for an individual client. There is also a single worksheet named CONTROL which lists all all clients and their details, and has fields for me to input their individual fees for this month, plus the date and some other info that is universal to all the invoices for this month. The individual invoice worksheets draw info from the CONTROL sheet via VLOOKUPs.
When the Export macro is run it exports all the worksheets into a single folder as individual PDFs, so I can input the totals for each client each month on the CONTROL worksheet and press one button to create the whole batch of PDFs.
I have added a line to hide the CONTROL worksheet using Worksheets("CONTROL").Visible = True before the macro exports the files, and then a corresponding second line after the important bit happens to convert the value back to False. On its own this creates a runtime error (since it can't find the hidden CONTROL worksheet to export), but this is solved with the On Error Resume Next line.
The macro works perfectly at that. Code is as follows:
Sub Multiple_PDF_Print()
Dim Folder_Path As String
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Select The Folder Path"
If .Show = -1 Then Folder_Path = .SelectedItems(1)
End With
Worksheets("CONTROL").Visible = False
Dim sh As Worksheet
On Error Resume Next
If sh.Visible = True Then
For Each sh In ActiveWorkbook.Worksheets
sh.ExportAsFixedFormat xlTypePDF, Folder_Path & Application.PathSeparator & sh.Name & ".pdf"
Next
Worksheets("CONTROL").Visible = True
MsgBox "PDFs have been exported to the selected folder."
End If
End Sub
I was hoping to put the cherry on top and have it open all the PDFs so I can check them manually before sending them out, and a quick Google search leads me to assume this should be a simple matter of using either the line OpenAfterExport:=True or OpenAfterPublish:=True, which I assume should go toward the end of the code. However, I cannot for the life of me get this to work.
Firstly, when I input the line with this exact formatting it turns red and tells me there is a compile error. It doesn't like the combination of the colon and = sign positioned adjacent to each other. If I write it as OpenAfterExport = True then the word True turns blue, like the other working functions elsewhere in the code, which appears to indicate that this is the correct formatting.
Secondly I tried both variants (Export and Publish) because I came across another forum-posted solution somewhere that worked for somebody when he used the other command, but this doesn't work for me.
Thirdly, I have tried sticking both variants absolutely all over the place in the code, but it does not matter where I put them they do not work. At this point I believe I have tried every combination of formats, commands and locations but the PDFs never open after the macro runs. Adobe Reader Touch is the default app I have to open the PDFs, I have reinstalled it.
Apologies for my inexperience, I recognise that I'm pretty out of my depth when it comes to engaging with coding matters, however I am learning. So far I have managed to put together a few working macros by copying code from different places, cobbling together elements to make what I need and fiddling around until it works, but this is the first time I have come across something I can't get working at all - and it seems so straightforward because its just one simple line.
I might be missing something fundamental in my understanding here, but could somebody please explain what I am doing wrong or why the PDFs do not open? I have seen a similar post on another forum from a MAC user, but it apparently works fine on his Windows machine.
I am using Office 365 on Windows 10. Local copy of Excel.
Thank you for your help.
I am using PK's Export all Excel worksheets in separate PDF files macro, which can be found here - Export all Excel Worksheets in separate PDF files - PK: An Excel Expert.
I have a workbook which is composed of about 20 worksheets; each worksheet is set up as a invoice for an individual client. There is also a single worksheet named CONTROL which lists all all clients and their details, and has fields for me to input their individual fees for this month, plus the date and some other info that is universal to all the invoices for this month. The individual invoice worksheets draw info from the CONTROL sheet via VLOOKUPs.
When the Export macro is run it exports all the worksheets into a single folder as individual PDFs, so I can input the totals for each client each month on the CONTROL worksheet and press one button to create the whole batch of PDFs.
I have added a line to hide the CONTROL worksheet using Worksheets("CONTROL").Visible = True before the macro exports the files, and then a corresponding second line after the important bit happens to convert the value back to False. On its own this creates a runtime error (since it can't find the hidden CONTROL worksheet to export), but this is solved with the On Error Resume Next line.
The macro works perfectly at that. Code is as follows:
Sub Multiple_PDF_Print()
Dim Folder_Path As String
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Select The Folder Path"
If .Show = -1 Then Folder_Path = .SelectedItems(1)
End With
Worksheets("CONTROL").Visible = False
Dim sh As Worksheet
On Error Resume Next
If sh.Visible = True Then
For Each sh In ActiveWorkbook.Worksheets
sh.ExportAsFixedFormat xlTypePDF, Folder_Path & Application.PathSeparator & sh.Name & ".pdf"
Next
Worksheets("CONTROL").Visible = True
MsgBox "PDFs have been exported to the selected folder."
End If
End Sub
I was hoping to put the cherry on top and have it open all the PDFs so I can check them manually before sending them out, and a quick Google search leads me to assume this should be a simple matter of using either the line OpenAfterExport:=True or OpenAfterPublish:=True, which I assume should go toward the end of the code. However, I cannot for the life of me get this to work.
Firstly, when I input the line with this exact formatting it turns red and tells me there is a compile error. It doesn't like the combination of the colon and = sign positioned adjacent to each other. If I write it as OpenAfterExport = True then the word True turns blue, like the other working functions elsewhere in the code, which appears to indicate that this is the correct formatting.
Secondly I tried both variants (Export and Publish) because I came across another forum-posted solution somewhere that worked for somebody when he used the other command, but this doesn't work for me.
Thirdly, I have tried sticking both variants absolutely all over the place in the code, but it does not matter where I put them they do not work. At this point I believe I have tried every combination of formats, commands and locations but the PDFs never open after the macro runs. Adobe Reader Touch is the default app I have to open the PDFs, I have reinstalled it.
Apologies for my inexperience, I recognise that I'm pretty out of my depth when it comes to engaging with coding matters, however I am learning. So far I have managed to put together a few working macros by copying code from different places, cobbling together elements to make what I need and fiddling around until it works, but this is the first time I have come across something I can't get working at all - and it seems so straightforward because its just one simple line.
I might be missing something fundamental in my understanding here, but could somebody please explain what I am doing wrong or why the PDFs do not open? I have seen a similar post on another forum from a MAC user, but it apparently works fine on his Windows machine.
I am using Office 365 on Windows 10. Local copy of Excel.
Thank you for your help.