This problem has been driving me crazy and can't find a solution anywhere. I have a super simple strip of code in powershell that opens and excel and then runs a macro (named Refresh). The macro is saved to the workbook. The first thing in the macro is to run RefreshAllWorkbooks. As soon as powershell hits that line it errors out: run-time error 1004 Cannot run the macro 'Refreshallworkboooks' the macro may not be available or all macros may be disabled.
When I manually open excel and run that same macro, I get no errors. What can I do to fix this?
I appreciate any help!
$excel = New-Object -comobject Excel.Application
# open file
$FilePath = 'C:\Users\ap1\OneDrive\DailyPerf.xlsm'
$workbook = $excel.Workbooks.Open($FilePath)
#make it visible
$excel.Visible = $true
#access the Application object and run a macro
$app = $excel.Application
$app.Run('Refresh')
Related
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!
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
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
I have a program in an Excel workbook that opens a Powerpoint-File, updates the links within this file and closes it after that. This works fine. Here is my problem: When the links are updated an Excel file with the source data is opened. After Powerpoint is closed this file stays open. I want it to get closed because I repeat this process for many files and I can't end up with hundreds of open Excel files.
I tried the following:
WBKs=Application.Workbooks.count
For i = WBKs to 1 Step -1
If Workbooks(i).Name<>ThisWorkbook.Name then
Workbooks(i).close savechanges:=False
End if
Next i
Now comes the weird part. Whenever I just run my code, WBKs always returns 1 and the Excel file only pops up after the code is finished. If I go through my code in debug mode it works. The workbook pops up as soon as I enter debug mode.
I tried Applicatio.Wait in the hope that the file would show after a second. The file only showed after the code was finished.
I tried a Do While Loop to wait until the file is open. Excel crashes because I never leave the loop.
Edit: I tried DoEvents as suggested. Does not work either.
This is just a workaround, but try using a brute force after x times your macro has run. Store that x somewhere in workbook, save. And kill excel process (all instances, including self) :
Dim sKill As String
sKill = "TASKKILL /F /IM excel.exe"
Shell sKill, vbHide
Found here : VBA script to close every instance of Excel except itself
When running your macro next time, you will use that x as a starting point for next PPT file to update.
I created an an Excel macro that works correctly however when I add this code to my macro in PERSONAL.XLS from the start directory of MS Excel (in a bad place in the macro), I can't open the macro and I can't find it..
How can I open the macro to delete this bad code!!!
Dim sKill As String
sKill ="TASKKILL /f /IM EXCEL.EXE"
....
shell sKill , vbHide
... Why not just remove your personal.xls from that directory and start over??
OR Remove it, rename it as a.xls or anything else and then open it.
OR set your Tools > macro options so that the macros don't run.