PDA

View Full Version : Controlling sections of Word through VBA



Renee Voice
05-31-2005, 06:15 AM
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

MOS MASTER
05-31-2005, 12:39 PM
Hi Renee, :yes

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:
Sub GoToSection()
Selection.GoTo What:=wdGoToSection, _
Which:=wdGoToAbsolute, _
Count:=3
End Sub


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....?

:whistle:

mdmackillop
05-31-2005, 01:30 PM
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

Renee Voice
05-31-2005, 03:13 PM
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

Renee Voice
06-01-2005, 12:24 AM
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

fumei
06-01-2005, 10:13 AM
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
Public SectionName() As String

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

Dim i As Integer
Redim Preserve SectionName(i)
SectionName(i) = ComboBox1.ListIndex



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

MOS MASTER
06-01-2005, 10:14 AM
Hi Renee, :yes

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? :whistle:

fumei
06-01-2005, 10:26 AM
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.

MOS MASTER
06-01-2005, 10:30 AM
Hi Gerry, :yes

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

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...:whistle:

Renee Voice
06-01-2005, 08:40 PM
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

MOS MASTER
06-02-2005, 09:49 AM
Hi Renee, :yes

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! :whistle:

mdmackillop
06-02-2005, 12:03 PM
Hi Joost/Renee,
A word of warning; there may be a problem posting a zipped database file, because they easily exceed the site limit.

MOS MASTER
06-02-2005, 12:05 PM
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...:hi:

Renee Voice
06-02-2005, 06:49 PM
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

MOS MASTER
06-03-2005, 09:21 AM
Hi Renee, :yes

Sure: joost at webforums dot nl :whistle:

Renee Voice
06-09-2005, 11:16 PM
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

MOS MASTER
06-10-2005, 02:29 PM
Hi Renee, :yes

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

I'll do my best. :whistle:

Renee Voice
06-11-2005, 11:02 PM
Hi Joost

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

Regards

Renee

fumei
06-13-2005, 08:17 AM
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.

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"

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.

Public SectionsChosen() As Integer
Public i As Integer

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.

Sub cmdChoose_Click()
Redim Preserve SectionsChosen(i)
SectionsChosen(i) = Combobox.ListIndex
i = i + 1

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

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

Renee Voice
06-16-2005, 08:24 PM
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