Force read-only prompt to "no" in Excel via VBA - excel

I have an excel spreadsheet that are being used by multiple users. I saved it as read-only recommend prompt pop-up.
However, when I to use another program to open this file ( to refresh database and save the file ) , I still have to select read-only to 'No' from the prompt pop-up everytime.
How could I temporary enable the read-only prompt pop-up ?
Thank you.

if opening from excel ... this should do it
Application.DisplayAlerts = False
Workbooks.Open ("yourfile.xlsm"
Application.DisplayAlerts = True
if opening from elsewhere, perhaps you could just use a "stub" workbook or xla with the same 3 lines of code to get at yourfile.xlsm. Hope that helps.

Related

SaveAs Template With External Data Connections

I have been hunting for an answer to this for almost an hour now with no luck. I have an Excel Macro Enabled Template which saves the template as a new name (still as a .xltm) but unfortunately this new file becomes corrupt because my template contains external data connections and I am using Application.DisplayAlerts = False to bypass any prompts upon saving. I do not want users to have to deal with a prompt, but when saving a template file with external data connections a prompt appears stating "This workbook contains external data. Do you want Microsoft Excel to clear the data before saving the template, and then automatically refresh the data whenever the template is opened?" with Yes, No and Cancel buttons. What I need is for the No to be selected so the user does not see this prompt and the template is saved without that feature. Is this possible?
Note: If I just leave the Application.DisplayAlerts as False, when the new template file is opened an error message is received stating the file is damaged because that external data prompt was never addressed. Not what I want to see at all.
Example of code:
Application.DisplayAlerts = False
Dim strSaveTool as String
strSaveTool = [TL_Loc] & "\New Template Name.xltm" 'where [TL_Loc] holds the directory path
ThisWorkbook.SaveAs Filename:=strSaveTool, FileFormat:=53
#PEH I am using sendKeys "N" ... Using your code add SendKeys "N" before the line that raises the prompt. DisplayAlerts needs to be on otherwise the prompt assumes Yes.
To figure out what Key to send experiment on the prompt concerned .. for the - 'This workbook contains external data etc' prompt ... Typing N is accepted in place of clicking NO
Application.DisplayAlerts = TRUE ' False
Dim strSaveTool as String
strSaveTool = [TL_Loc] & "\New Template Name.xltm" 'where [TL_Loc] holds the directory path
SendKeys "N"
ThisWorkbook.SaveAs Filename:=strSaveTool, FileFormat:=53

How to open a macro enabled Excel file on a SharePoint in edit mode from an Excel vba?

I have an macro enabled Excel file on a SharePoint that when a user opens it from the SharePoint the file opens programmatically another macro enabled Excel file on the same SharePoint. The file being opened by the vba macro needs to be edited, and must be editable by multiple users at the same time. However I can't get it to even open in edit mode. Using Office 365
I've tried << ActiveWorkbook.LockServerFile >> but always get an error message << Run-time error 1004: Method 'LockServer' of object'_Workbook' failed >>.
The code that I show below is in the Excel file that is opened manually by the user and that opens automatically the other Excel file. The other Excel file when opened works fine (if I remove the LockServerFile command), all it's macro's work fine, but it is open in read only and changes cannot be saved. Again this file should be editable by multiple users simultaneously.
' this code is in the "ThisWorkbook" tab
Sub workbook_open()
Set DB = Workbooks.Open(DBname, 3, False, , , , True)
ActiveWorkbook.LockServerFile ' this is where is crashes
'more code...
End Sub
' Note: DB is declared in a module
Public DB as Workbook
Public Const DBname As String = "https://ledvance365.sharepoint.com ... .xlsm"
Looks like << ActiveWorkbook.LockServerFile >> wasn't working because the SharePoint settings was not on "Open Documents in Client Applications by Default"
But once I got the SharePoint owner to change the SharePoint settings to "Open Documents in Client Applications by Default" the << ActiveWorkbook.LockServerFile >> command worked.
Maybe check out the following link to check if the file is already locked.
https://www.mrexcel.com/forum/excel-questions/906983-vba-support-checking-if-file-locked-sharepoint.html
Also in general it helps if you use your objects when you set them.
Dim DB as Workbook
Set DB = Workbooks.Open(filename:=DBname, editable:=True)
DB.LockServerFile
I had a similar issue. The code crashes at the same point you highlighted.
ActiveWorkbook.LockServerFile
Seems to fail when the document is already editable.
On Error Resume Next
ActiveWorkbook.LockServerFile
Fixes the issue I was experiencing since the code will continue on when the file is already editable and similarly makes a file editable if it wasn't previously.

Excel 2016 sharepoint file still opens as readonly

Since updating to Office 2016 I can't get excel to open a sharepoint file as editable, despite declaring it to do so.
Workbooks.Open ThisWorkbook.Sheets("Filelist").Cells(i, 2), _
UpdateLinks:=False, ReadOnly:=False, Local:=True, Editable:=True
The file opens without issues, but I have to run a break on the next line to stop the macro and manually select EDIT, before allowing the code to proceed.
Whilst this is an obvious work around, I am looping through about 40 files, and have to do this manually in each open instance.
Ok so I found a solution to this LockServerFile is the equivalent of hitting the Edit Workbook button.
When opening with VBA you can follow the open command with:
Workbooks.Open ThisWorkbook.Sheets("Filelist").Cells(i, 2)
ActiveWorkbook.LockServerFile
Solved my problem for now if anyone comes across a similar issue.
ActiveWorkbook.LockServerFile
The above code will lock that workbook for editing.
You can edit the workbook and you can save it.
But when you try to open the workbook again by manually, the changes you had done will not reflect in that sheet.

Excel 2010 asking for PW twice upon Open via VBA in open of another workbook

I have a Workbook that, when opened, I would like to pull copied information from another workbook located on a shared drive on a remote server. This remote file, though, is password protected. When these files are on the same computer, it works fine. When tested over the network, though, it prompts me for the password almost as if it were a loop glitch. That is to say, when it prompts me, even if I cancel, it will still use the password given from the code and complete.
Private Sub Workbook_Open()
Application.DisplayAlerts = False
Set DATES = Workbooks.Open("Shareddrivepath\test.xlsx", password:="test")
Range("G2:H27").Copy
ThisWorkbook.Sheets(1).Range("C2:D27").PasteSpecial
Range("J2:K27").Copy
ThisWorkbook.Sheets(1).Range("G2:H27").PasteSpecial
Range("A1:E21").Copy
ThisWorkbook.Sheets("SET").Range("A1:E21").PasteSpecial
ThisWorkbook.Sheets(1).Activate
DATES.Close
Application.DisplayAlerts = True
End Sub
Is there something I am missing that can load that password immediately or will it have to be linked to another sub called upon when opened? I have also tried just merely linking the cells and it still requires/requests password on start.
I believe I found a way around this:
http://office.microsoft.com/en-us/excel-help/create-and-manage-links-to-other-workbooks-HA001054812.aspx
Linking to password protected workbooks
Assigning a protection password to a source worksheet or workbook does not prevent users of linking workbooks who don't know the password from updating the links. If you want users to have to enter a password to update links, save the source workbook file with a password to open (click Save As on the File menu, click General Options on the Tools menu).
Set DATES = Workbooks.Open _
("Shareddrivepath\test.xlsx", WriteResPassword:="test")

Why is this file always saved as FALSE.xls?

I wrote a VB script that creates an .xls file, based on .xlt file. Then it calls a macro from the .xls file that populates it with information from a database. In the last step the script saves the .xls file on the disk.
I did this before with VB and Excel 2003. Now I upgraded to Excel 2007 and before it saves the file, a window pops up and tells me that:
"The following features can't be saved in macro free workbooks:
VB project
...some yada yada about what the Yes and No option do.
And the yes and no buttons in the dialog box.
"
I want the script to automatically select and execute Yes in the dialog box. But I can't figure how to do this. I've also posted the script I wrote.
If you have a better approach for this please share.
Thank you,
Steve
Sub Main()
Dim xl_app
Set xl_app = CreateObject("Excel.Application")
xl_app.Workbooks.Open("E:\Work\Send Mail\Clienti.xls")
'Run the macro
xl_app.Run( "ImportData(""Data Source=SFA;Initial Catalog=Campofrio;
Integrated Security=SSPI;Connect Timeout=3000"", -1, 47)")
xl_app.ActiveWorkbook.SaveAs FileName="E:\Work\Send
Mail\Clients.xls",FileFormat=xlNormal
xl_app.Quit
Set xl_app = Nothing
End Sub
Now the cod works but instead of saving the file at the specified location, it saves it in My Documents folder under FALSE.xls.
Merging responses from the two duplicate questions the poster asked:
1
Preventing False.xls when saving files in Excel
2
You are using named parameters in the .SaveAs wrong. When writing out the named parameter you'll have to do it in the format
FileName:="e:\myfile.xls"
Notice the colon before the equal sign.
If you just write Filename="myfile.xls" then its a boolean comparison that will return false. And thats why it save the file as false.xls.
Really funny error I think. ;)
I'm not sure if this is the problem, but shouldn't you just be saving as a .xlsm instead of .xls, which lets you save a macro-enabled workbook in Office 2007? You can check which version the macro is running on and if it's Excel 2007 then save as .xlsm, .xls otherwise...
Do you really need to save it with the macros included? If not, use:
ActiveWorkbook.SaveAs Filename:="E:\Work\Send Mail\Clients.xls", FileFormat:=xlNormal
EDIT: the key is that the extension used should match the FileFormat specified. The above works for me (to exclude macros) and the below works for me (to include macros). Neither has any popup, and both end up in the right directory (as the other poster mentioned, you have to have the := if you specify FileName; otherwise, use the form below.
ActiveWorkbook.SaveAs "c:\temp\wordmacros\mybook.xlsm", xlOpenXMLWorkbookMacroEnabled

Resources