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.
Related
How to remove read-only flag when file gets saved?
Because the file I work in to run this code is read-only it seems to save the document as read-only, but how to save the file so other people can still edit it?
comp = Environ("username")
fname = "C:\Users\" & comp & "\Testing\" & Format(Now(), "yyyymmdd-hh.mm") & "
Testing " & ".xlsx"
MsgBox "Correct saved" & vbNewLine & "Yes"
ActiveWorkbook.SaveAs Filename:=fname, FileFormat:=xlOpenXMLWorkbook
You can change the access permissions for a workbook by using the .ChangeFileAccess method. I would also look if you are opening the workbook in read-only mode, instead of in read/write. If you opened in read/write the access should not change when you .save the file.
That said since you are using .SaveAs you can try changing the file access to xlReadWrite before saving it. Try:
ActiveWorkbook.ChangeFileAccess Mode:=xlReadWrite
ActiveWorkbook.SaveAs Filename:=fname, FileFormat:=xlOpenXMLWorkbook
In the line "ActiveWorkbook.SaveAs Filename:=fname, FileFormat:=xlOpenXMLWorkbook" try add ReadOnlyRecommended:=False, and FileFormat:=xlWorkbookDefault so:
ActiveWorkbook.SaveAs Filename:=fname, FileFormat:=xlWorkbookDefault, ReadOnlyRecommended:=False
Hey guys thanks for all the answers I fixed it.
comp = Environ("username")
fname = "C:\Users\" & comp & "\Testing\" & Format(Now(), "yyyymmdd-hh.mm") & " Testing " & ".xlsx"
MsgBox "Correct saved"
ActiveWorkbook.SaveAs Filename:=fname, FileFormat:=xlWorkbookDefault, ReadOnlyRecommended:=False, CreateBackup:=False, WriteresPassword:="", Password:=""
File gets saved now without password or read-only mode, also there won't pop-up a message saying the autor wants you to use read-only.
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'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
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 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
: