I'm using Access VBA to auto update a shared Excel file - create XL object, open the shared file by sharepoint path, make necessary updates and close the file. In the codes, the displayalerts is set to False
Most of the time it works ok, but occasionally upon closing the file, it gives this prompt, even if the displayalerts is set to false.
When this happens, how do I auto select "Discard my change" by vba code?
Here's the code:
Dim XL As Excel.Application
Dim wkb As Excel.Workbook
Set XL = CreateObject("Excel.Application")
XL.Visible = False
XL.DisplayAlerts = False
Set wkb = XL.Workbooks.Open(SharePointFilePath)
'do stuff
wkb.Save
wkb.Close
XL.Quit
Set XL = Nothing
Related
I automate a process using a .vbs file and sometimes I forget to close the excel file at night, so when the process runs the file opens as a read-only causing issues. I'd like to just close or end all instances of open workbooks.
I thought a ".Quit" would do the trick but doesnt seems to close workbooks that are currently open. I test .quit in a macro and it does indeed close all open workbooks and the excel app in general .
Any ideas?
Set ExcelApp = CreateObject("Excel.Application")
ExcelApp.Applicaiton.Quit
'reopen actual file as non read only
Set ExcelApp = CreateObject("Excel.Application")
ExcelApp.Visible = True
ExcelApp.DisplayAlerts = False
ExcelFilePath = "C:\Users\user\Documents\test.xlsm"
Set wb = ExcelApp.Workbooks.Open(ExcelFilePath)
Instead of
Set ExcelApp = CreateObject("Excel.Application")
try
Set ExcelApp = GetObject(, "Excel.Application")
To bind to existing excel instance.
After an current update of Excel/Office365 to version 16.03.13127.21064 (64bit) the below dialog (Others are also making changes) pops up if I open a file with VBScript.
I already use Application.DisplayAlers = False in my VBScript to suppress dialogues, but for this dialog the setting has no effect.
This is my script:
Option Explicit
Dim inputFile
Dim macroName
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("Excel.Application")
xlApp.DisplayAlerts = False
xlApp.Visible = False
xlApp.EnableEvents = False
Set xlBook = xlApp.Workbooks.Open(inputFile)
xlApp.Run macroName
'...
Is there another option to switch Excel in a kind of interactive mode.
Thank you for help
Andreas
I want to call a macro from Outlook.
I can do it with an Excel .xlsm file.
How can I call a macro in an addin .xlam file?
Example for .xlsm
Sub trans_outlook()
Dim xlApp As Object, xlWkb As Object
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = False ' can be False if you do not wont see reaction, byt make sure is not fail
Set xlWkb = xlApp.Workbooks.Open("C:\Users\xyz.xlsm")
xlApp.Run "xyz.xlsm!Test"
xlWkb.Close savechanges:=False
xlApp.Quit
End Sub
You need to go to Tools -> References. Find Microsoft Excel 14.0 Data Objects Library in the list, and check the box next to it. Click OK
Dim ExApp As Excel.Application
Dim ExWbk As Workbook
Set ExApp = New Excel.Application
Set ExWbk = ExApp.Workbooks.Open("C:\Folder\Folder\File.xls")
ExApp.Visible = True
ExWbk.Application.Run "ModuleName.YourMacro"
ExWbk.Close SaveChanges:=True
I am opening an excel workbook from excel and delete a sheet inside. I want to do this without the message from Excel:
"Excel will permanently delete this sheet, do you want to continue"
However I cannot make the "DisplayAlerts = False" work correctly.
Public Sub xportQuery()
Dim appExcel As Excel.Application
Dim myWorkbook As Excel.Workbook
Dim PathDaily, FileName As String
PathDaily = Forms!Menu!Text69
FileName = Forms!Menu!Text84
Set appExcel = CreateObject("Excel.Application")
Set myWorkbook = appExcel.Workbooks.Open(PathDaily & FileName)
appExcel.Visible = True
'Set appExcel = Nothing
Set myWorkbook = Nothing
appExcel.DisplayAlerts = False
Workbooks(FileName).Sheets("Sheety").Delete
appExcel.DisplayAlerts = True
End Sub
Writing it this way I get a "Subscript out of range" on the Sheets("Sheety").delete
If I take of the two DisplayAlerts lines, the code works but with the alert.
How do I write my code correctly to work without any alert message?
Note: DoCmd.SetWarnings didn t work either as the message is displayed in Excel
Thanks To Sam's Comment:
it works either by changing
Workbooks(FileName).Sheets("Sheety").Delete
into
myWorkbook.Sheets("Sheety").Delete
or
AppExcel.Workbooks(FileName).Sheets("Sheety").Delete
However the rest of the macro can still use Workbooks(Filename) without the "AppExcel." Reference
I have a VBScript code to open an excel file, run a macro and close it. Fine.
Now, the only thing I want to change is to leave the file open.
If I remove the 2 lines of code xlApp.activewindow.close and xlApp.Quit, then the workbook and the application are closed anyway, but they remain open in the background (Excel process still active in Task Manager). Hence, it is impossible to re-run the macro later on the same file by calling the script again (which is exactly what I want to do).
Why?
Here is the code:
Option Explicit
On Error Resume Next
MyTest
Sub MyTest()
Dim xlApp
Dim xlBook
Dim fpath
Dim fname
' Excel application running? if not, open Excel
On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If xlApp <> "Microsoft Excel" Then
Set xlApp = CreateObject("Excel.Application")
End If
Err.Clear
' correct Excel file open? if not, open it
fpath = "D:\Desktop\"
fname = "MyTest.xls"
xlApp.Workbooks(fname).Activate
If Err = 0 Then
' no error, so it has been possible to activate the workbook
Set xlBook = xlApp.Workbooks(fname)
Else
' unable to activate, so workbook was not open -> open it now
Set xlBook = xlApp.Workbooks.Open(fpath & fname, 0, True)
End If
Err.Clear
' now run the desired macro in the excel file
xlApp.Run "HelloWorld"
' WANT TO CHANGE THIS
xlBook.saved = True
xlApp.activewindow.close
' AND THIS
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
End Sub
You just need to make your new instance of Excel visible. Do this right after creating it:
xlApp.Visible = True
This line of code will close your current activated workbook (by now, it is D:\Destop\MyTest.xls);
xlApp.activewindow.close
This line will quit Excel application;
xlApp.Quit