Macros missing when opening Excel file with PowerShell - excel

The purpose of the PowerShell script I am trying to write is - Open an excel document and run a macro. (The macro is stored in the Personal Workbook file - personal.xlsb and it runs successfully)
The problem -
When the excel file is opened via PowerShell code, it opens fine but when the macro is run by calling Run("NameOfMacro"), I get an error stating macro not found. I try to find the macro in the opened excel file and it does not appear in the macro list. This seems to be because the personal.xlsb file is not loading.
So I close the excel file, open it manually, but the macro is still missing. I then manually open the personal.xlsb file and notice that the macro exists however the macro still does not appear in the excel file or any other excel file I open thereafter.
After a frustrating hour trying to understand why is the personal.xlsb file stopped loading suddenly. I noticed another thing - if I kill the excel.exe process that was started by the PowerShell code (via task manager window) and then open any excel document the macro is available!!
So I have narrowed down the issue to this - The macros are not available because the Personal.xlsb is NOT opened/loaded WHEN I open excel file via PowerShell.
Does anyone have an idea why is this happening and what do I need to do to resolve this?
Code reference
$excel = New-Object -comobject Excel.Application
$FilePath = "D:\DailyReport.xls"
$workbook = $excel.Workbooks.Open($FilePath)
$excel.Visible = $true
$worksheet = $workbook.worksheets.item(1)
$excel.Run("SelectDataFromReport")
Error I get -
Exception calling "Run" with "31" argument(s): "Cannot run the macro 'SelectDataFromReport'. The macro may not be available in this workbook or all macro
s may be disabled."
At line:18 char:11
+ $excel.Run <<<< ("SelectDataFromReport")
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
By the way, I use the 64 bit PowerShell console by default but I also tried running the code using 32 bit console but it didn't make a difference.
In excel I have added the location as a trusted location and i have allowed excel files to be opened by default in edit mode.

you can call the macro within the personal.xlsb like so:
$excel = New-Object -comobject Excel.Application
$wbPersonalXLSB = $excel.workbooks.open("$env:USERPROFILE\Application Data\Microsoft\Excel\XLSTART\PERSONAL.XLSB")
$FilePath = "D:\DailyReport.xls"
$workbook = $excel.Workbooks.Open($FilePath)
$excel.Visible = $true
$worksheet = $workbook.worksheets.item(1)
$excel.Run("PERSONAL.XLSB!SelectDataFromReport")
$wbPersonalXLSB.Close()
$workbook.save()
$workbook.close()
$excel.quit()

$x1 = New-Object -comobject Excel.Application
$wb = $x1.workbooks.open("$env:C:\teste\testamacro.xlsm")
$FilePath = "c:\teste\testamacro"
$workbook = $x1.Workbooks.Open($FilePath)
$x1.Visible = $true
$worksheet = $workbook.worksheets.item(1)
$x1.Run("testamacro.xlsm!SelectDataFromReport")
$wb.Close() $workbook.save()`
$workbook.close()
$x1.quit()

Related

PowerShell how to open multiple instances of Excel

My script is opened an Excel sheet from SPO and after the reading from the file and then I closing it.
The issue is when I run this script from 2 instances of PS (Ctrl + T)
While the first script is running all good but when the second script running I get a pop up message that the file is already opened
This is the script that read from the excel
Connect-PnPOnline -Url $SharePointSiteURL
$ExcelObject = New-Object -ComObject Excel.Application
$ExcelWorkBook = $ExcelObject.Workbooks.Open($SharePointSiteURL)
$ExcelWorkSheet = $ExcelWorkBook.Sheets.Item("VIP List")
QuitExcel
function QuitExcel {
# when done, quit Excel and remove the used COM objects from memory (important)
$ExcelWorkBook.Close()
$ExcelObject.Quit()
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($ExcelWorkSheet)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($ExcelWorkBook)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($ExcelObject)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
Disconnect-PnPOnline
While First script is accessing xls, definitely the second script which is accessing the same xls would cause an issue because it is in use. Alternate is to make a copy of the same xls on runtime and use it.

Powershell Run Excel Macros

hello im trying to open an excel document then open an excel Macro Document, then have powershell run the specific macro that i want and let the macro do its magic and call it a day.
the script i have is this:
# start Excel
$excel = New-Object -comobject Excel.Application
#open file
$FilePath = 'C:\Users\Username\Desktop\ExcelWorkbook.xlsm'
$workbook = $excel.Workbooks.Open($FilePath)
#make it visible (just to check what is happening)
$excel.Visible = $true
#access the Application object and run a macro
$app = $excel.Application
$app.Run("Macro")
$excel.Quit()
#Popup box to show completion - you would remove this if using task scheduler
$wshell = New-Object -ComObject Wscript.Shell $wshell.Popup("Operation Completed",0,"Done",0x1)
So my issue is im getting the error "all macros may be disabled"
what code do i use to make them enabled, i'm having issues with that.
$app it's not defined, so try to replace $app.Run("Macro") with $Excel.Run("Macro")

PowerShell Initiated Excel VBA Macro not doing anything

I have a working VBA Macro with working Data in an Excel. Probably some of you wan't to see that Macro but trust me it works. It worked for multiple weeks and if I open the File PowerShell is going to edit and click on the Button the Macro works as well, so no Problem there.
For the start my PowerShell Script should just execute the macro and save the Excel.
I Found out how you run macros / click a button and how to save the File. Here's my Code:
$ExcelPath = "PathToMyExcel"
$excel = New-Object -comobject Excel.Application
$workbook = $excel.Workbooks.Open($ExcelPath)
$excel.Visible = $true
$app = $excel.Application
$app.Run("Sheet.Macro")
Start-Sleep -Seconds 2 # -> Just so I can see what the Macro "Should" be doing
$excel.Visible = $false
$workbook.Saved = $true
$workbook.SaveAs($ExcelPath)
$xl.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($xl)
[System.GC]::Collect()
My first Problem was that it didn't find and couldn't run the Macro, so I activated macros from the getgo and later found out that I had to include the sheet name my Button / Macro was in (Sheet.Macro).
I tried changing up the $app.Run("") with everything, Sheet, Workbook, Whole Excel Path and others, everyone gives me back an Error that It couldn't find the Macro but not the "Sheet.Macro" which gives back no Errors but, like I already said, doesn't do anything.
So, now I don't have any Errors it should work, but it doesn't. Is it because it's a VBA Macro or something different?
$ExcelPath = "PathToMyExcel"
$excel = New-Object -comobject Excel.Application
$workbook = $excel.Workbooks.Open($ExcelPath)
$excel.Visible = $true
$app = $excel.Application
$app.Run("Sheet1.Macro")
Start-Sleep -Seconds 2 # -> Just so I can see what the Macro "Should" be doing
$excel.Visible = $false
$workbook.Saved = $true
$workbook.SaveAs($ExcelPath)
$excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
[System.GC]::Collect()
Make sure you have the sheet name right.
I am assuming it should be Sheet1 and the macro should be a public sub under `Sheet1'.

