Solved VBA Code to save as a PDF

July 10, 2019 at 11:47:29
Specs: Windows 10 Enterprise
Hello,
I am trying to figure out why my code is not working the way I want to. Everything works fine except the location of where the file is saving. I already specify where I want the file to go and tried different things and it keeps saving it in the "Documents" folder.
Do you know why is this happening? Thanks

This is my script:

Sub SavePDF()
'
' SavePDF Macro

'Set path to Mechanics Inspection Log
fPath = "K:\Manufacturing\Manufacturing Engineering\3. Tools"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Range("B7").Value _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=True
End Sub

message edited by Oscartm4


See More: VBA Code to save as a PDF

Reply ↓  Report •

#1
July 10, 2019 at 13:20:17
✔ Best Answer
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=Range("B7").Value _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=True

Where is the fPath in this command? Are you looking for
Filename:=fPath & "\" & Range("B7").Value
?

How To Ask Questions The Smart Way

message edited by Razor2.3


Reply ↓  Report •

#2
July 11, 2019 at 05:27:51
I am a beginner with VBA, but it worked perfectly just by adding that.
Thanks a lot!!

Reply ↓  Report •

#3
July 11, 2019 at 06:26:15
But do you know why it worked?

IOW, do you know why just including fPath = "K:\Manufacturing\Manufacturing Engineering\3. Tools" in your code didn't work?


message edited by DerbyDad03


Reply ↓  Report •

Related Solutions

#4
July 11, 2019 at 07:21:10
Because I was just defining the path but not actually implementing it in the code, right?

Reply ↓  Report •

#5
July 11, 2019 at 09:16:18
Right. To put it another way, you assigned a text string (in this case, the path) to a variable (fPath) and then didn't include the variable as part of the Filename argument.

You could also have eliminated the variable and used:

Filename:="K:\Manufacturing\Manufacturing Engineering\3. Tools\" & Range("B7").Value

Good job!

How To Post Data or Code ---> Click Here Before Posting Data or VBA Code


Reply ↓  Report •

#6
July 11, 2019 at 10:00:00
If we're going down that route, he could have also gone with:
fPath = "K:\Manufacturing\Manufacturing Engineering\3. Tools\" & Range("B7").Value
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=fPath, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _ 
OpenAfterPublish:=True

How To Ask Questions The Smart Way

message edited by Razor2.3


Reply ↓  Report •

#7
July 11, 2019 at 13:15:00
But I wouldn't use a variable name of fPath in that case. ;-)

There are varying opinions as to whether or not the filename and extension (fubar.suk) is part of the filepath.

I choose that it is not.

message edited by DerbyDad03


Reply ↓  Report •

#8
July 11, 2019 at 16:41:19
It is, and Wikipedia agrees. A path is a map to a single resource. It can be relative or absolute (or a URI), but that path includes the resource's name.

If you're asked to locate someone and hand them a package, you don't throw the package into their room as you walk down the hall, do you?

How To Ask Questions The Smart Way


Reply ↓  Report •

#9
July 11, 2019 at 18:15:40
Yet even Wikipedia appears to be wishy-washy on that point.

"A path, the general form of the name of a file or directory, specifies a unique location in a file system. A path points to a file system location by following the directory tree hierarchy expressed in a string of characters in which path components, separated by a delimiting character, represent each directory."

The first sentence includes the word "file" but everything after that uses only the words "location" or "directory". The last part of the second sentence reads "a string of characters in which path components...represent each directory." Based on that wording, the components of the path only represent locations/folders/directories, not files.

In addition, Excel VBA has 2 properties related to this: Path and FullName

Me.Path will not return the filename, just the directory hierarchy. Me.FullName will return the directory hierarchy along with the file name.

If I am asked to locate someone and hand them a package, I will the follow the path into their room and all the way up to them. At that point I will be at the end of the path. I do not consider the package (file) to be part of the path to the person (final directory).

No biggy either way. My point was that I wouldn't use fPath as the variable name if the contents of the variable was going to include the file name. I'm pretty sure that the majority of code that people write call out the "path" and the "filename" separately (such as the OP did) typically with something like fPath & fName.

message edited by DerbyDad03


Reply ↓  Report •

Ask Question