I'm trying to help my mum remotely with her problem: she needs to save a workbook as an xlsx and a PDF. Here's my code:
Sub sb_Copy_Save_ActiveSheet_As_Workbook()
Dim wksht As Worksheet
Set wksht = ActiveSheet
Dim path As String
path = "C:\Users\" & Environ$("Username") & "\Company Name\Company Name Team Site - Documents\PO Numbers\"
wksht.Copy
ActiveWorkbook.SaveAs Filename:=path & wksht.Range("G1") & " " & wksht.Range("F1").Value & ".xlsx"
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF FileName:=path & wksht.Range("G1") & " " & wksht.Range("F1").Value & ".pdf" Quality:=xlQualityStandard OpenAfterPublish:=True
End Sub
We got it working to the point where she can save an xlsx file in the specified filepath, but attempting to export it as a PDF isn't working. She says she's getting a syntax error, but as I don't have excel myself I can't test it. I've looked at some similar questions but I can't seem to find an answer.
Thanks very much in advance
you just need to add commas so that
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF FileName:=path & wksht.Range("G1") & " " & wksht.Range("F1").Value & ".pdf" Quality:=xlQualityStandard OpenAfterPublish:=True
becomes
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF, FileName:=path & wksht.Range("G1") & " " & wksht.Range("F1").Value & ".pdf", Quality:=xlQualityStandard, OpenAfterPublish:=True
Related
I wrote a code for PDF export in Excel for Mac Office 2019 with a variable filename. It's working despite Mac's permission bugs with VBA saving as when using the makro for the first time Excel asks for the permission to access the concerning folder.
My question:
Can I anyhow give new users the option to choose their exporting path the first time they are using the makro or do I have to adjust the code for every new user personally?
Here's my (otherwise working) code so far:
Sub als_PDF_speichern_recorded_mac()
ChDir "/Users/Admin/Documents/Dokumente/Finanzen/2021/Rechnungen 2021/"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
"/Users/Admin/Documents/Dokumente/Finanzen/2021/Rechnungen 2021/Rechnung_" & ActiveSheet.Range("D11") & ".pdf" _
, Quality:=xlQualityMinimum, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=True
End Sub
EDIT
I might have a solution working universally for Mac users. It's not following my original intent that users can choose the path at first use but it's supposed to be exporting the PDF to the current path of the workbook and creating a folder if not existing already.
Please could someone with Excel for Mac (I'm using 2019) confirm:
Sub exportPDF_mac()
'export to currentpath/greatnewfolder
ChDir "/" & ActiveWorkbook.Path
MakeFolderIfNotExist (ThisWorkbook.Path & Application.PathSeparator & "greatnewfolder")
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
"/" & ActiveWorkbook.Path & "/greatnewfolder/greatfile_" & ActiveSheet.Range("D11") & ".pdf" _
, Quality:=xlQualityMinimum, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=True
End Sub
Function MakeFolderIfNotExist(Folderstring As String)
' Ron de Bruin, 2-March-2019
' http://www.rondebruin.nl/mac/mac010.htm
Dim ScriptToMakeFolder As String
Dim TestStr As String
Dim FolderStr As String
If Val(Application.Version) < 15 Then
ScriptToMakeFolder = "tell application " & Chr(34) & _
"Finder" & Chr(34) & Chr(13)
ScriptToMakeFolder = ScriptToMakeFolder & _
"do shell script ""mkdir -p "" & quoted form of posix path of (" & _
Chr(34) & Folderstring & Chr(34) & ")" & Chr(13)
ScriptToMakeFolder = ScriptToMakeFolder & "end tell"
On Error Resume Next
MacScript (ScriptToMakeFolder)
On Error GoTo 0
Else
FolderStr = MacScript("return POSIX path of (" & _
Chr(34) & Folderstring & Chr(34) & ")")
On Error Resume Next
TestStr = Dir(FolderStr & "*", vbDirectory)
On Error GoTo 0
If TestStr = vbNullString Then
MkDir FolderStr
End If
End If
End Function
Thanks in advance!
Thomas
i have this code for convering my excel file to pdf but after changing format, i have a pdf file with lots of pages(in every page just 4 coumns with 19 rows while my source file has 30 columns and rows , i want to see same as excel file but in pdf format , any body can help me! thanks in advance
Sub creatpdf()
Dim fName As String
Dim fname1 As String
With Worksheets("Output_" & Date)
fName2 = .Range("D3").Value
fName3 = "_BOM"
End With
BrowseForFolder = "X:\\output\\"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:= _
BrowseForFolder & "\" & fName2 & fName3 & "\" & "\" & fName2 & fName3 & "\" & "BOM" & "\" & fName2 & fName3, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False,
OpenAfterPublish:=False
End Sub
I have this huge excel file with macros, it works perfectly on windows, but on MacOs it gives me an error 1004. Can somebody help me to translate following code so that it works on Macos? Or actually, can somebody translate it to work on Excel (Macos), because I have absolute no understanding of coding. If somebody can help me with this, I don't have to install Windows to my Mac :)
Sub SaveAsPdf(train)
'On Error Resume Next
'Windows(ThisWorkbook.Name).Activate
Application.Goto reference:="date"
days = Year(ActiveCell.Value) & Month(ActiveCell.Value) & Day(ActiveCell.Value)
Application.Goto reference:="path"
Path = ActiveCell.Value
If Right(Path, 1) <> "\" Then
Path = Path & "\"
End If
ws = "Train " & train & " Production schedule"
Sheets(ws).Select
Time_Stamp = Format(Now(), "yyyymmdd_HhNn")
TNimi = Path & ws & Time_Stamp & ".pdf"
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
TNimi, Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False
ws = "General Schedule " & train
fname = "General Schedule Train " & train
Sheets(ws).Select
Time_Stamp = Format(Now(), "yyyymmdd_HhNn")
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
Path & fname & "_" & Time_Stamp & ".pdf", Quality:= _
xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, _
OpenAfterPublish:=False
Sheets("Break Plan Input").Select
End Sub
Sub SaveQSheet(train)
Sheets("Break Plan Input").Select
Application.Goto reference:="date"
days = Year(ActiveCell.Value) & Month(ActiveCell.Value) & Day(ActiveCell.Value)
Application.Goto reference:="path"
Path = ActiveCell.Value
If Right(Path, 1) <> "\" Then
Path = Path & "\"
End If
Time_Stamp = Format(Now(), "yyyymmdd_HhNn")
Sheets("Inspection and Sold Info").Select
Sheets("Inspection and Sold Info").Copy
Range("A2").Select
ActiveWorkbook.SaveAs Filename:=Path & "Train " & train & " Inspection and Sold Info " & Time_Stamp & ".xlsx", _
FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveWindow.Close
Sheets("Break Plan Input").Select
End Sub
Path defined
I recoreded a macro of me saving a file, but I want the name of the file to be
"OOR" & " " & Date & " " & Time
so the output should be OOR 10.18.2017 07:38 AM
Can someone help me with the code? Greatly apreciate it:
ChDir "C:\Users\spall1\Desktop\Base Business\Base Business Report\OOR Report"
ActiveWorkbook.SaveAs Filename:= _
"C:\Users\spall1\Desktop\Base Business\Base Business Report\OOR Report\OOR & DATE & TIME.xlsx" _
, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
You need to close the constant String part with " before adding the variable of Date and Time.
Change the last part of the string from:
\OOR & DATE & TIME.xlsx"
to:
\OOR" & Date & Time & ".xlsx"
Or, you could use VBA Now, which will give you both Date and Time:
\OOR" & Now & ".xlsx"
:
ActiveWorkbook.SaveAs _
Filename:= _
"C:\Users\spall1\Desktop\Base Business\Base Business Report\OOR Report\OOR " & Format(Now(), "YYYY-MM-DD HHMM") & ".xls" _
, FileFormat:=xlOpenXMLWorkbook _
, CreateBackup:=False
:
I have spent quite a bit looking for answer on the web, and maybe im just dumb?
any help would be greatly appreciated
FName = Environ("USERPROFILE") & "\Desktop" & "\" & IRN & Space(1) & OLDA & Space(1) & Format(Now, "mm-dd-yyyy") & Space(1) & ".pdf"
ActiveWorkbook.ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=FName, _
Quality:=xlQualityStandard, IncludeDocProperties:=False, _
IgnorePrintAreas:=True, OpenAfterPublish:=False
There are two common reasons that would cause it to be unable to save the file.
The filename is invalid - this could occur in your case if either
of IRN or OLDA contained characters which are invalid in a file
name, such as a colon.
The file that is to be saved already exists but is open. This
would lock the file and prevent it being written over.