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'.
Related
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")
I am trying to add a button to an Excel worksheet in PowerShell as part of a bigger already coded and working script.
The button is not part of the Excel.Application COM object instead it is in Microsoft.Office.Tools.Excel namespace.
I've created the following code but it gives a TypeNot found error on the last line when run.
$excel = New-Object -ComObject "Excel.Application"
$excel.visible = $True
$workbook = $excel.Workbooks.Add()
$worksheet = $workbook.worksheets.item(1)
$Range1 = $worksheet.Range("A1", "A1")
[Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Tools.Excel")
$Btn = [Microsoft.Office.Tools.Excel]::AddButton( $worksheet, $Range1, "Test")
What am I doing wrong?
Thanks,
Dog
I have my code for opening and gathering information from any sheet within the workbook, basically what it does it open the workbook, ask what sheet you want to pull info from, and it processes it. It then quits powershell and leaves excel open.
The only problem I have is that I would like powershell to actually show the sheet it is pulling from in the excel window. For instance if I choose sheet 3 to process info from, excel will by default show the last sheet I had selected and leave it, I would like it to go to a specific one. Is there a way to do that?
Yes, it's the Activate() method.
$Excel = new-object -ComObject Excel.Application
$Workbook = $Excel.Workbooks.Add()
[void]$Workbook.Worksheets.Add()
[void]$Workbook.Worksheets.Add()
$Workbook.Worksheets.Item(2).Activate()
$Excel.Visible = $true
That will open Excel, create a workbook, add two sheets, and then display the second sheet.
$excel = New-Object -ComObject excel.application
$excel.Visible = $true
$workbook = $excel.workbooks.open('D:\Projects\working\data.xlsm')
$sheet = $workbook.Worksheets.Item('Graph')
$sheet.activate()
$rangeSource=$sheet.range("A50","M73")
$rangeSource.Copy() | out-null
$Results = Get-Clipboard -TextFormatType Html | select -skip 7 | Out-String
I need to run a script that just opens an excel file, calculates an excel cell connected with a Pi DataLink, then tells me the value.
If I try to do that in the way that's standard:
$objExcel = New-Object -com Excel.Application
$objExcel.Visible = $True
$WorkBook = $objExcel.Workbooks.Open("C:\Users\crclayton\sheet.xlsx")
$WorkSheet = $WorkBook.Sheets.Item("Sheet1")
write-host $worksheet.Range("A1").Text
$WorkBook.Save()
$WorkBook.Close()
$objExcel.Quit()
I get a #NAME? error. And even if I just use the first three lines to just open an excel file and look at it, I can't run calculations, =PICurrVal("TAGNAME",0,"SERVERNAME") is just a dead formula that excel doesn't understand if I open it this way. I've also tried to UpdateLinks when I open the file, but no dice.
However, if I open the file like so:
Invoke-Item "C:\Users\crclayton\sheet.xlsx"
I don't get a #NAME? error and I can run the calculations and excel understands this formula.
Maybe something like this?
Invoke-Item "C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.EXE"
Start-Sleep 10
$objExcel = Get-Process "EXCEL.EXE"
$WorkBook = $objExcel.Workbooks.Open("C:\Users\crclayton\sheet.xlsx")
$WorkSheet = $WorkBook.Sheets.Item("Sheet1")
write-host $worksheet.Range("A1").Text
Is there some way to get the value in cell A1 having opened the spreadsheet using Invoke-Item?
I'm not sure why you're getting #NAME? as Excel should be doing all the calculations within the sheet all we're doing in Powershell is getting the value of the cell.
However what you can try is outputting the value of your formula to a nearby cell and getting the value of it instead, for example:
Your formula is in D18 -> =PICurrVal("TAGNAME",0,"SERVERNAME")
Your value is in D19 -> =D18
Call the value in your Powershell:
$objExcel = New-Object -com Excel.Application
$objExcel.Visible = $True
$WorkBook = $objExcel.Workbooks.Open("C:\Users\crclayton\sheet.xlsx")
$WorkSheet = $WorkBook.Sheets.Item(1)
write-host $worksheet.Range("D18").Text
$WorkBook.Save()
$WorkBook.Close()
$objExcel.Quit()
Update
Excel addins can be added in powershell by using the Addins property like so:
$MyAddin = $Workbook.AddIns.Add('C:\test.xla', $True)
$MyAddin.Installed = "True"
Your new complete code might look something like
$objExcel = New-Object -com Excel.Application
$objExcel.Visible = $True
$WorkBook = $objExcel.Workbooks.Open("C:\Users\crclayton\sheet.xlsx")
$MyAddin = $Workbook.AddIns.Add('C:\test.xla', $True)
$MyAddin.Installed = "True"
$WorkSheet = $WorkBook.Sheets.Item(1)
write-host $worksheet.Range("D18").Text
$WorkBook.Save()
$WorkBook.Close()
$objExcel.Quit()
Edit 2:
Yes, add-ins were the problem. I needed to add each the following files:
$ExcelAddin = $WorkBook.Application.AddIns.Add("C:\Program Files (x86)\PIPC\Excel\PITrendXL.xla", $True)
$ExcelAddin.Installed = "True"
$ExcelAddin = $WorkBook.Application.AddIns.Add("C:\Program Files (x86)\PIPC\Excel\pipc32.xll", $True)
$ExcelAddin.Installed = "True"
$ExcelAddin = $WorkBook.Application.AddIns.Add("C:\Program Files (x86)\PIPC\Excel\OSIsoft.PIDataLink.UI.dll.manifest", $True)
$ExcelAddin.Installed = "True"
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()