Iterate through the headings (or TOC) and create bookmarks to it

T

Totem

Hi,

I'm in need of writing a macro that goes through the .docx file and goes
through each heading like:

1.1 Specification number one

1.2 Specification number two

and create bookmarks on each one (bookmark names will persist - including
names - even if a small edit happened) . Then write save this .docx as an
HTML. All of this is so that other type of documents (defect tracking
system, etc) can point to specific sections/headings in the document.

How would I go about this?

thanks
 
D

Doug Robbins - Word MVP

Try

Dim apara As Paragraph
Dim i As Long
With ActiveDocument
For Each apara In .Paragraphs
If Left(apara.Style, 7) = "Heading" Then
.Bookmarks.Add "BM" & Format(i, "000"), apara.Range
i = i + 1
End If
Next apara
.SaveAs Replace(.Name, ".doc", ".htm"), wdFormatHTML
End With

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
K

Klaus Linke

BTW just in case it's not obvious, if you have a TOC, a bookmark for every
heading has already been generated automatically, which you might use in
your hyperlinks, like say
C:\data\test\myTest.doc#_Toc196742352

OTOH, having your own bookmarks with your own naming scheme does give you a
lot more control...

Regards,
Klaus
 
T

Totem

Doug, thanks a bunch, that works fairly well.

Something that I wondering about too (since I need to check for a specific
apara.Style), is there a way to have a .dotx that is shared accross .docx?
Kind of like a CSS? This way I can change the .dotx and it will
automatically update how things are seeing in the .docx.

Thanks
 
T

Totem

Yes, I knew about the TOC generating the bookmarks, the problem is that I
need to be able to:
1. Expose this bookmark name that can be linked from other systems
2. The bookmark name should not change because a new section was added in
between sections or something.

I'm affraid that Word decides to change the bookmark names and then all of
our links to the docx/html would be broken.
 
T

Totem

oh, another question. I need to run this macro that I'm writting accross
multiple .docx. Is there any way of specifying to run a specific macro (from
the command line or something) on a .docx?

thanks
 
K

Klaus Linke

Yes, lots of good reasons to create your own bookmarks.

It's still likely going to be hard maintenance...
I'm afraid you may be needing something like
http://www.linkfixerplus.com/office-marketplace/linkfixerplus-office-overview.htm
sooner rather than later :-(

Off topic: Maybe the XML format (XLink/XPointer/XPath) will make it easier
to work with such hyperlinks (say to a heading of a certain outline level,
with a certain text, in a certain document) in the future?

Klaus
 
D

Doug Robbins - Word MVP

You can create a routine to run the code on all of the documents in a
folder.

An example is contained in the article "Find & ReplaceAll on a batch of
documents in the same folder" at:

http://www.word.mvps.org/FAQs/MacrosVBA/BatchFR.htm

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
D

Doug Robbins - Word MVP

You can create multiple documents from any template (.dot, dotm or dotx) or,
attach the same template to multiple documents - Access the Templates and
Add-ins dialog by selecting Document Template from the Templates section of
the Developer tab on the Ribbon.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top