cmdbtn to save as pdf with cell values

DB73

Board Regular
Joined
Jun 7, 2022
Messages
102
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2010
  6. 2007
Platform
  1. Windows
  2. Mobile
  3. Web
Hi,

I want to save a file as PDF with a cmdbtn
i want the filename as set as in a cell, like cell A1
i also want the path name as a cell value/text, co i can change the path if neccecary.

pathname for now is like desktop...dont know if it matters if its a onedrive path ?
and also dont know if there should be a folder on the desktop

i have someting like this, but doesnt seems to work;
VBA Code:
Private Sub CommandButton2_Click()
Dim wks As Worksheet
Set wks = ActiveSheet

Dim Path As String
Path = ActivesheetRange(“D2”).Value

Dim filename As String
filename = ActiveSheet.Range(“I18”).Value

ActiveSheet.ExportAsFixedFormat Type:=TypePDF, filename:=filename
End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Hi
Try
VBA Code:
Sub test()
    Dim wks As Worksheet
    Dim fname As String
    Set wks = ActiveSheet
    Dim fPath As String
    fPath = ActiveSheet.Range("D2").Value
    fname = ActiveSheet.Range("A1").Value
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:=fPath & fname
End Sub


BTW should not use excel keyword as variable
in this case filename
 
Upvote 0
Hi Mohadin,

it doesnt seem to work.🤔
but i also dont get any error🥴
i changed it to;
VBA Code:
Sub commandbutton2()
    Dim wks As Worksheet
    Dim fname As String
    Set wks = ActiveSheet
    Dim fPath As String
    fPath = ActiveSheet.Range("D2").Value
    fname = ActiveSheet.Range("I18").Value
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:=fPath & fname
End Sub
 
Upvote 0
Ok
Can you show the contents of Range("I18")
It should be ended with \
So either add it manually or
VBA Code:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:=fPath & "\" & fname
 
Upvote 0
this should be my desitation folder
1686399163879.png


and this the file name
1686399219105.png


i added the line now bcause i dindnt had that slash

but still not working, withou any errors given.
mabe the files are saved on another location, but i cant find them
 

Attachments

  • 1686399132212.png
    1686399132212.png
    11.7 KB · Views: 3
Upvote 0
Upvote 0
Then try to map it to desktop

C:\Users\****\Desktop\ '<< chang *** to your pc name and see what​
 
Upvote 0
got it working with:
VBA Code:
Private Sub CommandButton2_Click()
    ActiveWorkbook.Save
    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, filename:= _
    Range("A1").value, Quality:=xlQualityStandard _
        , IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False
End Sub

A1 = formula CONCATENATE(D3;"any text";I19;"_any text";I20)

D3 ist path name with "\" at the end
any text for me is text "invoice No.
I19 = file name of the PDF as i want
any text is for me "_date"
I20= invoice date

A1 is hidden.
just have to adjust the path in D3, I19 file name and I20 date
 
Upvote 0

Forum statistics

Threads
1,215,455
Messages
6,124,937
Members
449,196
Latest member
Maxkapoor

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