'Remote procedure call failed' when opening Excel .xlsm in VBScript - vba

I've been running into trouble opening Macro-Enabled Excel Workbooks through a VBScript that runs overnight (works with extracting data from SAP, into Excel).
It used to work all the time, but recently it keeps throwing a
Windows Script Host error 800706BE "remote procedure call failed"
on the third line of code below:
Set ExcelApp = CreateObject("Excel.application")
ExcelApp.Visible = true
Set MyBook = ExcelApp.Workbooks.Open("N:\Automation_Materials\Open AutoPO Issues\Scripts\A_scriptPackage\AB_MacroController.xlsm")
Set ExcelApp = Nothing
The file doesn't open, which causes some problems for the rest of the automated process. All the excel file should do is run some VBA to edit other workbooks that have fresh data. It's a few manipulations, then this VBA just closes up shop and exits everything. Problem is, it never even opens!

Related

How to suppress "SQL Server Login" window on Excel?

I am using Microsoft Task Scheduler to run Excel models everyday. The models retrieve data from the database and do stuff. The connection strings contain all information(username, password etc.) needed to connect to the database. However, I get this prompt sometimes:
SQL Login Prompt
When I click "OK", everything goes fine, so I need the Excel to just click OK because the username and password are already there. I want to suppress this window by clicking OK. Otherwise the models stop running and wait for me to click OK manually.
I checked for the solutions on the web but did not encounter a working one. I tried to change the connection string in various ways but it did not work.
Does anyone have any idea on how to solve this issue?
Thanks
Edit: Here is the code I am running on VBScript for Task Scheduler.
'Input Excel File's Full Path
ExcelFilePath = "MyExcelFile"
'Input Module/Macro name within the Excel File
MacroPath = "Macro1"
'Create an instance of Excel
Set ExcelApp = CreateObject("Excel.Application")
'Do you want this Excel instance to be visible?
ExcelApp.Visible = True 'or "False"
'Prevent any App Launch Alerts (ie Update External Links)
ExcelApp.DisplayAlerts = False
'Open Excel File
Set wb = ExcelApp.Workbooks.Open(ExcelFilePath)
'Execute Macro Code
ExcelApp.Run MacroPath
WScript.Sleep 10000
'Input Module/Macro name within the Excel File
MacroPath = "Macro2"
'Execute Macro Code
ExcelApp.Run MacroPath
WScript.Sleep 10000
'Save Excel File (if applicable)
wb.Save
'Reset Display Alerts Before Closing
ExcelApp.DisplayAlerts = True
'Close Excel File
wb.Close
'End instance of Excel
ExcelApp.Quit
The excel VBA code for Macro1 is just
Sub UpdateModel()
ActiveWorkbook.RefreshAll
End Sub
command for pivot tables which are getting data from the database.

MS Excel macro runs different from VB Script?

I have a VB Script file that I open using a task in Windows Task Scheduler each morning. The script opens MS Excel and then runs a macro stored in a workbook. The problem is that the macro runs differently if I use the VB Script file to kick off the macro versus when i run it myself from the workbook. Specifically, I find that "Refresh All" doesn't actually refresh all data connections when I run the macro starting from the VB Script file. It works fine if I run the macro from the workbook itself. I'm thinking that I am missing something in this VB Script to ensure MS Excel opens correctly and makes "refresh all" work properly. Anything plainly wrong with this VB Script?
Dim ObjExcel, ObjWB
Set ObjExcel = CreateObject("excel.application")
ObjExcel.Application.Visible = True
Set ObjWB = ObjExcel.Workbooks.Open("K:\Analytic Reporting\11.Projects\TonyAdHoc\Autorefresher\DashboardAutorefresher.xlsm")
objExcel.Application.Run "DashboardAutorefresher.xlsm!Main"
ObjWB.Close True
ObjExcel.Quit
Set ObjExcel = Nothing
It seems that you are trying to do a refresh to pull data from a database, but you are not seeing any new data from this script when you manually open the Excel file after the script runs.
You will need to save the Excel file after making modifications.
ObjExcel.Save
For anyone still caring about this, what I've found is that for connections to a database table, if the property "Enable Background Refresh" is selected, then the vba command in RefreshAll in the marco doesn't work. So I add a little loop to disable that property for all the connections just to make sure before I call RefreshAll. That fixed it for me.
' -- make sure enable background refresh is not checked else RefreshAll doesn't work
Dim cn As WorkbookConnection
For Each cn In ActiveWorkbook.Connections
cn.OLEDBConnection.BackgroundQuery = False
Next cn
' -- then ok to refresh all
ActiveWorkbook.RefreshAll

