Create a word doc from data in excel one doc per row / record

ideasfactory

New Member
Joined
Aug 22, 2013
Messages
38
Hi,

I want Excel to create a word doc based on a .dot that is populated with data from excel, most importantly I need one word doc per row or record from excel.

I have 4 columns in excel:

ID
Title
Name
Address
FY1718-824
Initiative Name1
Lisa
Address1
FY1718-627
Initiative Name2
Craig
Address2
FY1617-345
Initiative Name3
Ben
Address3
FY1617-450
Initiative Name4
Elizabeth
Address4

<tbody>
</tbody>

I have a word .dot that has areas throughout the document where I want to populate the 4 columns of data above. For example on page 1 I want ID in a specific location, on page 2 I want Title and Name and on Page 3 I want Address.

I want to save a word document .doc with the ID and Title as the filename.

The end result in this example would be four word .doc with page 1 ID in a specific location, on page 2 Title and Name and on Page 3 Address.

Does anyone know how to achieve this with VBA? I have investigated Mail Merge but does not create separate word docs only creates them in the single document.

Thanks
 
Did you at least try the code in the link I posted with a mailmerge main document?

As for "the code does not work", with reference to post #4, that's hardly surprising as it's incomplete and erroneous: it references a copycell sub/function that is missing; and references a mis-named variable (Percell) instead of PersonCell.
Your comments re PersonCell and Perran suggest you really haven't understood the code's basics and that you've misinterpreted variable names as necessarily implying something about the data.


Hi,

Yes I did have a quick go at it but I do not know where to paste the code, excel or word and when I do a mail merge I used the wizard so not sure how to interact to get the macro to run with the wizard so hit a brick wall at that point.

I would like to round out the bookmark method and get the runtime error working and that it only creates word files for the data and title of the file is based on ID and Title values.

Any help would be appreciated.
 
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
The code creates 5 word files when it should be 4, the first a blank word file called ID then it creates the 4 word documents after.
That's probably because your worksheet has a header row titled 'ID' and your SOPRange is defined as including that row.
 
Upvote 0
I do not know where to paste the code, excel or word and when I do a mail merge I used the wizard so not sure how to interact to get the macro to run with the wizard so hit a brick wall at that point.
The fact it's a Word macro should give you a pretty good idea. As such it doesn't need to know whether the data source is an Excel workbook, Access database, Text file, Word table, CSV file or any other valid mailmerge data source. All you'd use the wizard for is the mailmerge setup. You'd then run the macro exactly the same way you would with Excel.
 
Upvote 0
That's probably because your worksheet has a header row titled 'ID' and your SOPRange is defined as including that row.

Great thanks I have started from A2 not A1 that has fixed that issue. Do you have any idea what the runtime error 462 is to do with wd.Visible = True ?

I have all this code in a module is this in the right place?

Option Explicit

'change this to where your files are stored

Const FilePath As String = "C:\Users\pablo.DESKTOP-EE3UNF2\Desktop\Excel to Word"
Dim wd As New Word.Application

Dim SOPCell As Range



Sub CreateWordDocuments()

'create copy of Word in memory

Dim doc As Word.Document
wd.Visible = True
Dim SOPRange As Range
'create a reference to all rows of data

Set SOPRange = Range(Range("A2"), Range("A2").End(xlDown)).Cells

'for each row in the list

For Each SOPCell In SOPRange
'open a document in Word
Set doc = wd.Documents.Open(FilePath & "test.docx")
'go to each bookmark and type in details
CopyCell SOPCell, "ID", 0
CopyCell SOPCell, "Title", 1
CopyCell SOPCell, "Name", 2
CopyCell SOPCell, "Address", 3
'save and close this document
doc.SaveAs2 FilePath & SOPCell.Value & ".docx"

doc.Close
MsgBox "Created files: " & SOPCell.Value & ".docx"


Next SOPCell

wd.Quit

MsgBox "Created files in " & FilePath

End Sub

Sub CopyCell(rg As Range, BookMarkName As String, ColumnOffset As Integer)

'copy each cell to relevant Word bookmark

wd.Selection.GoTo What:=wdGoToBookmark, Name:=BookMarkName

wd.Selection.TypeText rg.Offset(0, ColumnOffset).Value

End Sub
 
Upvote 0
The fact it's a Word macro should give you a pretty good idea. As such it doesn't need to know whether the data source is an Excel workbook, Access database, Text file, Word table, CSV file or any other valid mailmerge data source. All you'd use the wizard for is the mailmerge setup. You'd then run the macro exactly the same way you would with Excel.

Sorry I am a beginner so you will need to bear with me, okay I will add the code to Word then use the wizard to get to the last part before printing and run the macro to see what happens.
 
Upvote 0

Forum statistics

Threads
1,215,232
Messages
6,123,765
Members
449,121
Latest member
Vamshi 8143

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top