VBA Save As PDF error

ahmed nahedh

New Member
Joined
Dec 24, 2018
Messages
1
hello guys
i make excel sheet in link below and make macro for it to save as PDF put this error always stop me can you help me with that :mad:


i used this code :
Sub save_pdf()
Sheets("Sheet1").Select


Dim filename As String
Dim ChDir As String




filename = Range("A1")


ChDir = "C:\Users\alwaha\Desktop\doctors\PDF"


Sheets("Sheet1").Range("a1:i26").ExportAsFixedFormat Type:=xlTypePDF, filename:= _
ChDir & filename & ".pdf", Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:= _
False


End Sub
please see the pictures for more information

p_108849k2b1.jpg



p_10884s88h2.jpg

p_1088i2dn13.jpg
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
You are mixing 2 things up I think
- ChDir is a VBA function which changes the working folder
- it is better practice not to use ChDir as a variable which is what you are doing (but would have worked had you not omitted the filepath separator)
- see https://www.techonthenet.com/excel/formulas/chdir.php

Either
Code:
[I][COLOR=#006400]'use a string variable[/COLOR][/I]
   Dim fpath  As String
   fpath [COLOR=#ff0000]=[/COLOR] "C:\Users\alwaha\Desktop\doctors\PDF"
   Sheets("Sheet1").Range("a1:i26").ExportAsFixedFormat Type:=xlTypePDF, filename:=fpath [COLOR=#ff0000]& Chr(92)[/COLOR] & filename & ".pdf"
Chr(92) is the usual filepath separator (\)

Or
Code:
[I][COLOR=#006400]'change the working folder like this[/COLOR][/I]
ChDir "C:\Users\alwaha\Desktop\doctors\PDF"  [COLOR=#006400] '[/COLOR] [COLOR=#ff0000]= [/COLOR][COLOR=#006400][I]NOT required[/I][/COLOR]
'Sheets("Sheet1").Range("a1:i26").ExportAsFixedFormat Type:=xlTypePDF, filename:=filename & ".pdf"   [COLOR=#006400]'[/COLOR][COLOR=#ff0000][I]path[/I][/COLOR][COLOR=#006400][I] NOT required - saves to the latest working folder [/I][/COLOR]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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