Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to paste excel data from clipboard into word at bookmark point

540 views
Skip to first unread message

Johan

unread,
Apr 4, 2011, 12:55:04 PM4/4/11
to
Hello,

I'm trying to automate a report I'm required to run on a weekly
basis. the report collects a bunch of data and generates a formatted
excel document out of which I have to copy ranges (including
formatting) into a word document.

However I'm stuck at pasting my excel data.

This is what I have so far:
----------------------------------------------------
$ScriptFolder = "C:\ps\VC"
$xlsxTemplate = "Template.xlsx"
$docxTemplate = "Template.docx"

#open Excel and template
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$workbook = $excel.Workbooks.open("$ScriptFolder\$xlsxTemplate")

#Open Word and template
$msWord = New-Object -Com Word.Application
$msWord.visible = $true
$wordDoc = $msWord.Documents.Open("$ScriptFolder\$docxTemplate")
$wordDoc.Activate()

#select sheet 1 and copy cell range A1 through D5
$sheet1 = $workbook.worksheets.item(1)
$sheet1.Activate()

$range = $sheet1.Range("A1:D5")
$range.Copy() | out-null

#At this point excel shows the marching ants around the range and I
can
#manually paste this into a word document

?????
not sure what to code here
?????

# I can successfully place text at the bookmark location in the word
document with this:
$objRange = $wordDoc.Bookmarks.Item("Bookmark1").Range
$objRange.Text = "test text"
$wordDoc.Bookmarks.Add("Bookmark1",$objRange)
----------------------------------------------------

I have tried get-clipboard, but this pastes too much information or
only the text into my word document. Is there perhaps a way to
instruct word to just paste the current contents of the clipboard?

Thanks in advance,
Johan


Jonathan G

unread,
Apr 5, 2011, 3:27:50 PM4/5/11
to

I am fairly new with powershell however I believe the following should
work... change this line:

$objRange.Text = "test text"

to something like this...

$objRange.Text = $range.Paste() | out-null

0 new messages