Trying to save and print a single sheet in Excel using VBA - 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.

Related

Excel pdf exporting - Run-time error '5" in Excel 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.

How to edit my macro to ignore text in a certain color(blue) when exporting sheets into a pdf report?

I am currently working on some simple VBA code executed by clicking a button. This button exports the sheets into a pdf format.
code:
ThisWorkbook.Sheets(Array(Sheet1.Name, Sheet2.Name, Sheet3.Name, Sheet4.Name)).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:="C:\Some location", Quality:=xlQualityStandard,
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
ThisWorkbook.Sheets("Intro").Select
I was wondering if it's possible to add an IF statement that would go through all sheets and skip publishing whatever is written in blue color (any variant if possible e.g. any shade of blue). The reason I want to designate this color to ensure comments can be written in blue and not be printed in the official pdf report produced by this macro.
Thank you!
You can check font color like so:
Sub CheckingA1ForBlueFont()
If Range("A1").Font.Color = vbBlue Then
MsgBox "Font color of A1 value is blue!"
End If
End Sub
However, it will work only for this particular blue color (vbBlue).

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...

(VBA) Convert all excel tabs to each separate protected pdf file

How to add password into the pdf files which generate from excel?
I have such part of following code:
Worksheets(i).ExportAsFixedFormat Type:=xlTypePDF, filename:=OutputPath2, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
I tried to add
Worksheets(i).Protect ("My Password")
and alternately
ActiveWorkbook.Protect "password",True,True
but neither worked.
Does anyone know how to make it work?
Many many thanks!
The answer is no ... Excel itself will not allow you to password protect the exported PDF file. You have to use an external tool to do so.
See the following for discussion and for alternatives explored: VBA to save a sheet to a password protected PDF and see https://social.msdn.microsoft.com/Forums/office/en-US/a26cae16-f039-4bb9-9b15-8016b7d0965a/conerting-excel-into-password-protected-pdf-using-vba?forum=exceldev

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)