PDA

View Full Version : Word report generation page/header problem.



ukemike
07-06-2005, 06:41 AM
Hey everyone,

Got a quick questions about a problem that I am having. I thought I might post it here first before I try to solve it myself because there might be an easy solution to it.
Basically, I have a C# program using VBA to control a Word document which is a template for a survery. The C# program uses Find/Replace to fill in appropriate fields in the survey.
The survery can be 2 - 5 pages long and it is for a mall. In the header of each survery I have it saying "Mall 1 mall_name, mall_address", and I am also using the Page {PAGE} out of {NUMPAGES} in the right side of my header. Everything thing works ok.
I now have to generate a batch of these surveys. I want them all to be one Word document. So i am using InsertFile so insert one copy do all the find/replace, go to a next page and insert another copy and so on. They problem now I have is with the headers. I need the header of the first survey to say Mall 1 mall_name1, mall_address_1, and then when we start survery 2 the header needs to say Mall 2 mall_name2, mall_address_2 and so on.
The pages at the start of survery one have to be One to SizeOfSurvey1, then at the start of survey two One to SizeOfSurver2 and so on...

Can anyone suggest a way to do this?

Mike

Anne Troy
07-06-2005, 10:48 PM
First of all, I'm no coder, okay? I'm just a Word geek, and I apologize for making you wait so long for a response. I had no idea "non-VBA" questions were being asked. :)

I don't think I can help you with the mall1, mall2 bit.

But, the NUMPAGES is wrong. Go to Insert-Field, choose numbering and choose SECPAGES and you'll get the number you need for the document as long as you're inserting next-page section breaks.

fumei
07-06-2005, 11:11 PM
1. Why do you want it to be one Word document? This seems to be asking for trouble....as you have discivered. From a design point of view, could you tell me why you have it as one document?

2. Make a Section break by code before your bring in the next file.

3. Move the Selection one character into the new section.

4. IMMEDIATELY make all headers NOT Same as previous.

5. Bring in your file.

Now, how are you getting "Mall 1 mall_name, mall_address" to become Mall 2, mall_name2"?

ukemike
07-07-2005, 06:13 AM
Hey guys,

Thanks for the response. I am trying to make this one document so that the person so that when the user asks to generate 15 surverys, he/she clicks on a button and 15 get generated and he/she can just click print and all of them would be printed.
Dreamboat, so your saying using {PAGE} and {SECPAGES} would work as long as I separate each survery by a section break right?

fumei, the way I am doing it right now is that all headers will be the same. They will be filled with the information from the last survey. This makes sense since the program used the last survey's information (mallname, malladdress, etc) to set the headers.
the procedure you wrote will make the header of each section different basically?
and also when you say Make a section break, is this a Next Page break right??


Thanks
Mike

fumei
07-07-2005, 10:05 AM
You are not consistent. Your last post:

the way I am doing it right now is that all headers will be the same
and the original post:

I need the header of the first survey to say Mall 1 mall_name1, mall_address_1, and then when we start survery 2 the header needs to say Mall 2 mall_name2, mall_address_2 and so on

Can't have it both ways. You chose to make it one document. therefore either the header is the same, or it is not. Your original post seems to want to have different headers, this last seems (but I am not totally sure) that you want same headers. Please clarify.

If you make a Section break, make it a Next Page section break. This makes....a new section. A new section has its own headers. However, by default, Word makes those headers the same as the previous Section. It seems like you want different headers.

They will be filled with the information from the last survey. This makes sense since the program used the last survey's information (mallname, malladdress, etc) to set the headers. You must explain this very very carefully. This sounds like code.

What last survey? Are you opening a previous file? Please explain, precisely, how your headers are being filled.

mdmackillop
07-07-2005, 10:58 AM
Dreamboat would know better than me, but would this not be a suitable use for the Office Binder? (I've never found one before)

Anne Troy
07-07-2005, 11:07 AM
Can't really use binder anymore since they canned it...

mdmackillop
07-07-2005, 11:20 AM
Oh well...:(

ukemike
07-07-2005, 11:42 AM
Ok step by step:

Sorry my posts need some decrypting.

1) I have a file (.doc) while is a template for a survey. In it I have a bunch of special keywords (<<mallname>>, <<mallstate>>, etc) in where the input data needs to go.

2) I open this file. I am using a C# program which calls the VBA.

3) I find/replace all the things that are needed (Example: all the "<<mallname>>" with 10th District Mall, and "<<mallstate>>" with New Jersey)

4) I do a page break and I do an InsertFile and insert another copy of this template (with all the special keywords) into the current location of this document.

