Share via

Generating MS word document (automation) with a menu of section entries

Anonymous
2023-03-12T14:02:59+00:00

Hello,

Here is the description of what needs to be done through a VBA macro. I am not clear on how this should be structured (lack of enough experience). Sorry, if this kind of question is not a good one to ask here without concrete code but I actually don't know how to structure this.

  1. I need to generate a document (say report1.docx) through a VBA macro (e.g. click of a button)
  2. The document has predefined number of sections and titles (say three)
  3. For each section there should be a menu of multiple paragraph options (these are not like a restaurant menu option, this is actually a paragraph) made available separately (these can be predefined and itemized qualified documents in another folder where each document is one possible paragraph of report1.docx). For example- section 1 has 10 possible qualified entries, section 2 has 10 possible qualified entries, section 3 has 5 possible qualified entries etc. The "database" of qualified entries can be a separately managed folder or any suitable choice.
  4. User gets to chose from a menu - Section 1 - chose entry 3, Section 2- chose entry 5, Section 3, chose entry 2.
  5. After the menu choice, click button and generate report1.docx in predefined format.

I was not sure if the "menu entries" should be a table in Excel instead of paragraph documents etc. Either way, I was not clear on a good structure for such a code.

Any help in this direction will be very much appreciated.

-HK_002.

Microsoft 365 and Office | Word | Other | 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

19 answers

Sort by: Most helpful
  1. Jay Freedman 207.6K Reputation points Volunteer Moderator
    2023-03-12T20:53:03+00:00

    I agree with Femi's general outline. I'll try to describe some of the details that it's likely to involve, and some of the issues that will need to be resolved, preferably before you make any concrete effort.

    One of the first questions to settle is whether the system for generating the final documents will be used only on one computer or on several or more. If it's only one, then the documents that hold the option paragraphs can be stored in any folder, as long as the code has the path to that folder. If the setup needs to be on multiple computers, especially if some are Windows PCs and others are Macs, it will be simpler to put everything into a single folder -- then the code can just look in "the same folder as the file that contains the code".

    You may see some suggestion that the option paragraphs should be stored in Quick Parts (building blocks) in the report template. Although that could make the programming simpler, it would make it more difficult to edit, add, or delete paragraphs. Separate documents, either per option or per paragraph, will make maintenance easier. If you choose to have a document per paragraph, then each option must be identifiable, most likely by being enclosed in a bookmark.

    The user interface that Femi mentioned should be a userform, which is a custom dialog box created by VBA code (https://gregmaxey.com/word_tip_pages/create_employ_userform.html). There are a couple of ways to design the userform. One way is to display one dropdown list for each section of the report; each dropdown contains the list of options for that section. Another way is to have one dropdown containing a list of sections; when a section is selected in that dropdown, the list of its options is loaded into a second dropdown. The first way has the advantage that all of the choices of options can be seen at once, but the drawback that a dialog with 10 or 12 dropdowns and a couple of buttons will be rather cluttered. The second way is less imposing, but you'd be able to see the chosen option for only one section at a time; that may require adding a summary display.

    Another usability question is how you would know which option to choose for a given section of a report. Each entry in a dropdown is limited to a single line, which may make describing the paragraphs difficult. The userform could have a text area (technically, a label control) that displays some or all of the paragraph that corresponds to the current selection in the dropdown. Alternatively, a "Show" button could call a message box containing the paragraph.

    I'll keep an eye on this thread in case you have other questions about the design and the code to implement it.

    Was this answer helpful?

    2 people found this answer helpful.
    0 comments No comments
  2. Jay Freedman 207.6K Reputation points Volunteer Moderator
    2023-03-12T23:17:29+00:00

    As with many situations in software design, there are various ways to set up the system, and they always involve tradeoffs.

    Storing the sections and options in a spreadsheet is an excellent arrangement, with a caveat.

    I have experience with a similar setup for a law practice to create complex estate planning documents and wills based on a series of option selections. The optional paragraphs were supplied in cells of an Excel sheet, much as described here. There was quite a lot of text formatting in those cells, which must have been hard to create and certainly was painful to read. It would have been far easier in Word documents, which are intended to be formatted. Further, transferring formatted text from Excel to Word isn't always easy. Also, if variable values have to be inserted in the middle of a paragraph, where Word would use a field, Excel would have to designate the location in some other way. All told, I'd go with keeping the full text of the paragraphs in Word documents and have only the "titles" (the text to show in the userform dropdowns) in the spreadsheet along with a link.

    The code to run the whole system can be in the Excel workbook. It's certainly possible to write macro code in Excel that carries out operations in Word, and vice versa. https://wordmvp.com/FAQs/InterDev/ControlWordFromXL.htm gives an example. Of course, you need to know how to do the operations with Word's objects and commands.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  3. Doug Robbins - MVP - Office Apps and Services 323K Reputation points MVP Volunteer Moderator
    2023-03-12T22:32:23+00:00

    I would use an Excel Workbook as the repository for all of the text that you want to have available for selection, with a separate sheet for each Section and on each of those sheets, in the first column have an entry that can be used to identify the text that is to be inserted with that text being inserted into the second column.

    WIth that arrangement, the repository could be expanded, modified simply by accessing the Workbook and the UserForm to which Jay made reference would be populated with the current lists of Sections and items for each Section.

    The Word side of things should be a Macro-enabled Template (*.dotm) containing an AutoNew macro that contained the code to populate and display the UserForm.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  4. Anonymous
    2023-03-12T14:53:24+00:00

    Hi Femi,

    Thanks for the prompt reply. Please advise and clarify the following -

    "Create the list of paragraph options for each section in a separate folder or document."

    Do you mean save -

    para1_option1.docx

    para1_option2.docx

    ....

    para2_option1.docx

    ...

    All in ONE folder?

    OR

    One document having all options for paragraph 1

    Second document for all options for paragraph 2.

    etc?

    In practical implementation having each qualified paragraph option submitted/saved as a separate document is more convenient but then the labeling of the documents and how they are stored to be treated as a "database" might matter.

    Once this structure is understood, I have some more questions about the details of implementation and I will get started.

    Thanks and best regards,

    HK_002.

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2023-03-12T14:40:51+00:00

    Hi HK_002,

    I'm Femi and I'd be happy to help you with your question.

    Kindly create a new Word document and save it as a macro-enabled document (with extension .docm) so that you can run VBA macros in it.

    Create the sections and titles of the report as required.

    Create the list of paragraph options for each section in a separate folder or document.

    Create a user interface to select the paragraph options for each section. You can create a form with dropdown lists or radio buttons to select the options.

    Write a VBA macro that reads the user's selections and inserts the selected paragraphs into the corresponding sections of the document.

    Best Regards,

    Femi

    Was this answer helpful?

    0 comments No comments