Macro to export Excel Doc as PDF for all Users - vba

I need to create a macro that will save an excel document as a PDF file to any user's desktop (i.e. multiple people will be using this document/macro).
Here is VBA code I have so far:
Sub CreatePDF()
'
' CreatePDF Macro
'
'
ChDir "C:\Users\Public\Desktop"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"C:\Users\Public\Desktop\QuickView Update Dec_2017.pdf", Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End Sub
I think the issue is with the directory it's saving to (in the instances where this code says "Public" in the file path, I had changed that from my username which was initially populated).
Does anyone know a way to specify a generic path to save this document as a PDF to any users' desktop?

Use .specialfolders("Desktop") to save to the desktop. Set to a string variable and add the path separator
Example
Option Explicit
Sub CreatePDF()
Dim FilePath As String
FilePath = CreateObject("WScript.Shell").specialfolders("Desktop")
Debug.Print FilePath
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
FileName:=FilePath & "\" & "QuickView Update Dec_2017.pdf", _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End Sub
The following special folders are available:
AllUsersDesktop
AllUsersStartMenu
AllUsersPrograms
AllUsersStartup
Desktop
Favorites
Fonts
MyDocuments
NetHood
PrintHood
Programs
Recent
SendTo
StartMenu
Startup
Templates
The SpecialFolders property returns an empty string if the requested folder (strFolderName) is not available. For example, Windows 95 does not have an AllUsersDesktop folder and returns an empty string if strFolderNameis AllUsersDesktop.
Here is another example
https://stackoverflow.com/a/31694603/4539709

You can use environment variables. %USERPROFILE%\Desktop should work in this instance.
Here's a list of environment variables. https://en.wikipedia.org/wiki/Environment_variable#Default_Values
Accessing environment variables with VBA: Environ Function code samples for VBA
In VBA you could translate it to Environ("USERPROFILE") & "\Desktop" This is untested as I'm not using Windows.

I have not tested this but based on what you gave try :
Dim url As String
url = Application.DefaultFilePath & "\" & ActiveWorkbook.Name & ".xls"
in your export part of your code replace filename:= blah blah to Filename:=url

Related

Detect PDF file in folder and also check if it is open

