How to save a sheet and PDF in VBA - excel

I created a workbook in which would be a chart to input data and stored in the same workbook. I managed to create a save path in which the PDF file would be stored but I also need in the same code the coding for saving the sheet in the same workbook as a continuation.
This is what I come up so far for only saving in the PDF format.
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:="D:" & Range("H60").Value & Format(Date, "ddmmyyyy")
If somebody could help me would be much appreciated.

Suppose the path is on sheet1
dim path as string 'path variable that stores the path
path="D:" & sheet1.Range("H60").Value & Format(Date, "ddmmyyyy")
'sheets to export
'they must be selected before exporting.
'ThisWorkbook.Sheets(Array("Sheet1", "Sheet2",...)).Select
ThisWorkbook.Sheets(Array("Sheet1", "Sheet2")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= path, _
Quality:= xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
I hope it helps you

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.

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.

Save Excel Sheet as PDF in another directory

I have a macro that works perfectly well to copie an excel file from one directory to another and adding in the title of the date. But what I need, is to copy only one sheet of the Excel file, and has it saved as .PDF
Both path (source, new location) should stay the same as the code below.
here is my existing code :
Sub CopierFichier()
Dim fso As Object
Set fso = VBA.CreateObject("Scripting.FileSystemObject")
Call fso.CopyFile("\\mtr.intra\367$\profils\R06C286A\_RD\NASC02\Desktop\test\Heures.xls", "V:\DVI\11000_Surveillance\11200_ISP\PCQ\Suivi\FeuilleDeTemps_" & Format(Now(), "DD-MMM-YYYY hh mm AMPM") & ".xls")
End Sub
EDIT:
I just tried something and It worked perfectly. There's one problem, when I run the macro, it takes an eternity to publish the pdf in the directory. Any help is appreciated
this is the code I used :
Sub Macro1()
Sheets("Grille").Activate
ActiveSheet.UsedRange.Select
ThisWorkbook.Sheets("Grille").Select
Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"V:\DVI\11000_Surveillance\11200_ISP\PCQ\Suivi\FeuilleDeTemps_" & Format(Now(), "DD-MMM-YYYY hh mm AMPM") & ".pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
True
End Sub

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"

Resources