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?
Related
I have an API request that set a variable named $data with CSV style data.
I want to open a Workbook with that data without writing on disk.
How can I do that?
The best I can do is
$excel = New-Object -ComObject excel.application
$workbook = $excel.Workbooks.Add()
$workbook.worksheets(1).Cells(1,1) = $csv
Unfortunately, it fills my CSV into 1 cell, adding delimiter, instead of simply "copying" data into the workbook.
This is a fairly simple task if you want to use the ComObject, as you can use ConvertTo-Csv with a tab delimiter, copy to the clipboard, and paste into Excel. Here's a snippet of what I've used in a script myself to do just that:
$tasks|ConvertTo-Csv -del "`t" -NoTypeInformation|clip
$XL = New-Object -ComObject Excel.Application
$XL.Visible = $true
$WB = $XL.Workbooks.Add()
$XL.ActiveCell.PasteSpecial() | Out-Null
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 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'.
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
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