-
Inserting data from excel to word files?
I am up with a task to read data from excel table and create a bills as word files for every person (every row). I have managed to type this much code thus far ...
[VBA]Option Explicit
Public Sub CreateBills()
Dim rng As Range 'Excel table
Dim n As Integer 'Number of rows
Dim wrdApp As New Word.Application 'Word app
Dim wrdDoc As Word.Document 'Word doc
Dim myWordFile As String 'File path
Dim i As Integer 'Iterator
Application.StatusBar = "Reading data from excel..."
Set rng = Range("alg").CurrentRegion
n = rng.Rows.Count
''.DOCX CREATION
Application.StatusBar = "Creating word files..."
Set wrdApp = New Word.Application
For i = 2 To 2 'TEMP
Set wrdDoc = wrdApp.Documents.Add(ThisWorkbook.Path & "\BillTemplate.dotx")
rng.Cells(i, 2).Copy
wrdDoc.Paragraphs(wrdDoc.Paragraphs.Count).Range.PasteAndFormat (wdSingleCellText) 'TEMP
wrdDoc.Paragraphs(wrdDoc.Paragraphs.Count).Range.InsertParagraphAfter 'TEMP
myWordFile = ThisWorkbook.Path & "\" & rng.Cells(i, 2).Value & " " & rng.Cells(i, 5) & ".docx"
wrdApp.ActiveDocument.SaveAs2 Filename:=myWordFile
Next
Application.StatusBar = "Clearing..."
wrdApp.Quit
Set wrdDoc = Nothing
Set wrdApp = Nothing
Application.StatusBar = "Done"
End Sub[/VBA]
I'm stuck at the part where I need to start inserting data into specific regions in my word template. How can this process be most optimally done? Can I somehow name specific regions in my word template and insert data into named regions? Any help will be appreciated.
Best regards,
-Vent
Last edited by Ventilaator; 08-11-2011 at 10:11 AM.
Reason: Misleading title
-
Welcome to the forum!
Your project is usually done from the MSWord side using Mail Merge.
This thread showed how one can do one record at a time. Links to other methods are included. It would not be hard to iterate through all of the records if you needed a saved file for each rather than all in one file as a mail merge does.
-
Thank you for the reply, but I still need to learn how to do it from the Excel side as I have already built in a lot of functions/user forms to this Excel database.
I tried modifying the Word template file by adding Quick parts -> Fields, but there didn't seem to be anything that could be of use. All I need to do is to add data to specific paragraphs and tabel cells in the template file.
-
I guess that one could do a mail merge from Excel. I did not detail or link an example for that method though. It is certainly one of the most powerful methods.
The bookmark method that I detailed is an easy method. Of course bookmarks are added by Ctrl+Shift+F5 in MSWord. In MSWord do File > Options > Advanced > Show Bookmarks > OK, if you need to see bookmarks. I block select text and create the bookmark. The block selection is replaced with data in the macro run.
-
I will try the bookmark solution.
Thank you!
-Vent
-
I can't seem to edit my last post anymore ....
Anyway, using bookmarks is working great - an easy and convenient way to enter Excel data to specific regions of Word doc.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules