reformat TOC (table of content) and write it to text file

Hi,

I'd like to read the TOC starting from below "Part II test cases"

until the
end of TOC. After that, I need reformat the TOC hierarchy in the

format like
example below and finally write the result to a text

file.

Example:

TOC in Ms Word

Part II Test Cases

7
1 Schedule Document 7
1.1 Cleanup instance after

scheduling. 7
1.2 WebI Specific Scheduling. 8
1.3 Document

Format 9
2 Schedule to printer 9
2.1 Collate option for

Crystal Report 9
3 Default Destination. 11
3.1 Alter the

object destination to default destination. 11
3.2 Schedule to Default

Destination. 12

Text file

format: (wanted)

1=/Schedule Document
2=/Schedule Document/Cleanup instance

after scheduling
3=/Schedule Document/WebI Specific

Scheduling
4=/Schedule Document/Document Format
5=/Schedule to

printer
6=/Schedule to printer/Collate option for Crystal

Report
7=/Default Destination
8=/Default Destination/Alter the

object destination to default destination
9=/Default Destination/Schedule

to Default Destination

Code so far (only reading TOC with so many bugs)
Dim pField As Word.Field
Dim pFilenum as long
Dim pIndex as

long
Dim pText as string

Const pFIND = "part II Test

Cases"

'Iterate the fields in the document
For Each pField In

ActiveDocument.Fields

'Find the TOC
If pField .Type =

wdFieldTOC Then

'Get the TOC
pText =

pField.Result

'Find "part II Test Cases"


pIndex = instr(pText, pFIND)
if pIndex = 0

then
... 'error: not found


else
pText = Mid$(pText, pIndex +

len(pFIND)

'Write the TOC to the output

file
pFileNum = Freefile
open

"C:\.....\myfile.txt" for output as pFileNum
Print pFileNum,

pText
Close #pFileNum

end

if

Exit For
End If
Next

Please help me!
Thank you

so much. Any helps will be greatly

appreciated.

[2127 byte] By [tsoetant] at [2007-12-23]
# 1

Just wanted to make a quick post so you know your question hasn't been lost. I'll look into this at some point this evening. I haven't done much work with Word fields recently, but what you're trying to do is definitely possible -- it's more an issue of string parsing and hierarchy than of the fields themselves. I'll try to get back to you within the next day with a sample of code that does what you want.

duckthing at 2007-8-31 > top of Msdn Tech,Microsoft ISV Community Center Forums,Visual Basic for Applications (VBA)...
# 2
Thank you so much Duck!
Looking forward to your code.
tsoetant at 2007-8-31 > top of Msdn Tech,Microsoft ISV Community Center Forums,Visual Basic for Applications (VBA)...