5) I do find/replace on all the things again. And I again replace the keywords with data only this time from another mall.

This is the main loop. My problem is that the template is 3 pages, but it can expand. I need it so that the headers on each survery refrect the data in the survey, and the pages are numbered from 1 to the last page number of that survery.

Lets say i generated a document which has 3 surveys,

Survey 1) mall1 and it 10 is pages in length
Survey 2) another being mall2 and it is 2 pages in length
Survey 3) third being mall3 and it is 3 pages in length

The whole thing adds to 15 pages.

So here is a graphical example:

Actual PageNum: PageItNeedsToBe: HeaderItNeedsToBe

1 1 mall1_header
2 2 mall1_header
3 3 mall1_header
4 4 mall1_header
5 5 mall1_header
6 6 mall1_header
7 7 mall1_header
8 8 mall1_header
9 9 mall1_header


10 10 mall1_header



11 1 mall2_header

12 2 mall2_header



13 1 mall3_header

14 2 mall3_header

15 3 mall3_header



Let me know what you think





Mike

Anne Troy
07-07-2005, 11:46 AM
I can't figure out why you're not just using a mail merge...

ukemike
07-07-2005, 11:47 AM
I try to avoid Mail Merge as much as possible. Especially on Word 2000

ukemike
07-07-2005, 11:50 AM
Its buggy as hell especially in the earlier versions

Anne Troy
07-07-2005, 11:53 AM
What??

I think perhaps you don't know how to use it. I have never heard anyone refer to mail merge as being "buggy" at all. And the truth is, they've ruined it in the newer versions.

mdmackillop
07-07-2005, 12:03 PM
I agree with that sentiment DB, but maybe he still has the Binder?

Anne Troy
07-07-2005, 12:07 PM
Sorry, MD. I don't really see how binder comes into play, either.

ukemike
07-07-2005, 12:17 PM
Using mailmerge for the find/replace you mean right?

Bilby
07-07-2005, 02:49 PM
I don't know whether this is help hinderance or irrelevant, but in my application I use loops to generate/process multiple documents (each with their own header/Footers).

How about using a loop to generate all surveys as independent docs from one main macro?

Anne Troy
07-07-2005, 02:54 PM
Mike: Do you have a data file? Would something like this be easier?
http://www.vbaexpress.com/kb/getarticle.php?kb_id=381

fumei
07-07-2005, 08:39 PM
I still would like to know how you are filling the headers. I understand what you want as headers.

Do not a page break. Use a Section break.
Make the new Section not Same as previous

ukemike
07-08-2005, 07:04 AM
Hey guys,
Appreciate for all your help.
Dreamboat my data file is SQL Server. Im reading data from there.
fumei: Use a secion break. Ok, what did you mean Make the new Section not Same as previous.

I was trying to write some text then add a section break and add some more text in the second section. Then i went to the first section's headers/footers and named it "Section 1", after that I went into the second Section's headers/footers and named it "Section 2". After i did that section 1's header/footer also became "Section 2". So headers/footers are not section dependant.

Maybe theres an option that can make them section dependant that i dont know about??


Mike

Anne Troy
07-08-2005, 07:11 AM
fumei: Use a secion break. Ok, what did you mean Make the new Section not Same as previous.


Open a blank Word doc, Mike.
Insert a section break (next page).
Go to view header and footer.
See that "Same as previous"?
You don't want that.
There's a button on the header/footer toolbar to turn it off.

Anne Troy
07-08-2005, 07:12 AM
Also, if I were you, I'd dump the data from SQL to CSV, do a mail merge...

fumei
07-08-2005, 07:28 AM
I was trying to write some text then add a section break and add some more text in the second section. Then i went to the first section's headers/footers and named it "Section 1", after that I went into the second Section's headers/footers and named it "Section 2". After i did that section 1's header/footer also became "Section 2". So headers/footers are not section dependant.

Sorry, but in fact the example your wrote proves the point.

1. You write into Section header - "Section 1"

2. You write into Section 2 header - "Section 2"

If Section 2 is still set for Same as previous....and you are updating it, what exactly are you doing? You are making Section 2 header content = "Section 2". Yes. And since Section = Same as previous.....then it makes sense that because you are, in fact updating ONE object (shared by both Section 1 and Section 2), then...yes, the content updates for the header in Section 1.

Keeping it simple (by ignoring any settings for Different first page, Different Odd/Even), Word determines headers like this:

Section 1:
header object always exists.

Section 2:
If .LinkToPrevious = True then header object values are BYREF to header object Section 1. Any changes are also BYREF back to the object.

