Consulting

Results 1 to 20 of 20

Thread: Controlling sections of Word through VBA

  1. #1

    Controlling sections of Word through VBA

    Hi All

    I have a form in Access which lists all the sections in a word template. The user needs to type in a number next to the name of each section and this number will determine the order of the sections. I do not know how to use these numbers to then control the order of the sections in Word. Can anyone help me please.


    Thanks
    Renee

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi Renee,

    Welcome to VBAX!

    I don't understand your question. The part I understand is that you want to navigate to perticular sections.

    You could do that for instance with: [VBA]
    Sub GoToSection()
    Selection.GoTo What:=wdGoToSection, _
    Which:=wdGoToAbsolute, _
    Count:=3
    End Sub
    [/VBA]

    But using selection is a bad habit and I'm shure if you tell us more about the thing you wish to accomplish there are better ways of coding it.

    So could you please tell us what you wanna do? Like: I press the button in access and the word document is opened and I wanna do this ant that to a perticular section....?

    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Renee,
    It sound to me like you're trying to compose a Word document by selecting some sections (AutoText?) by number, or am I barking up the wrong tree?
    Regards
    MD

  4. #4

    Controlling Word document through vba

    Thanks for your replies.

    I have a word template and in each section there is predefined text. The order of these sections can be changed. For example, the sections are:

    Executive Summary
    Introducing the Company
    Plan Fees
    Insurance
    Personal Services

    There is an Access interface which lists all these sections. The user determines the order of the sections in the Word template by typing in a number in a text box next to the section name in the Access form. When the user clicks on a button, one of the things that has to happen is that the order of the sections is determined by the numbers they have typed in the text boxes in the Access form. For example, if they type in a 1 next to Plan Fees, the document must start with Plan Fees. If they type in a 2 next to Personal Services, the next section in the Word document must by Personal Services, etc.

    Hope this makes more sense.

    I need all the help I can get.

    Thanks
    Renee

  5. #5
    Thanks for your replies.

    I have a word template and in each section there is predefined text. The order of these sections can be changed. For example, the sections are:

    Executive Summary
    Introducing the Company
    Plan Fees
    Insurance
    Personal Services

    There is an Access interface which lists all these sections. The user determines the order of the sections in the Word template by typing in a number in a text box next to the section name in the Access form. When the user clicks on a button, one of the things that has to happen is that the order of the sections is determined by the numbers they have typed in the text boxes in the Access form. For example, if they type in a 1 next to Plan Fees, the document must start with Plan Fees. If they type in a 2 next to Personal Services, the next section in the Word document must by Personal Services, etc.

    Hope this makes more sense.

    I need all the help I can get.

    Thanks
    Renee

  6. #6
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    OK, the problem is the use of the word "section". When you are talking with VBA coders, and Word. "Section" does NOT mean a textual portion of a document. A Section to VBA people is specifically a piece of a document separated by a Section break.

    It is very possible to have a document with:

    Executive Summary
    Introducing the Company
    Plan Fees
    Insurance
    Personal Services

    all in the same "Section" - according to Word. The fact that you think of it as different sections does not, to Word at least mean anything at all, unless these parts of the document are, in fact, separated by Section breaks.

    OK. So what can you do?

    Separate these chunks of text by Section breaks.

    Here is how you could do this. Let look at your two scenarios.

    Scenario 1. They type in 1 beside, or with, Plan Services and 2 for Personal Services.

    Scenario 2 - the reverse.

    First of all, I am going to assume that:
    Executive Summary
    Introducing the Company

    will always be there.

    Build an array of the Sections you want. Oh, and BTW, you have not discussed headers and footers. You had better take of that now or you will FOR SURE be posting back!

    So lets say you have an array

    Dim SectionNames (2) As String

    SectionNames(0) = "Plan Fees
    SectionNames(1) = "Insurance
    SectionNames(2) = "Personal Services

    Now you can match the choices the user makes to an actual item.

    The choose 1 (Plan Services), then as 2 (Personal Services) I am assuming this is from a drop down of some kind, or a listbox. Whatever. Just so long as you can make some sort of match to the array.

    i HOPE YOU ARE USING STYLES!!!!!!!!! If you are you can make things a lot easier.

    In any case. If you are using a dropdown of sort, then you can use the ListIndex of the choice to

    No comment on how you are making the document...so I will assume you have created it.....and this is starting off with a blank new document. If this is not correct, please post details.

    This is a public variable
    [vba]Public SectionName() As String[/vba]

    This is for however you are selecting the choices...the assumption is a Combobox names Combobox1. It SHOULD be named something else.

    [vba]Dim i As Integer
    Redim Preserve SectionName(i)
    SectionName(i) = ComboBox1.ListIndex
    [/vba]


    [vba]With Selection
    .Style = ActiveDocument.Styles("NewStyle1")
    .TypeText Text:="Executive Summary"
    .InsertBreak Type:=wdSectionBreakNextPage
    .TypeText Text:="Introducing The Company"
    .InsertBreak Type:=wdSectionBreakNextPage
    .TypeText Text:= SectionName(0)
    .InsertBreak Type:=wdSectionBreakNextPage
    .TypeText Text:= SectionName(1)
    .InsertBreak Type:=wdSectionBreakNextPage
    .TypeText Text:= SectionName(2)
    End With[/vba]

  7. #7
    Administrator
    VP-Knowledge Base VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi Renee,

    Ok so you have a document and that's devided in section. In each section there's another type of story and you want a macro to change the order of the sections (Document)

    This sounds like a strange way of setting up a document.

    Why not:
    • Have separate documents for a your different topics you'd like to combine
    • The checkboxes in the access form will represent those documents (You could also have a few dropdowns in which people can choose the documents and then just loop over those dropdowns and they will be the insert order of the document)
    • When you press the button al the documents are inserted into a predefined document in the order you want them to.
    I Think that would be easier to maintain as well.

    Does that sound like it would work for you?
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  8. #8
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Joppst, yes that could be done that way. Even as an alternative to my post above...

    You could do this with bookmarks. Have the "sections" - execitive Summary etc etc, as defined bookmarks and you can resort by them.

    Renee - it really comes down to trying to come up with a proper design. Are you just talking about the TITLES of these sections? Do you want to be able to move the entire "section"?

    Better specs....better code.

    Think about what you actually want.

  9. #9
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi Gerry,

    We where typing our (different) approaches simultaniously so I hope Renee doesn't get to confused over here...

    For me your approach of course would work as wel. And it was a good idea to explain the definition of a section, because that's not always as clear as seams....

    Well I hope Renee will give us some more clues to what she's trying because this is al I can come up with the current information...
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  10. #10
    Hi

    Thanks to everyone who has replied to my problem.

    I hope that I can explain myself better. Yes the Word document has already been created (as a template). The template contains a lot of text most of which always stays the same. Depending on choices made in the Access interface some of the text can change. These changes I have managed to insert OK. In the Word template, there are predefined sections with section breaks. The order of the sections in the template will change each time the user creates the document from the template. The order of these sections in the document is determined by a number that the user inserts into a check box next to a label in the Access form. These labels represent each section in the Word document. (The form has other uses as well. For example, when the user clicks on the name of the section, certain requirements need to be inserted - not really relevant here). Hence, the whole of each section needs to be moved and not just the headings. Unfortunately I am bound to the layout of the document as one file. However, if it is the best way forward, I am happy to do so. I will also need to attend to the headers and footers as well. Does this clarify the situation?

    Your help is greatly appreciated.

    Regards
    Renee

  11. #11
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi Renee,

    Well sorry but I'm still not receiving the info correct over here...(Sorry)

    Perhaps we should begin on coding something for you and pick it up from there...because I couldn't suggest a better way to help you.

    Now could you strip your database so it only contains the form you're automating Word with. And have a sample Word document that has more or less the layout the orignal document?

    If you could specify a work flow of your plan then that would be a great help aswell. But a skinned working database with a sample document will get us started.

    If you attach (zipped) it here then I'll start on it this weekend!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  12. #12
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Joost/Renee,
    A word of warning; there may be a problem posting a zipped database file, because they easily exceed the site limit.

  13. #13
    Administrator
    VP-Knowledge Base VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by mdmackillop
    Hi Joost/Renee,
    A word of warning; there may be a problem posting a zipped database file, because they easily exceed the site limit.
    Good point that's why I asked for a stripped version of the critter...if need be we can always do a personal mail...
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  14. #14
    Dear Jooste and mdmackillop

    Thanks for your replies.

    I have created a sample database which only has the form included. I have also created a doc with the contents page. Hopefully this will clarify the situation. I thought that it would probably be best to send them to you via personal email - would that be OK with you?

    Thanks
    Renee

  15. #15
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi Renee,

    Sure: joost at webforums dot nl
    Last edited by mdmackillop; 06-05-2005 at 05:07 AM. Reason: modification of email address
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  16. #16
    Hi

    Thanks to everyone who has tried to help. I think that probably the only way to do it is to divide the word document into separate files. The order of these files will then be determined by the numbers next to the section names in the Access forms. Does anyone know how to do it this way? Joost, did you get the Access form and the sample Word doc?

    Thanks so much again.

    Regards
    Renee

  17. #17
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi Renee,

    Yes I did..but I've been a bit busy..Sorry.

    I'll do my best.
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  18. #18
    Hi Joost

    Thanks - I don't mean to hassle you. I am just so grateful to anyone who tries to help.

    Regards

    Renee


  19. #19
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Hi Renee.

    Yes, the easiest way is, in fact, to have the sections as separate files and build the document from the order given from the user.

    The best way to dao this is have an array of the documents.

    [vba]Public IncludeFiles(4) As String
    IncludeFiles(0) = "C:\Source\File01.doc"
    IncludeFiles(1) = "C:\Source\File02.doc"
    IncludeFiles(2) = "C:\Source\File03.doc"
    IncludeFiles(3) = "C:\Source\File04.doc"
    IncludeFiles(4) = "C:\Source\File05.doc"[/vba]

    Now when you get the choices from the user you can build the document. I would pick up the choices and store them in a Public array. You also need a public counter.

    [vba]Public SectionsChosen() As Integer
    Public i As Integer[/vba]

    So say you have a dropdown, and a Choose button, and a Build Document button. The dropdown lists the sections. The Choose button takes the ListIndex of the dropdown and dumps that into the array of sections chosen.

    [vba]Sub cmdChoose_Click()
    Redim Preserve SectionsChosen(i)
    SectionsChosen(i) = Combobox.ListIndex
    i = i + 1[/vba]

    Say you have the final command button - named, for this exercise, cmdBuildDoc.

    [vba]Sub cmdBuildDoc_Click()
    Dim var
    ' reset i
    i = 0
    For var = 0 To Ubound(SectionChosen)
    With Selection
    .InsertFile FileName:=SectionChoses(i), Range:=""
    .InsertBreak Type:=wdSectionBreakNextPage
    End With
    Next[/vba]

  20. #20
    Hi

    The above solution looks good - thanks for your help. I will give it a try. I have to focus on something else for the next few weeks. If I struggle with it can I come back to you then?

    Thanks
    Renee

Posting Permissions

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