Share via

Generating Multiple MS Word Docs from Different Excel Rows

Anonymous
2019-07-03T14:54:44+00:00

Hello,

I recently created a code, in excel, that inputs data from row 3, columns A:J, into a MS word document. I did this by creating bookmarks in Word and referencing those bookmarks in my excel code. Moving forward, what I would like to do is add additional rows and create multiple Word documents based on the individual row's columns. For example, if I had 5 rows I would generate 5 different Word documents. Is this even possible? Below is my code for a single row.

Sub Word_Test()

 On Error GoTo errorHandler

 Dim WDApp As Word.Application

 Dim myDoc As Word.Document

 Dim mywdRange As Word.Range

 Dim CatD As Excel.Range

 Dim CatB As Excel.Range

 Set WDApp = New Word.Application

 With WDApp

 .Visible = True

 .WindowState = wdWindowStateMaximize

 End With

 Set myDoc = WDApp.Documents.Add(Template:="C:\Desktop\Test_Test.docm")

 Set EOD = Sheets("Sheet1").Range("A3")

 Set IED = Sheets("Sheet1").Range("B3")

 Set FED = Sheets("Sheet1").Range("C3")

 Set IP = Sheets("Sheet1").Range("D3")

 Set MN = Sheets("Sheet1").Range("E3")

 Set MName = Sheets("Sheet1").Range("F3")

 Set LOCA = Sheets("Sheet1").Range("G3")

 Set NOB = Sheets("Sheet1").Range("H3")

 Set BOC = Sheets("Sheet1").Range("I3")

 Set Add = Sheets("Sheet1").Range("J3")

With myDoc.Bookmarks

 .Item("EOD").Range.InsertAfter EOD

 .Item("IED").Range.InsertAfter IED

 .Item("FED").Range.InsertAfter FED

 .Item("IP").Range.InsertAfter IP

 .Item("MN").Range.InsertAfter MN

 .Item("MName").Range.InsertAfter MName

 .Item("LOCA").Range.InsertAfter LOCA

 .Item("NOB").Range.InsertAfter NOB

 .Item("BOC").Range.InsertAfter BOC

 .Item("Add").Range.InsertAfter Add

 End With

errorHandler:

 Set WDApp = Nothing

 Set myDoc = Nothing

 Set mywdRange = Nothing

 End Sub

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2019-07-03T16:07:43+00:00

Replace the comments

        ' Optionally add lines here to save and close the document

        ' Otherwise you'll end up with a whole bunch of open documents

with

        myDoc.SaveAs2 Filename:=Sheets("Sheet1").Range("H" & r) & ".docx", _

            FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False

        myDoc.Close

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

5 additional answers

Sort by: Most helpful
  1. HansV 462.6K Reputation points
    2019-07-03T15:31:22+00:00

    Like this:

    Sub Word_Test()

        Dim WDApp As Word.Application

        Dim myDoc As Word.Document

        Dim mywdRange As Word.Range

        Dim r As Long

        Dim m As Long

        On Error GoTo errorHandler

        Set WDApp = New Word.Application

        With WDApp

            .Visible = True

            .WindowState = wdWindowStateMaximize

        End With

        With Sheets("Sheet1")

            m = .Range("A" & .Rows.Count).End(xlUp).Row

        End With

        For r = 3 To m

            Set myDoc = WDApp.Documents.Add(Template:="C:\Desktop\Test_Test.docm")

            With myDoc.Bookmarks

                .Item("EOD").Range.InsertAfter Sheets("Sheet1").Range("A" & r)

                .Item("IED").Range.InsertAfter Sheets("Sheet1").Range("B" & r)

                .Item("FED").Range.InsertAfter Sheets("Sheet1").Range("C" & r)

                .Item("IP").Range.InsertAfter Sheets("Sheet1").Range("D" & r)

                .Item("MN").Range.InsertAfter Sheets("Sheet1").Range("E" & r)

                .Item("MName").Range.InsertAfter Sheets("Sheet1").Range("F" & r)

                .Item("LOCA").Range.InsertAfter Sheets("Sheet1").Range("G" & r)

                .Item("NOB").Range.InsertAfter Sheets("Sheet1").Range("H" & r)

                .Item("BOC").Range.InsertAfter Sheets("Sheet1").Range("I" & r)

                .Item("Add").Range.InsertAfter Sheets("Sheet1").Range("J" & r)

            End With

            ' Optionally add lines here to save and close the document

            ' Otherwise you'll end up with a whole bunch of open documents

        Next r

    errorHandler:

        Set WDApp = Nothing

        Set myDoc = Nothing

        Set mywdRange = Nothing

    End Sub

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Anonymous
    2019-07-03T18:12:32+00:00

    Thank you very much, my problem now though is placing the files in a specific folder I created. Right now, they are being saved to my documents folder. Should I add a line underneath with the code "custompath" and then include the file name?

            myDoc.SaveAs2 Filename:=Sheets("Sheet1").Range("H" & r) & ".docx", _

                FileFormat:=wdFormatXMLDocument, AddToRecentFiles:=False

            myDoc.Close

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2019-07-03T15:59:54+00:00

    Thank you so much this works perfectly, moving forward I will want to save and close the newly created word docs. Ideally, I would like to save the file using the text in the Excel column H. Can I just reference the cell and still use a code like this:

    .SaveAs2 Filename:=("file name goes here"),

    FileFormat:=wdFormatXMLDocument, AddtoRecentFiles:=False

    Was this answer helpful?

    0 comments No comments
  4. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more