+ Reply to Thread
Results 1 to 10 of 10

Looping through an Excel list to generate a dynamic Word document

  1. #1
    Registered User
    Join Date
    03-22-2011
    Location
    France
    MS-Off Ver
    Excel 2003
    Posts
    5

    Question Looping through an Excel list to generate a dynamic Word document

    Hello to all,

    I'm an Excel and Word user but not used to use macro or vba programming (don't have any ability on these fields).
    I'm looking for a solution for the following problem : I'd like to generate a dynamic Word document (which would be based on a template) depending on the content of an Excel document.

    What I'm looking for for instance is to be able to generate a document like this :

    I) first information on the first line of Excel

    content_of_the_other_first_line_cells would compose the content of this

    II) first information on the second line of Excel

    content_of_the_other_second_line_cells would compose the content of this

    ...

    And the Word document would thus be generated depending on the content of this Excel, and would allow me to generate any new Word document using the same template just by modifying my Excel file, so it would probably don't have the same number of titles depending on the content of this Excel file.

    I tried to use "mailing" fonction in Excel but it only allows me to create fixed documents without the possibility to have a dynamic document.

    My idea was to loop through this Excel list and to always have the same treatment on the line (generating the title then the content depending on the contents of the line... and so on). But I'm stuck at this point because I don't know how to do this using either basic Excel/Word fonction or VBA. Maybe macros would make the point too.

    Any clue or solution ?

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Looping through an Excel list to generate a dynamic Word document

    Hi,

    Why do you need to generate a Word document? In my experience it's much easier to keep everything in Excel.
    An Excel sheet can be set out to look like a typical word document. Most users I know invariably prefer to receive a finished document in Excel so that where necessary they can use it as the base for other tasks.

    And if the ultimate output is a pdf file there's absolutely no need for Word.

    Just a thought.....

    Regards
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    03-22-2011
    Location
    France
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Looping through an Excel list to generate a dynamic Word document

    Actually, I need a Word document because the document will be made of many information with a lot of text pages. My thought was to made it like I said, but there are actually many content in each section, with each section corresponding to only 1 line in the Excel.
    The objective is to automate the creation of the word document using Excel preset document with only cells to complete to generate the Word document.

    Thanks for your answer anyway

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Looping through an Excel list to generate a dynamic Word document

    Hi,

    OK, but I still don't see why a Word doc is the only unique solution.

    It would of course be possible to pick up a line of cells from Excel and with a macro use them to populate a paragraph or single row in Word, and even slot them into specific places.

    But just because you have other text pages in the Word document I can see no reason why those self same text paragraphs could not exist in an Excel sheet, using word wrap and merged cells if necessary. There is no conceptual difference between something you see in a Word doc and something you see in an Excel sheet. The two can be made to look exactly the same.

    Regards

  5. #5
    Registered User
    Join Date
    03-22-2011
    Location
    France
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Looping through an Excel list to generate a dynamic Word document

    Well, actually, I want to make a Word document in order to have a summary function, and so on. And the difficulty is that I need that summary to be dynamic, depending on the content of my Excel document.
    Because as I said, the word document would be generated based on the content of the xls doc, which wouldn't be with the same number of line each time...

    That's why I had in mind a "loop" to populate the Word document. I guess that a macro would perform this indeed, but my point is that I don't know where to start, as I never created any macros...

    That's on this point that I'd really need help

    Thanks anyway for your answer.

    Regards

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Looping through an Excel list to generate a dynamic Word document

    You can use mailmerge in Word to assemble any kind of document, using the information in Excel.
    Through the mailmergemethod you can establish a direct connection between the Word-document and the Excel workbook.
    By using fields in Word you can import, introduce text conditionally on the values in Excel, etc.

    To help you any further it would be wise to post an example Word-document and an example Excel workbook.



  7. #7
    Registered User
    Join Date
    03-22-2011
    Location
    France
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Looping through an Excel list to generate a dynamic Word document

    I tried to used mailmerge actually, but I was blocked because the generated document was not "dynamic" and the mailmerge function creates me 1 Word document for each line in the Excel document.
    The fields were static, and that was not what I was looking for.

    Here's the structure of my Excel file :
    Phase ----- Phase Description ----- Phase Beginning date ----- Phase Ending date ----- Phase ressources


    In addition to that, I would need to have a global field for the following fields :
    Document Reference
    Document Date
    Read by (name of verificator)
    Writed by (name of writer)

    And this would create a Word composed of a title (Document Reference) a first page containing my document information (date, reader, writer, brief introduction), then a summary with the content of the document, which would be composed of :
    I. Phase 1
    --- Description Phase 1
    --- Date Phase 1
    --- Ressources Phase 1

    II. Phase 2
    --- Description Phase 2
    --- Date Phase 2
    --- Ressources Phase 2

    ...

    And so on, depending on the number of line of the Excel document.

    Did I made my point clear ? It's not easy to explain in english, as I'm french :P

    Thanks anyway !

  8. #8
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Looping through an Excel list to generate a dynamic Word document

    In the attachment a Word document with a macro.

    The only thing you have to change in the macro is the fullname of the Excel-file.
    Then you can 'play' the macro and see the result in your document.
    You can switch the fieldview using Alt-F9.

    Awaiting your feedback.
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    03-22-2011
    Location
    France
    MS-Off Ver
    Excel 2003
    Posts
    5

    Re: Looping through an Excel list to generate a dynamic Word document

    You're macro is a very good beggining to help me.
    I still have one more question about this :
    - How would I apply a "style" to the content of a cell ? (like in your macro, apply title_1 style to the sq(j,1) for instance)

    Thank you very much for your fast answer :-D

  10. #10
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Looping through an Excel list to generate a dynamic Word document

    In the example file I put the whole content of sheet1 into 1 Word-field.
    Since I have no idea how you want to format the distinct elements I can't give a simple solution.
    It would be nice to post an example of the Excel sheet and of the layout in your Word-document.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1