Macro VBA to save As PDF file

TimvMechelen

Board Regular
Joined
Nov 7, 2016
Messages
121
English:
Hi,

I'd like to have a macro which can save a particular sheet named "Afdrukpagina Draaien" as PDF file. Here I want to save only what's in the printrange.
Also, I'd like to have the path (location) where the file must be saved in a particular cell. This path (in this case: "H:\Mijn documenten\EXCEL MODEL\Testcalculaties\Opgeslagen als PDF") can be found on sheet "Beginblad" in cell "P3". So, the macro must be reffering to this cell, so i can always change the save location.

I also would like to change the Filename. That name is in sheet "Beginblad" in cell "P4".

The macro/VBA i have now is:

Code:
Sub OpslaanAls_AfdrukpaginaDraaien()
'
' OpslaanAls_AfdrukpaginaDraaien Macro
''
    Sheets("Afdrukpagina Draaien").Select
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "H:\Mijn documenten\EXCEL MODEL\Testcalculaties\Opgeslagen als PDF\Opzet parameters Excel Model_18.pdf" _
        , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=True
End Sub

Maybe someone can edit this macro so i can refer to the cells P3 and P4 for the name and location of the PDF file.

I hope somebody can help me.



Nederlands:
Hallo allemaal,
Ik zou een macro willen waarmee ik een bepaalde sheet genaamd "Afdrukpagina Draaien" op wil slaan als PDF. Hierbij wil ik alleen hetgeen in het afdrukbereik opslaan.

Ook wil ik het pad(de locatie) waar het bestand opgeslagen moet worden in kunnen voeren. Dit pad (bijvoorbeeld in dit geval: H:\Mijn documenten\EXCEL MODEL\Testcalculaties\Opgeslagen als PDF) is te vinden op sheet "Beginblad" in cel "P3". Dus in de macro moet hierna verwezen worden, zodat het pad altijd gewijzigd kan worden.

Verder staat in een bepaalde cel ook de naam die het PDF bestand zou moeten krijgen. Deze cel staat op de sheet "Beginblad" in cel "P4"
Ik hoop dat iemand mij hiermee verder zou kunnen helpen.

De macro die ik nu heb is:
Code:
Sub OpslaanAls_AfdrukpaginaDraaien()
'
' OpslaanAls_AfdrukpaginaDraaien Macro
'
'
    Sheets("Afdrukpagina Draaien").Select
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        "H:\Mijn documenten\EXCEL MODEL\Testcalculaties\Opgeslagen als PDF\Opzet parameters Excel Model_18.pdf" _
        , Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
        :=False, OpenAfterPublish:=True
End Sub

Hierin zou ik graag de locatie willen wijzigen waar het bestand opgeslagen wordt, en de naam.
De locatie moet uit de cel "P3" op sheet "Beginblad" gehaald worden.
De naam van het bestand moet uit cel "P4" op sheet "Beginblad" gehaald worden.

Misschien iemand die voor mij deze macro aan zou kunnen passen?
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Try:
Code:
Sub OpslaanAls_AfdrukpaginaDraaien()
'
' OpslaanAls_AfdrukpaginaDraaien Macro
''
With Sheets("Afdrukpagina Draaien")
  .Range("Print_Area").ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:=.Range("P3") & "\" & .Range("P4") & ".pdf", _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, OpenAfterPublish:=True
End With
End Sub
 
Last edited:
Upvote 0
It doesn't work. I think that macro takes the cells P3 and P4 from the sheet "Afdrukpagina draaien" and not from the sheet "Beginblad"
 
Upvote 0
Missed that. Try:
Code:
Sub OpslaanAls_AfdrukpaginaDraaien()
'
' OpslaanAls_AfdrukpaginaDraaien Macro
''
Dim StrFilePathAndName As String
With Sheets("Beginblad")
  StrFilePathAndName = .Range("P3") & "\" & .Range("P4") & ".pdf"
End With
With Sheets("Afdrukpagina Draaien")
  .Range("Print_Area").ExportAsFixedFormat _
    Type:=xlTypePDF, Filename:=StrFilePathAndName, _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, OpenAfterPublish:=True
End With
End Sub
 
Last edited:
Upvote 0
Missed that. Try:
Code:
Sub OpslaanAls_AfdrukpaginaDraaien()
'
' OpslaanAls_AfdrukpaginaDraaien Macro
''
Dim StrFilePathAndName As String
With Sheets("Beginblad")
  StrFilePathAndName = .Range("P3") & "/" & .Range("P4") & ".pdf"
End With
With Sheets("Afdrukpagina Draaien")
  .Range("Print_Area").ExportAsFixedFormat _
    Type:=xlTypePDF, Filename:=StrFilePathAndName, _
    Quality:=xlQualityStandard, IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, OpenAfterPublish:=True
End With
End Sub

still doesnt work. i get a pop up:
Failure -2147024773 (8007007b)
document is not saved
 
Upvote 0
In that case you should check that you have valid content in P3 & P4 on the Beginblad sheet, including no illegal filename characters. Also, you might add:
MsgBox StrFilePathAndName
after the first:
End With
This will generate a message saying where the new file will be saved and what it will be called.

PS: There was a typo in the code I posted - "/", which should have been "", but that should not affect the save.
 
Last edited:
Upvote 0
In that case you should check that you have valid content in P3 & P4 on the Beginblad sheet, including no illegal filename characters. Also, you might add:
MsgBox StrFilePathAndName
after the first:
End With
This will generate a message saying where the new file will be saved and what it will be called.

PS: There was a typo in the code I posted - "/", which should have been "", but that should not affect the save.

Thank you a lot! I think the problem was the filename. The filename was "280.000.900; Blade; One+; Revisie 012; 07-11-2016 12:42".
Why is this a problem?
 
Upvote 0

Forum statistics

Threads
1,216,101
Messages
6,128,838
Members
449,471
Latest member
lachbee

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top