AppellatePerson
New Member
- Joined
- May 15, 2024
- Messages
- 8
- Office Version
- 365
- Platform
- Windows
I am trying to put data from one row in an Excel file into a bunch of different Word templates at bookmarked locations. Each row in the spreadsheet pertains to an employee. Basically, I have over 30 document templates (for different purposes) that need to be filled out for about 20 different people. Then, I need to save those filled out documents as Word templates themselves. Each employee needs a personalized version of the template but it has to stay a template so that they can use it repeatedly to create new copies of those documents to edit for a particular customer.
Here is a dummy version of the type of data the Excel file will be putting into the templates at various locations:
Mail merge is not the answer because I need to apply the information to thirty different template documents. Doing a mail merge creates one document containing the version for each employee. It is too cumbersome to do the mail merge and then separate that one output document into 20 different documents. And then repeat that whole process 29 more times for all the different templates.
I found an answer on Quora about using bookmarks in the Word template and a VBA module applying data from an Excel file. That answer from Danielle O'Connell is available here but here is the code:
This code worked like a charm for one of the Word templates but I need help to make it do what I need for all the documents.
The remaining problems I need to solve are how to get the VBA module to:
Here is a dummy version of the type of data the Excel file will be putting into the templates at various locations:
For MrExcel Post.xlsm | |||||||
---|---|---|---|---|---|---|---|
A | B | C | D | E | |||
1 | Employee Name | Employee Number | Title | Initials | |||
2 | John Smith | 123435 | test@aol.com | Employee1 | JS | ||
3 | Donald Duck | 2346534 | test2@aol.com | Employee2 | DD | ||
4 | Tom Cruise | 4687876 | test3@aol.com | Employee3 | TC | ||
Names_Info |
Mail merge is not the answer because I need to apply the information to thirty different template documents. Doing a mail merge creates one document containing the version for each employee. It is too cumbersome to do the mail merge and then separate that one output document into 20 different documents. And then repeat that whole process 29 more times for all the different templates.
I found an answer on Quora about using bookmarks in the Word template and a VBA module applying data from an Excel file. That answer from Danielle O'Connell is available here but here is the code:
VBA Code:
Sub SendToMemo()
'Creates a new memo using the details of the selected row.
Dim wd As Object
Dim wdDoc As Object
Dim r As Integer
Dim h As Integer
Dim lCol As Integer
Dim strV As String
Dim strH As String
Dim strBM As String
Dim wdBM As Object
Dim i As Integer
'Create Word Document & Set Object References
Set wd = CreateObject("Word.Application")
Set wdDoc = wd.Documents.Add("C:\Users\danie\Documents\test\Test Template.dotx")
wd.Visible = True
'Loop through each column and retreive heading text and cell text (strH = Heading Text, strV = cell Text)
r = ActiveCell.row
lCol = ActiveSheet.Cells(1, ActiveSheet.Columns.Count).End(xlToLeft).Column
h = 1
While h <= lCol
strH = Replace(Replace(Replace(Replace(ActiveSheet.Cells(1, h).Value, " ", "_"), ",", ""), "-", ""), ".", "")
strV = ActiveSheet.Cells(r, h).Text
strBM = strH
i = 1
'Test if Bookmark named with column heading exists
lbTestBM:
If wdDoc.Bookmarks.Exists(strBM) Then
wdDoc.Bookmarks(strBM).Range.Text = strV
'Test if multiple bookmarks with same name exist using column heading & iterating integer
i = i + 1
strBM = strH & "_" & i
GoTo lbTestBM
End If
h = h + 1
Wend
End Sub
This code worked like a charm for one of the Word templates but I need help to make it do what I need for all the documents.
The remaining problems I need to solve are how to get the VBA module to:
- apply the Excel data to more than one template file (about 30 files). One option I considered is putting all the templates into a single folder and then looping through every file in that folder.
- Have the output files be Word templates ( .dotm) files.
- "Save as" the new Word documents. Otherwise, I have to save 600 documents manually.
- Name the files with a descriptive name. Ideally, the name would be the employee's initials + the template title. I think this could be done by referring to the "Initials" column in the Excel file.
- Not a huge deal but it is probably best to have the files not open automatically when the VBA module creates them.