$xl = New-Object -ComObject Excel.Application -Property #{
Visible = $true
DisplayAlerts = $false
}
$wb = $xl.WorkBooks.Add()
$sh = $wb.Sheets.Item(‘Sheet1’)
$sh.Shapes.AddPicture("c:\xyz.jpg",1,0,0,0,400,200)
Using this code I am able to insert picture in excel sheet but it always puts the picture at "A1" position.
Is there any way to insert picture at selected row,column location using PowerShell?
A picture isn't in any cell. It's in a different layer on top of the cells.
You are telling it to be at 0,0 with your code. If you do this:
$sh.Shapes.AddPicture("c:\xyz.jpg",1,0,300,300,400,200)
you will insert it a 300, 300.
Related
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()
I've been diving into how Powershell can use Excel as a COM object, have most of it down but there are two things I'd like to be able to do that I haven't been able to find anywhere, hoping someone can help.
1/ Would like to be able to script hiding a range of columns in the generated Excel spreadsheet.
2/ Would like to be able to have Excel add a border around all cells in the script as well.
Thanks!
Hiding a column:
Here is an example that you can adapt. This is hiding the first column in the active work sheet.
$file = "C:\Users\Micky\Desktop\not locked.xlsx"
[Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Interop.Excel")|Out-Null
$excel = New-Object Microsoft.Office.Interop.Excel.ApplicationClass
$excel.Visible = $true
$wb = $excel.Workbooks.Open($file)
$ws = $wb.ActiveSheet
$c = $ws.Columns
$c.Item(1).hidden = $true
Cell border:
For the example I use a double border and apply to the first cell, A1.
The XlLineStyle Enum can be found here
$xlDouble = -4119
$item = $ws.Range("A1")
$item.Borders.LineStyle = $xlDouble
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 want to add a column after a particular column number in excel sheet using Powershell.
I am able to add it at starting of sheet, but couldn't insert after a specific column.
#This will insert a column at column R
$Excel = New-Object -ComObject excel.application
$ExcelWorkSheet = $ExcelWordBook.Worksheets.Add()
$ExcelWorkSheet.Name = "TestThis"
#do other things
$ColumnSelect = $ExcelWorkSheet.Columns("R:R")
$ColumnSelect.Insert()
Alas, I agree, I have not found neither documentation or examples :-/ .
Nevertheless here is below how to insert a column 7th and give it a name:
(Get-ChildItem "*.xlsb")|
foreach-object {
$xl=New-Object -ComObject Excel.Application
$wb=$xl.workbooks.open($_)
$ws = $wb.worksheets.Item(1)
$ws.Columns.ListObject.ListColumns.Add(7)
$ws.Cells.Item(1,7) ='Comment'
$wb.Save()
$xl.Quit()
while([System.Runtime.Interopservices.Marshal]::ReleaseComObject([System.__ComObject]$xl)){'released'| Out-Null}
}
Best regards
As the title suggested, I'm not sure how to do the equivalent of selecting only a portion of a row and insert cells (shift rows down) using Powershell. It seems like all tutorials online are about inserting the entire row or entire column which is not what I want to do.
Any ideas?
Thanks.
Try something like this:
$xl = New-Object -COM "Excel.Application"
$xl.Visible = $true
$wb = $xl.Workbooks.Open "C:\path\to\your.xlsx"
$ws = $wb.Sheets.Item(1)
$ws.Range("C5:E9").Insert(-4121)