简体   繁体   中英

Powershell code to insert excel worksheet into excel file

Is it possible to embed object in to excel file?

I tried using "$sheet.OLEObjects().Add() but was not able to get the right parameters to embed the object. I'm wondering if its possible to do it using powershell.

Any help on this matter would be greatly appreciated.

Edit: 21/12/2020

Here is the code I was referring to:

$src = "C:\Temp\Temp\src.xlsm"
$dst="C:\Temp\Temp\dst.xlsx"

$excel=New-Object -ComObject Excel.Application
$excel.visible = $false
$excel.DisplayAlerts=$false
$book = $excel.workbooks.Open($src, $null, $false)

$sheet = $excel.worksheets.item("sheetname")
[void]$sheet.Activate()

$sheet.OLEObjects().Add($missing, $dst, $false, $true,$missing, 1, "Attachment', $missing, $missing, $missing)

[void] $book.save()
[void]$book.Close()
[void]$book.Quit()

The above code gives error "Unable to get the Add property of the OLEObjects class.

Also tried the below which actually attaches an empty sheet to the woorkbook but not the destination file and also gives the same error message as above.

$sheet.OLEObjects().Add('excel.sheet', $missing, $false, $true, $dst, 1, "Attachment', $missing, $missing,$missing)

Got it working with the below two code line additions:

$oleObjects = $sheet.OLEObjects($missing)

$oleObjects.Add($missing,$dst, $false, $true, "WINWORD.EXE", "LabelName", $missing, $missing, $missing)

You are trying to insert an icon into a worksheet, you aren't trying to insert an additional worksheet into a workbook

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM