VBA Code to export as pdf

Chris1973

Board Regular
Joined
Apr 17, 2020
Messages
115
Office Version
  1. 2016
Platform
  1. Windows
Goodday everyone.

I have a problem with my export to pdf code

i use

Sub create PDF()

Dim ID As Atring

Id= Range("D4").Text

ActiveSheet.ExoprtAsFixedFormat _
Type:=xlTypePdf, _
Filename:C:\Users\chrish\documents\1.Wurth Master\Quotes\Pdf\" + id +".pdf",
IgnorePrintAreas:=False, _
OpenAfterPublish:=False

End Sub


My question now how do i adjust the code to save cell D4 AND D8. That must be the reference to my workbook so it wil be

3005 john
3005 is D4
John is d8

And then it needs to close the workbook when done

Thanks a mil
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hello Chris,

Don't worry about declaring a variable, try it this way:-

VBA Code:
Sub CreatePDF()

ActiveSheet.ExoprtAsFixedFormat Type:=xlTypePDF, _
Filename:="C:\Users\chrish\documents\1.Wurth Master\Quotes\Pdf\" & Range("D4").Value & " " & Range("D8").Value & ".pdf", _
IgnorePrintAreas:=False, OpenAfterPublish:=False

ActiveWorkbook.Close

End Sub

I hope that this helps.

Cheerio,
vcoolio.
 
Upvote 0
@vcoolio

Thanks for the feedback

I have done exactly as you said, but it does not save the worksheet

There is no error code it just does not safe it
 
Upvote 0
I'm not sure why it wouldn't as the procedure is a fairly standard method.

Perhaps try this:-

VBA Code:
Sub CreatePDF()

        Dim NewFN As Variant
        
        NewFN = "C:\Users\chrish\documents\1.Wurth Master\Quotes\[B]Pdf[/B]\" _
        & ActiveSheet.Range("D4").Value & " " & ActiveSheet.Range("D8").Value & ".pdf"

        ActiveSheet.ExoprtAsFixedFormat Type:=xlTypePDF, Filename:=NewFN, _
        IgnorePrintAreas:=False, OpenAfterPublish:=False

        ActiveWorkbook.Close

End Sub

I am wondering though, is the "Pdf" in your file path actually a part of the path or should it stop at ".......Quotes\" ??

Cheerio,
vcoolio.
 
Upvote 0
You completely lost me now.

I got it to work, i only try to figure out how do i save 2 cells as name

Sub create PDF()

Dim ID As Atring

Id= Range("D4").Text

ActiveSheet.ExoprtAsFixedFormat _
Type:=xlTypePdf, _
Filename:C:\Users\chrish\documents\1.Wurth Master\Quotes\Pdf\" + id +".pdf",
IgnorePrintAreas:=False, _
OpenAfterPublish:=False

End Sub

Ho do i dave it ad Range d4 d8

How do i enter the code for this here Id= Range("D4").Text this need to be d4 and d8
 
Upvote 0
That's what the code in post #2 does but if you say your original code works then add the additional name as follows:-

VBA Code:
Sub createPDF()

Dim ID As String, ID2 As String

ID = Range("D4").Text
ID2 = Range("D8").Text

ActiveSheet.ExoprtAsFixedFormat Type:=xlTypePDF, Filename:="C:\Users\chrish\documents\1.Wurth Master\Quotes\Pdf\" + ID + ID2 + ".pdf", _
IgnorePrintAreas:=False, OpenAfterPublish:=False

End Sub

Copy and paste the above into the module and see if it works.

Cheerio,
vcoolio.
 
Upvote 0
Looks like you have a typo.
VBA Code:
ActiveSheet.ExoprtAsFixedFormat _
should be
VBA Code:
ActiveSheet.ExportAsFixedFormat _
 
Upvote 0
Unfortunately not
Error 2147024773
document not saved

That error means that your file name is bad. There are certain characters you are not allowed to use when making a file name. Either Cell D4 or D8 contains characters that are illegal for use in a file name. in the example below ID & ID2 + ".pdf" must evaluate to a LEGAL file name. Use this example to inspect the file name before the export

VBA Code:
Sub createPDF_Test()

    Dim ID As String, ID2 As String, FileName As String

    ID = Range("D4").Text
    ID2 = Range("D8").Text
    FileName = "C:\Users\chrish\documents\1.Wurth Master\Quotes\Pdf\" & ID & ID2 + ".pdf"

    Select Case MsgBox("ID = '" & ID & "'" & vbCrLf _
                     & "ID2 ='" & ID2 & "'" & vbCrLf _
                     & "ID & ID2 ='" & ID & ID2 & "'" & vbCrLf & vbCrLf _
                     & "FileName ='" & FileName & "'" & vbCr & vbCr & "Export File?", vbOKCancel + vbInformation, Application.Name)
    Case vbCancel
        Exit Sub
    End Select

    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:=FileName, IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,551
Messages
6,120,156
Members
448,948
Latest member
spamiki

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