[powershell] Pasting table from Excel to Word, autofit table

Pater

New Member
Joined
Jan 13, 2021
Messages
1
Office Version
  1. 2016
Platform
  1. Windows
Currently working on a robot where I want to download an excel file from a system, copy paste the contents of the excel file to a word file as an embedded excel table, but the table is wider than the page (it is already in landscape mode). I am using a powershell script in DAS to execute this step. To solve the problem normally, I would simply right-click on the table and use Autofit -> Fit to Window. How do I do this in powershell?

My script is currently like this:

$destination = 'C:\Users\Pater\Downloads\archive2.docx' $word = new-object -comobject Word.application $word.visible = $true $doc1 = $word.documents.open($destination) $bookmark1 = $doc1.Bookmarks.Item("FacilitySheet") $xl = New-Object -comobject Excel.Application $xl.Visible = $true $xl.DisplayAlerts = $False $wb = $xl.Workbooks.Open("C:\Users\Pater\Downloads\spreadsheet.xlsx") $ws = $wb.ActiveSheet $Range1 = $ws.UsedRange.Cells $RowCount = $Range1.rows.count $CopyRange = $ws.Range("A1:O$RowCount").Copy() $bookmark1.Range.Paste()
 

Some videos you may like

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.

Watch MrExcel Video

Forum statistics

Threads
1,127,711
Messages
5,626,422
Members
416,183
Latest member
IanA

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top