Hello,
I have an MS Excel file inserted as object and displayed as icon between paragraphs of word document. What I want to achieve, is to use GoTo->Object function in PowerShell to navigate to MS Excel object and open it via $xl = new-object -comobject Excel.Application for further modifying. I found a very few tutorials, examples about working with MS Word objects in PowerShell. Could you please forward me to the resources that might help me to better understand how to work with MS Word objects, or help me by editing my script.
Script:
function Remove-ComObject { [CmdletBinding()] param() end { Start-Sleep -Milliseconds 500 [Management.Automation.ScopedItemOptions]$scopedOpt = 'ReadOnly, Constant' Get-Variable -Scope 1 | Where-Object { $_.Value.pstypenames -contains 'System.__ComObject' -and -not ($scopedOpt -band $_.Options) } | Remove-Variable -Scope 1 -Verbose:([Bool]$PSBoundParameters['Verbose'].IsPresent) [gc]::Collect() } } Function Using-Culture ([System.Globalization.CultureInfo]$culture, [ScriptBlock]$script) { ## this is required for runing the script where current machine`s locale is not English-Us $OldCulture = [System.Threading.Thread]::CurrentThread.CurrentCulture trap { [System.Threading.Thread]::CurrentThread.CurrentCulture = $OldCulture } [System.Threading.Thread]::CurrentThread.CurrentCulture = $culture $ExecutionContext.InvokeCommand.InvokeScript($script) [System.Threading.Thread]::CurrentThread.CurrentCulture = $OldCulture } Using-Culture en-us { $scriptBasePath = Get-Location $wrd = new-object -comobject Word.Application $wrd.visible = $true $wrd.DisplayAlerts = $False $path = "C:\users\lt0arzg\desktop\word" $AryProperties = "Title","Author","Keywords", "Number of words", "Number of pages" $binding = "System.Reflection.BindingFlags" -as [type] [ref]$SaveOption = "microsoft.office.interop.word.WdSaveOptions" -as [type] $docs = Get-childitem -path $Path -Recurse $docs = $docs | where { (($_.extension -eq ".doc") -or ($_.extension -eq ".docx"))} Foreach($doc in $docs) { try{ $document = $wrd.documents.open($path + "\" + $doc.name) $selection = $wrd.Selection
$selection.goto(9,1,1,"Microsoft Office Excel 97-2003 Worksheet") | get-member<#$xl = new-object -comobject Excel.Application pass $object to excel for further modification#> #$document.close() [System.Runtime.InteropServices.Marshal]::ReleaseComObject($document) | Out-Null Remove-Variable -Name document } catch [system.exception] {Write-Host $_.Exception} } #$wrd.quit() #Remove-ComObject -Verbose }
For now script moves pointer to MS Excel object.
Any help appreciated.