PDA

View Full Version : Use Bookmark To Fill in Word Document From Excel Data



MH123
10-15-2016, 01:46 AM
Hi everyone ,
I have an excel file that contains my data , and a word form .
I want to fill The word form with my excel data . (I've bookmarked texts that needs to be fill out )

for example i want to choose row 2-10 column A-I in excel and and click on a button to export 9 word document that filled out with excel data and saved in my Destination folder .

mana
10-15-2016, 02:40 AM
Option Explicit

Sub test()
Dim wd As Object
Dim doc As Object
Const myDoc = "D:\***\***\test.docx"

Set wd = CreateObject("Word.application")
wd.Visible = True
Set doc = wd.Documents.Open(myDoc)

With Selection.EntireRow
doc.Bookmarks("BM1").Range.Text = .Range("b1").Value
doc.Bookmarks("BM2").Range.Text = .Range("c1").Value

End With

doc.SaveAs ThisWorkbook.Path & "\" & Format(Date, "yyyymmdd") & ".docx"
doc.Close False
wd.Quit

Set doc = Nothing
Set wd = Nothing

End Sub

snb
10-15-2016, 03:58 AM
Using Dcoument variables instead of bookmarks would be the clever path to follow.

MH123
10-15-2016, 05:11 AM
Option Explicit

Sub test()
Dim wd As Object
Dim doc As Object
Const myDoc = "D:\***\***\test.docx"

Set wd = CreateObject("Word.application")
wd.Visible = True
Set doc = wd.Documents.Open(myDoc)

With Selection.EntireRow
doc.Bookmarks("BM1").Range.Text = .Range("b1").Value
doc.Bookmarks("BM2").Range.Text = .Range("c1").Value

End With

doc.SaveAs ThisWorkbook.Path & "\" & Format(Date, "yyyymmdd") & ".docx"
doc.Close False
wd.Quit

Set doc = Nothing
Set wd = Nothing

End Sub



Many Thanks mana

But This code Export somtimes one data to word . i want to choose Custom Rows for export to word .

For example i had 20 Row's of data yesterday (from A2-A21) i want to select all and fill out word documents, today i have to add 50 more in my excel (A22-A72) i want only today's word Document . for tomorrow maybe i only need 1 Row to export to word .



in similar way i want to say : From selected Row to xldown do this -> fill word template and save them

MH123
10-15-2016, 05:12 AM
Using Dcoument variables instead of bookmarks would be the clever path to follow.
thank you snb . could you please explain more ?

snb
10-15-2016, 07:41 AM
Please do not Quote !!

If you post your files I can show you.

MH123
10-15-2016, 07:47 AM
Sure i will post them .

MH123
10-15-2016, 09:00 AM
This is a sample files .

snb
10-16-2016, 04:49 AM
Open each file.
Run the macro in the Excel file