How do I prevent Excel from attempting to connect to its links when opening a file with links in Powershell?

I have a PS script that runs from the Task Scheduler. When a .xlsm file is added to a watched folder it opens this file and gathers specific data from it, then outputs it to a CSV. The problem I've been having recently is with XLSM files that have links to an external SharePoint site. Any time these files are opened with the script it just hangs. If I try to open the file manually Excel first asks me to "Enable Content", then once I click to enable a prompt to input my credentials to connect to the SharePoint site associated with the link.
I can confirm after breaking the link and then running the script that the problem is resolved, so it is definitely this link that is hanging the script up. I've tried looking into methods to break the link before opening the file, but I was not able to gather much. All the resources I could find were in references to updating links via Powershell, not breaking them.
Here are the pieces of my code related to opening the file:
$watchedfolder = "C:\Watched"
$filedirectory = Get-ChildItem $watchedfolder | Where-Object {($_.Extension -eq ".xlsm")} | Select-Object -ExpandProperty Name
foreach ($file in $filedirectory){
$sheetName = "Daily Dash"
#OPEN EXCEL WORKBOOK
$objExcel = New-Object -ComObject Excel.Application
$workbook = $objExcel.Workbooks.Open("C:\Watched\$file")
$sheet = $workbook.Worksheets.Item($sheetName)
$objExcel.Visible = $false
$objExcel.DisplayAlerts = $false
$rowMax = ($sheet.UsedRange.Rows).count
I am unsure of what I can add to this opening portion of my script that will prevent the attempt to connect to the SharePoint site. Any recommendations?
I cannot test this at the moment, but you need to do the settings for the Excel object before opening the file.
Setting the AskToUpdateLinks property to $false should do what you ask for.
#OPEN EXCEL WORKBOOK
$objExcel = New-Object -ComObject Excel.Application
$objExcel.Visible = $false
$objExcel.DisplayAlerts = $false
$objExcel.AskToUpdateLinks = $false
$workbook = $objExcel.Workbooks.Open("C:\Watched\$file")
$sheet = $workbook.Worksheets.Item($sheetName)
$rowMax = ($sheet.UsedRange.Rows).count

PowerShell + Excel.Application add sheets problem

I am trying to implement a script to update some Excel sheets/cells.
For this I am using Windows PowerShell ISE with the following code:
# open application
$document = New-Object -ComObject excel.application
$document.Application.Visible = $true
$document.DisplayAlerts = $false
# Create workbook
$workbook = $document.Workbooks.Add()
After this, I can use the following command to see which sheets I have:
$workbook.Sheets | Select-Object -Property Name
And it works perfectly. The problem is when I add a new sheet with the following:
$workbook = $document.Sheets.Add()
It creates the new sheet, but… when I use the command to see the sheet names, it does not show anything, looks like the Sheets.Add() crashes something…
Can someone help me with this topic? Am I doing something wrong?

Resources