How to zoom the excel window? - excel

I am doing a powershell script that creates a new excel file and I would like that when the file opens, the zoom of the page is at 85%.
Someone has an idea of ​​how?
$NewExcel = New-Object -ComObject excel.application
$NewExcel.visible = $True
$NewExcel.WindowState = "xlMaximized"
$NewWorkbook = $NewExcel.Workbooks.Add()
$NewWorksheet= $NewWorkbook.Worksheets.Item(1)
This is my script to create the worksheet

I found !
Function Set-XlColumns {
$Window = $NewExcel.ActiveWindow
$Window.Zoom = 80
}
Set-XlColumns
Just use this function and call it later

Related

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")

Format Chart DataLabel in Powershell generated Excel File

Try to format datalabel font size with a powershell generated barchart, but does not work
Read the "whole" API for Chart.SeriesCollection for VBA and .NET. But it does not help. Is it a bug or have I a brain bug? Anyone who can help?
https://learn.microsoft.com/de-de/office/vba/api/excel.chart.seriescollection
My try (with different iterations about this)
$chart.SeriesCollection(1).DataLabels.Format.TextFrame2.TextRange2.Font.Size = 18
Powershell Error Message: The property 'Size' cannot be found on this object. Verify that the property exists and can be set.
The whole short powershell script:
$excel = New-Object -comobject Excel.Application
$excel.Visible = $True
$workbook = $excel.Workbooks.Add()
$sheet = $excel.Worksheets.Item(1)
$sheet.Activate() | Out-NULL
$sheet.Cells.Item(1,1).Value2 = "City"
$sheet.Cells.Item(1,2).Value2 = "Citizens"
$sheet.Cells.Item(2,1) = "Offenbach"
$sheet.Cells.Item(2,2) = 111020
$sheet.Cells.Item(3,1) = "Heusenstamm"
$sheet.Cells.Item(3,2) = 18200
$sheet.Cells.Item(4,1) = "Rembruecken"
$sheet.Cells.Item(4,2) = 1850
$range = "A1:B4"
$chartSelect = $sheet.range($range)
$ch = $sheet.shapes.addChart().chart
$ch.chartType = 51
$ch.ApplyDataLabels(2)
$ch.SeriesCollection(1).DataLabels.Format.TextFrame2.TextRange2.Font.Size = 18
$ch.setSourceData($chartSelect)
Try this ?
1..3| %{$ch.SeriesCollection(1).DataLabels($_).Font.Size = 18}
There are a couple syntaxes that ought to work.
The old one which you need to select 'Show hidden members' in the VBIDE's Object Browser to see (as if it's been deprecated, but it works fine):
ActiveChart.SeriesCollection(2).DataLabels.Font.Size = 18
The new and improved and way more complex one:
ActiveChart.SeriesCollection(2).DataLabels.Format.TextFrame2.TextRange.Font.Size = 18
I'll let you convert from VBA to PowerShell.

Powershell select entire row with already opened excel worksheet

I'm writing a little GUI to ease working on some excel documents. It has a button that starts this function to open excel file and select required row.
Function open_bible_file
{
$Excel = New-Object -ComObject excel.application
$Excel.WindowState= "xlMaximized"
$Excel.visible = $true
$WorkBook = $Excel.Workbooks.Open($SCOMBibleFile)
$Worksheet = $Workbook.WorkSheets.item("(1) Alerts")
$worksheet.activate()
$Range = $Worksheet.Cells.Item($excelrow,1).EntireRow
[void]$Range.Select()
}
}
It opens the file and selects the row as it should. But when I use this button again it just opens excel one more time and again selects another row. When I've tried to do another button to just select rows It does not know anything about already opened worksheets. How can I get around it?
The code should check if Excel is already running and if so, if the workbook (file $SCOMBibleFile) is present. If that is the case, re-activate Excel, otherwise start a new instance.
This should work:
function open_bible_file {
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true, Position = 0)]
[string]$Path,
[Parameter(Mandatory = $false, Position = 1)]
[int]$RowToSelect = 1
)
$WorkBook = $null
# check if Excel is already open
try {
# Note: this only gets the excel instances that were started
# by the same user that runs this powershell function.
$Excel = [Runtime.Interopservices.Marshal]::GetActiveObject('Excel.Application')
# test if the $Path workbook is present in this Excel instance
foreach ($wb in $Excel.Workbooks) {
if ($wb.FullName -match [regex]::Escape($Path)) {
$WorkBook = $wb
break
}
}
}
catch {
# Excel wasn't opened yet, create a new instance
$Excel = New-Object -ComObject Excel.Application
}
if (!($Excel)) { Write-Error "Error opening Excel"; return }
if (!($WorkBook)) {
$WorkBook = $Excel.Workbooks.Open($Path)
}
# see https://learn.microsoft.com/en-us/office/vba/api/excel.xlwindowstate
$xlMaximized = -4137
$Excel.Visible = $true
$Excel.WindowState = $xlMaximized
$Excel.ActiveWindow.Activate()
$Worksheet = $Workbook.WorkSheets.item("(1) Alerts")
$worksheet.activate()
$Range = $Worksheet.Cells.Item($RowToSelect, 1).EntireRow
[void]$Range.Select()
}
$SCOMBibleFile = '<PATH TO YOUR .xlsx FILE>'
open_bible_file -Path $SCOMBibleFile -RowToSelect 3
As you can see, I have changed the open_bible_file function to take parameters. The first (-Path) is where you give it the filename to open. The second (-RowToSelect) is the row number you want selected.
Hope this helps

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?

Copying graph from Excel to Word with Powershell

I am trying to copy a graph from excel to word. The source file in excel has two sheets, 'data' & 'graph' on the 'graph' sheet there are 4 graphs, arranged 2x2.
The Word document is empty.
I am fairly new to powershell, but i want to automate a weekly report i have to make. This might seem a little steep, but i like a challenge. Plus, the report eats my time.
This is my code:
$xl = new-object -comobject excel.application
$xl.Visible = $true
$wb = $xl.workbooks.open("H:\Reporting\ULTRAgraphTest.xlsx")
$ws = $wb.worksheets.item(1)
$charts = $ws.ChartObjects()
$chart = $charts.Item(1)
$a = $chart.copy
$wd = new-object -comobject Word.application
$wd.visible = $true
$path = "H:\Reporting\insertest.docx"
$doc = $wd.documents.open($path)
$wd.selection.Paste()
When i run this, the files get opened, but it pastes the clipboard content in the word document. It doesn't seem to copy the graph. What am i not seeing here?
Forgot the partheses
$a = $chart.copy
Should be
$a = $chart.copy()

Resources