If .LinkToPrevious = False then header object values are BYVAL to the values given to it.

Section 3:
If .LinkToPrevious = True AND
Section 2 .LinkToPrevious = True Then
header object values are BYREF to header object Section 1. Any changes are also BYREF back to the object.

If .LinkToPrevious = True AND
Section 2 .LinkToPrevious = False Then
header object values are BYREF to header object Section 2. Any changes are also BYREF back to the object.

If .LinkToPrevious = False then header object values are BYVAL to the values given to it.

Section 4 etc etc etc.

They are very explicitly section dependent. It is just that it is a confusing, and perhaps not very intuitive, part of the object model. When you changed Section 2, because it is same as previous, Section 1 changed as well.

Try making Section 2 NOT same as, Then go back, do something with Section 1, then do something with Section 2. Section 1 should be whatever you put there.

ukemike
07-08-2005, 11:29 AM
This might be what i was looking for. Thanks very much guys for your help.


I will keep you posted on how it goes.
Mike

ukemike
07-11-2005, 09:18 AM
Hey guys. Ok everything is working out ok except the

Page { PAGE } out of { SECPAGES }

Secpages is always = 3.
This is how many pages the survey is actually in the beggining but after i write into it. It get to 4 - 5 pages. Shouldnt SECPAGES change from 3 to 4 or 5.

I use all line breaks except all the way at the end of the survey I put a new line section break.
Do i have to write the whole document and then put the

Page { PAGE } out of { SECPAGES }
in the header?

What do you guys think?

Thanks for all the help
Mike

fumei
07-11-2005, 12:59 PM
You need to post code. In addition:
I use all line breaks except all the way at the end of the survey I put a new line section break.
What does this mean? I do not understand, at all, what this means.

You use line breaks....uh, what do you mean?

SECPAGES is, well, as you may guess, the number of pages in the current section.

Doc has ONE section, with 3 pages. {PAGE} of {SECPAGES} will return:

Page 1 of 3
Page 2 of 3
Page 3 of 3

A NEW section inserted. You then add 2 pages into that section. {PAGE} of {SECPAGES} will return:

Page 1 of 3
Page 2 of 3
Page 3 of 3
Page 4 of 2
Page 5 of 2

{PAGE} is the calculated page number. It is NOT an absolute number. In the above example, if you make the new section break start numbering at 1, then the document looks like (same fields {PAGE} of {SECPAGES})

Page 1 of 3
Page 2 of 3
Page 3 of 3
Page 1 of 2
Page 2 of 2

As I do not know if you are using Section breaks properly, and I can not see code, I hope the above may help. You can leave the same fields in the header, just add a renumbering if that is what you want to do.

ukemike
07-12-2005, 08:02 AM
Messing with this right now. I can post code but its C# though. Im using a C# .net app to call VBA.

I have this code:

Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _

PreserveFormatting:=False


What is the actual value of Selection.Range. I tried looking in the object browser and searching for this and could not find it.


Mike

MOS MASTER
07-12-2005, 08:49 AM
Hi Mike, :yes

There is no constant value for Selection.Range

A range or selection object is your ticket to travel trough the document and access the properties & methods you need on the way.

A range or selection object represent a area in the document with a starting point (.Start) and ending point (.End) (Continues)

So you can set you own Range to where you want to insert the field.

But to answer your question better perhaps you could add some more detail to the question..What are you trying to do and whats not working. :whistle:

fumei
07-12-2005, 08:49 AM
Huh? I am sorry, but you totally through me for a loop.

The code inserts a empty field. What on eath has this got to do with Sections, section breaks, or header problems?????

Are you actually asking what is the value of Selection.Range?

MOS MASTER
07-12-2005, 08:52 AM
Huh? I am sorry, but you totally through me for a loop.

The code inserts a empty field. What on eath has this got to do with Sections, section breaks, or header problems?????

Hi Gerry, :yes

Yes good question...I must admit I didn't read the whole question..:giggle

fumei
07-12-2005, 09:06 AM
To expand on Joost's post...

Selection.Range is precisely that. It is the Range of the Selection. If the Selection is a point (sometimes known as the Insertion point - incorrectly I may add; it is the Selection point) then the Range Start ( as ALL ranges have Start and End) = range End.

Selection.Range.Start = 4589
Selection.Range.End = 4589 means these Selection is a point.

Selection.Range.Start = 4589
Selection.Range.End = 4599 means the Selection Range covers 11 characters. The numbers are inclusive. They are Long integers BTW, and are based on the very first character in the document = 0.

That aside, again...what is the relevance to this?????????