I need codes for below. As I try to find in this website, but nothing match my need. So please if anybody write some codes.
It should search a file in a folder and file name should be taken from cell whatever file name I type for search and if it is open it should warn me that file is open. The file will be in PDF format.
File shall not be duplicate if it find duplicate it shall show me warning REPLACE or NO.
If it is not a duplicate than save as PDF taking whatever name I write in cells and there will 2 different cells.
Option Explicit
Function FileExists(FullFileName As String) As Boolean
FileExists = Len(Dir(FullFileName)) > 0
End Function
Sub SaveAsPDF()
Dim nResult As Long
Dim fName As String
Const fPath As String = "C:\Users\KYD\Desktop\"
With ActiveSheet
fName = .Range("A1").Value & " " & Range("J1").Value & ".pdf"
If Not FileExists(fPath & fName) Then
MsgBox prompt:="PDF file saved." & vbNewLine & "Location <> C:\Users\KYD\Desktop\", _
Buttons:=vbOKOnly + vbInformation, Title:="Thanks"
.ExportAsFixedFormat Type:=xlTypePDF, filename:= _
fPath & fName, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
Else:
nResult = MsgBox(prompt:="PDF file already exit do you want to overwrite a previous file check in below given folder." _
& vbNewLine & "Location <> C:\Users\KYD\Desktop\", Buttons:=vbYesNo + vbCritical, Title:="MME")
If nResult = vbYes Then
.ExportAsFixedFormat Type:=xlTypePDF, filename:= _
fPath & fName, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
End If
Exit Sub
End If
End With
End Sub
i try to write these codes which export pdf and take name from cells and check if the file exited in folder ask user to replace or no.
i need some codes in it which can also check if the file is open taking name from cells. if it is opened show message the file is open else nothing.

vba save as pdf into a shared folder with different computer

Hi I wrote a code where it saves the excel sheet as PDF file into a our company's sharefolder (dropbox). I realized when my coworker tried to use that Macro, it doesn't work because of the path the file is saved.
in the code, where it says "MyComputerName" is what my computer name and i am guessing it's because my co workers computer name is different so it can't find the path on her computer.
Is there a way to solve this? so we both can use this macro and save it into the shared folder ?
Help!!!
Sub SaveAsPDF()
' FormatName
ActiveSheet.Name = "#" & ActiveSheet.Range("F6").Value & " " & ActiveSheet.Range("F4").Value
' saveAsPDF Macro
ActiveSheet.ExportAsFixedFormat Type:=xltypepdf, Filename:= _
"C:\Users\MyComputerName\Dropbox\Team Folder\PACKING LIST\201804\" & "PACKING LIST_" & ActiveSheet.Name _
, quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=True
End Sub
Try adding the following lines to the beginning of your code:
Dim username As String
username = Environ$("username")
And then your path should be:
"C:\Users\" & username & "\Dropbox\...
To make the year/month dynamic (assuming based on today's date), your link can be:
...LIST\" & Format(Now(), "yyyymm") & "\PACKING LIST...

How to Create an Excel Add-In Ribbon Button to be available to any workbook I open

I have a code that works perfectly well for me when I assign the code to a button in my excel workbook.
Option Explicit
Sub SAVEASPDF()
'SAVEASPDF Macro
Dim docname As String
docname = ActiveWorkbook.Name
docname = Replace(docname, ".xlsm", ".pdf")
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:= ActiveWorkbook.Path & "\" & docname, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True
End Sub
However, I would like to create a button (lets call it "SAVE Workbook to PDF") to add to the Add-In Ribbon that calls up this code every time I open excel. I am familiar with Visual Studio 2017 so any help would be grateful.

Saving a File as PDF using the file name and a specific file path

I'm new to VBA and trying to create a macro that will automatically save my file as a PDF file using the file's base name. This topic has been covered quite a bit in various sites so I was able to get most of the code I needed but for some reason I'm getting tripped up in the very last statement. Here is what I have so far:
Sub SaveAsPDF()
Dim SaveDirectory As String
Dim SaveFileName As String
Dim BaseName As String
Dim fso
SaveDirectory = Environ("Userprofile") & "\Dropbox\Operations\VBA Projects\"
Set fso = CreateObject("Scripting.FileSystemObject")
BaseName = fso.GetBaseName(ActiveWorkbook.Name)
SaveFileName = SaveDirectory & BaseName & ".pdf"
Sheets(Array("Page1", "Page2")).ExportAsFixedFormat Type:=xlTypePDF, _
FileName:=SaveFileName, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
End Sub
When I debug through the whole code, that last statement gets highlighted. Not sure what I"m doing wrong.
For some reason, the ExportAsFixedFormat method does not work directly with an array of sheets.
The below will work (it's one of the rare cases where Select and Activate are necessary in Excel VBA).
Sheets(Array("Page1", "Page2")).Select
Sheets("Page1").Activate
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
FileName:=SaveFileName, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False

Excel Add-in changes the file name to the add-in`s name although it should not

I need your help regarding an add-in I created using VBA, then I saved as add-in to be able to use on all Excel workbooks and also send it to my friends.
The add-in simply saves the printed area in the active sheet as a PDF file with the same name of the workbook, it saves the PDF to the desktop and works fine as a macro.
But when I save as PDF and use it, it saves the PDF file with the same name of the add-in, not as the workbook`s name.
Any suggestions?
The VBA code is:
Sub Save_as_pdf()
Dim FSO As Object
Dim s(1) As String
Dim sNewFilePath As String
Set FSO = CreateObject("Scripting.FileSystemObject")
s(0) = "C:\Users\" & Environ("UserName") & "\Desktop\" & ThisWorkbook.Name
If FSO.FileExists(ThisWorkbook.FullName) Then
'//Change Excel Extension to PDF extension in FilePath
s(1) = FSO.GetExtensionName(s(0))
If s(1) <> "" Then
s(1) = "." & s(1)
sNewFilePath = Replace(s(0), s(1), ".pdf")
'//Export to PDF with new File Path
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, _
Filename:=sNewFilePath, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=True
End If
Else
'//Error: file path not found
MsgBox "Error: this workbook may be unsaved. Please save and try again."
End If
Set FSO = Nothing
End Sub
To elaborate on Matteo NNZ's comment above:
ThisWorkbook refers to the workbook in which the code is currently being executed from (in this case, the add-in)
ActiveWorkbook refers to the workbook that is currently active within the same instance of Excel.