PDA

View Full Version : Word Document creation with different sections



vkhura
08-01-2007, 09:59 AM
Hi
I am currently using a product called System Architect and i need to generate a word document by writing a VBA macro by using information contained in the product.
I know how to get information from the product and simply write it to word document.
However, since i am new to office automation, i am looking for a sample macro which shows me how to browse through various section of the word document and put various subheaders, add tables , format font and write in content into sections and sub-sections.
The document will use a template containing some sections but we will be adding further sections and subsections using macro.

Would really appreciate if anyone can provide me with the same.

Thanks

fumei
08-01-2007, 10:29 AM
i am looking for a sample macro which shows me how to browse through various section of the word document and put various subheaders, add tables , format font and write in content into sections and sub-sections.1. There are no such things as sub-sections in Word.

2. There are no such things as sub-headers in Word.

3. What do you mean by "browse through various section of the word document". Browse????

As for putting in tables, formatting font, and writing content, try recording some macros. Then look at the code.

If you have actual specific questions please post them.

vkhura
08-02-2007, 02:39 AM
Hi Fumei
1) Technically there are no Sub-sections but logically they do, E.g.
Main Document Section (Heading 1)

Documnent Subsection (Heading 2)

2)- Yeah sub-headers again no such things you are absolutely right i was so much engrossed in my work that i wrote HEADER instead of HEADINGS :) but Heading 1 being parent Header 2 becomes subheader. I just needed to know how to put heading 1,2,3.
Sorry for the confusion

3)- By Browse, i just meant how to move around tables and then to each row and cell , how to move around various sections and then add contents

I did asked a general question since it was my first time that i was trying to use a word macro.

Thanks for your Help, i did recorded a macro and trying to learn things out of it :).

For newbees who just want to get started can visit the following link on Microsoft site :
support.microsoft.com/kb/313193/

Thanks

fumei
08-06-2007, 04:11 AM
Yes, very true, logically there are sub-sections. However.....how is Word supposed to know what is logical in YOUR mind? Hmmmmm?

The same for headings. Yes, actually Word DOES understand headings fairly well.

Main Document Section (Heading 1)

Documnent Subsection (Heading 2)

Be very careful here. These are NOT Sections. Headings have nothing at all to do with Sections. You can talk all you want about "sections" in a document, and humans may intuit what you are talking about. However, to Word "section" means a very, very, specific thing. An area of a document separated by a Section break from other areas.

Period. Nothing more, nothing less. You can have a completely blank document (no text, no graphics...nothing) and still have 10 different Sections.

In fact, I do this often with templates. Some templates are pre-configured with Sections and their headers. Then all I have to do is insert text content into the Sections.

As for your #3;

"3)- By Browse, i just meant how to move around tables and then to each row and cell , how to move around various sections and then add contents"

I strongly, strongly recommend you learn how NOT to do this. Moving around means (at least to me) moving around visually. The Selection is moved. You see the tables, rows etc.

This is very inefficient coding. It depends on your purpose of course. However, you do NOT need to "move" around a document to put things anywhere.

You can put anything you like, just about anywhere, directly. Without any "moving around".

For example (and I just making this up quickly), say you want to put the text "Completed" as a new "line" (ie. paragraph) in Cell (4, 3) of each table in a document.

So what ever is the text in the cell of Row 4, Column 3 - you want to make a new "line" and add the text "Completed". Rather than "move" through the document, you can use objects.
Dim aTable As Table
Dim r As Range
For Each aTable In ActiveDocument.Tables()
Set r = aTable.Cell(4, 3).Range
With r
.MoveEnd Unit:=wdCharacter, Count:=-1
.InsertAfter Text:=vbCrLf & "Completed"
End With
NextDone. This would put a new paragraph line and the text "Completed" into every cell(4,3) in every table in the document.

Objects and Ranges. Learn to use them.

Again, if you have actual specific questions, please ask them.

fumei
08-06-2007, 04:18 AM
I would like to add, because people get confused...

When using the Range of a cell, you move the end ONE character to exclude the end-of-cell marker.

When using the Range.Text of a cell, you need to drop off TWO characters to exclude the end-of-cell marker.

This is because the end-of-cell marker is an oddball "double" ASCII character - Chr(13) and Chr(7). So if you grab cell text (via Range), there are TWO characters to deal with; but if you grab a cell range, there is ONE.

Odd I know. The Range of a cell is terminated by ONE "object", but the text of a cell is terminated by TWO ASCII characters.