How to remove read-only when file gets saved? - excel

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.

Related

Exporting an Excel Workbook to PDF using VBA

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

Including SUBs While .SAVEAS

I'm converting CSVs to XLSMs using a new temporary file with the following SUB.
sub a()
j=array("apple","orange")
for each i in j
workbooks.open environ("userprofile") & "\desktop\" & i & ".csv"
activeworkbook.saveas environ("userprofile") & "\desktop\" & i & ".xlsm", xlopenxmlworkbookmacroenabled
activeworkbook.close
next i
end sub
I wonder whether I can include this SUB to each of the new XLSMs. Thanks.

Save file in excel using VBA

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
:

File Not Saved Error, converting excel sheet to PDF

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.

Saving Excel workbook to constant path with filename from two fields

I tried to search and put together a code to fit my purpose.
Sub save()
ActiveWorkbook.SaveAS Filename:="C:\-docs\cmat\Desktop\New folder\ck.xls", FileFormat:= _
xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False
End Sub
How to edit this to:
Instead of naming the saved file ck.xls, generate the filename from the worksheet cells C5 and C8, with a space in the middle.
try
Sub save()
ActiveWorkbook.SaveAS Filename:="C:\-docs\cmat\Desktop\New folder\" & Range("C5").Text & chr(32) & Range("C8").Text &".xls", FileFormat:= _
xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:=False _
, CreateBackup:=False
End Sub
If you want to save the workbook with the macros use the below code
Sub save()
ActiveWorkbook.SaveAs Filename:="C:\Users\" & Environ$("username") & _
"\Desktop\" & Range("C5").Text & Chr(32) & Range("C8").Text & ".xlsm", FileFormat:= _
xlOpenXMLWorkbookMacroEnabled, Password:=vbNullString, WriteResPassword:=vbNullString, _
ReadOnlyRecommended:=False, CreateBackup:=False
End Sub
if you want to save workbook with no macros and no pop-up use this
Sub save()
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:="C:\Users\" & Environ$("username") & _
"\Desktop\" & Range("C5").Text & Chr(32) & Range("C8").Text & ".xls", _
FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
Application.DisplayAlerts = True
End Sub
Ok, at that time got it done with the help of a friend and the code looks like this.
Sub Saving()
Dim part1 As String
Dim part2 As String
part1 = Range("C5").Value
part2 = Range("C8").Value
ActiveWorkbook.SaveAs Filename:= _
"C:\-docs\cmat\Desktop\pieteikumi\" & part1 & " " & part2 & ".xlsm", FileFormat:= _
xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
End Sub
How do I edit this part (FileFormat:= _ xlOpenXMLWorkbookMacroEnabled) for it to save as Excel 97-2013 Workbook, have tried several variations with no success.
Thankyou
Seems, that I found the solution, but my idea is flawed. By doing this FileFormat:= _ xlOpenXMLWorkbook, it drops out a popup saying, the you cannot save this workbook as a file without Macro enabled. So, is this impossible?

Resources