
Originally Posted by
hyn1999
Hi
I'm new to VBA, trying to use VBA to populate excel range data to MS Word bookmark.
I am using Excel 2007 have following field
Column A Column B
Cash xxxxx
Liabilities xxxxx
RE xxxx
What I wanted to do it to populate data at Column B of Excel to MS Word Template below
Cash xxxxxx
Liabilities xxxxxx
RE xxxxx
I've tried using below code sample code but it does not work. May I know what's wrong
Need your advise
Sub testing()
On Error GoTo errorHandler
Dim wd As Word.Application
Dim myDoc As Word.Document
Dim mywdRange As Word.Range
Dim Cash As Excel.Range
Dim liabilities As Excel.Range
Dim RE As Excel.Range
Range("C2:C4").Select
Set PersonRange = Range( _
ActiveCell, _
ActiveCell.End(xlDown))
Set wd = New Word.Application
FilePath = ThisWorkbook.Path
With wd
.Visible = True
.WindowState = wdWindowStateMaximize
With wd.Documents.Open("\\dbg.ads.db.com\KL-Users-U\VF10_USERS01\chikarc\data\sample test.docx")
End With
'set excel range
Set Cash = Sheets("Sheet1").Range("C2")
Set liabilities = Sheets("Sheet1").Range("C3")
Set RE = Sheets("Sheet1").Range("C4")
With myDoc.Bookmarks
Set mywdRange = wdDoc.Goto(what:=wdGoToBookmark, Name:="cash")
mywdRange.Text = Cash
Set mywdRange = wdDoc.Goto(what:=wdGoToBookmark, Name:="liabilities")
mywdRange.Text = liabilities
Set mywdRange = wdDoc.Goto(what:=wdGoToBookmark, Name:="RE")
mywdRange.Text = RE
.Item("Cash").Range.Paste
.Item("liabilities").Range.Paste
.Item("RE").Range.Paste
End With
wd.Selection.Goto what:=wdGoToBookmark, Name:="CASH"
ThisWorkbook.Sheets("Sheet1").Range("C2").Copy
wd.Selection.Paste
wd.Selection.Tables(1).Rows.Alignment = wdAlignRowLeft
errorHandler:
Set wd = Nothing
Set myDoc = Nothing
Set mywdRange = Nothing
End With
End Sub
Bookmarks