Excel Macro opens with run time error 32809

I have an Excel Macro Enabled workbook that I created on Excel 2013. The workbook's macros work well with my computer but does not work on some other computers even if they are using Excel 2013. Works on 7/10 computers I tried both on windows 7 and 8. When I send it to someone's computer that does not work this is what happens:
They open the workbook and once the user clicks "enable content" the workbook errors out "run time error '32809': Application-defined or object-defined error"
debug shows it getting stuck on 2nd line of code below:
private sub workbook open()
Worksheets(1).OLEObjects("ComboBox21").ListFillRange = "impacts"
**Worksheets(2).OLEObjects("ComboBox21").ListFillRange = "yesnoo"** This line errors
if I comment out these two lines, the workbook will open but the combobox21 on worksheet(2) gets renamed to combobox22 and does not work but the first combobox on worksheet(1) loads and functions fine.
I would like to add that if I comment out the lines
Worksheets(1).OLEObjects("ComboBox21").ListFillRange = "impacts"
Worksheets(2).OLEObjects("ComboBox21").ListFillRange = "yesnoo"
I get the error now that "Can't exit design mode because control ' dattimepicker21' cannot be created
I found and fixed the problem, the machines that were not working were missing MSCOMCT2.OCX
http://answers.microsoft.com/en-us/office/forum/office_2013_release-excel/date-picker-dropdown-in-excel-2013/71decdcd-6cc6-4a70-b1ab-20294a2a315a

Trouble running Excel Add In macro from MS Access 2010 procedure

I'm using VBA code in MS Access to export a query as an Excel workbook. I have a separate procedure that should open the workbook from MS Access and run a macro to format the workbook. The macro is stored in a separate Excel Add-In (.xlam) workbook.
My problem: I'm getting a 1004 can't run macro error, because "the macro can't be found or macros have been disabled." I tried enabling all macros in the Excel Trust Center options and I opened the Add-In workbook separately and re-ran the procedure. I'm getting the same error.
I've searched online for an answer, but I haven't found anyone who has successfully ran a macro stored in a separate Add-In workbook from a MS Access procedure. Anyone care to try and help?
Here's my MS Access procedure that's giving me problems:
Private Sub RunExcelTrackerMacro(strFileName As String)
Dim xl As Object
' Create excel app.
Set xl = CreateObject("Excel.Application")
' Open workbook to be formatted via a macro.
xl.Workbooks.Open (strFileName)
xl.Visible = False
' Run the macro, stored in a separate add-in (.xlam) workbook.
' This procedure fails to run the public Call_FormatTracker macro, which
' is stored in an Add-In in a module called "MacroCalls."
xl.Run "MacroCalls.Call_FormatTracker"
' Save and close the workbook.
xl.ActiveWorkbook.Close (True)
' Close excel app.
xl.Quit
Set xl = Nothing
End Sub
What about changing the xl.Run line to something like xl.Run "Sales.xlam!Orders"
Not sure what your xlam is called, but just change to suit.
I had a similar problem running a macro in an add on. Although the addin was available when I opened Excel directly it was not when Excel was called from Access.
After you create the Excel object add the line below.
Call ReloadXLAddins(xl)
Function ReloadXLAddins(oExcel) ' As Boolean
' Dim CurrAddin As Excel.AddIn
For Each CurrAddin In oExcel.AddIns
If CurrAddin.Installed Then
CurrAddin.Installed = False
CurrAddin.Installed = True
End If
Next CurrAddin
End Function

Vba closing excel applications/workbooks freezes

I have a problem with the following code:
Dim excelapp as object
set excelapp = CreateObject("excel.application")
dim ws as object
dim wb as Workbook
wb= excelapp.Workbooks.Open(path)
ws= wb.Sheets(1)
'in the code i send the worksheet object around by reference in order to read the
'worksheet and manipulate data, i dont create other instances of excel apps or
'workbooks
then i try :
wb.Close
and i have also tried :
excelapp.Quit
Neither have worked, they both freeze and say they are waiting on OLE actions, and i have multiple excel processes opening if i do not call these, when i try to open the excel files i had opened via code, i can only open them as read-only because theyre checked out to me.
I also tried executing a shell script that closes all applications "Excel.Exe" but it closes...the actual excel file where the vba is being executed, so thats not a good solution.
Thank you in advance.
It might be that the Excel app has detected that the workbook has changed and is putting up a dialog box (which is invisible because the app is not visible). Try:
wb.Close False
Which tells Excel to ignore any changes to the workbook.