One field referencing multiple bookmarks

U

UlfHJensen

I have a document containing several bookmarks, each a new letter for
revision; A, B, C...G. For each new version of the document, a new field and
letter is entered by the responsible. This procedure is ok (for now) and
works fine.
In the header of my document I would like the latest revision letter to
appear in a field.
How do I program my way out of:

Check if bookmarks until last filled is found
Clear field in header
Insert latest (last filled bookmark) revision

Any suggestions appreciated.
 
G

Graham Mayor

One possibility, assuming that you will use only single letters and that
each new letter is incremented by alphabet, you could add a docvariable
field to the header
{DocVariable varLetter} and run the following macro. This will update the
field to display the highest bookmark letter. It works on the premise that
bookmarks are filed alphabetically so when the macro checks each single
digit bookmark, the highest letter is recorded in the document variable.

Dim oVars As Variables
Dim oBM As Bookmark
Dim oStory As Range
Set oVars = ActiveDocument.Variables
For Each oBM In ActiveDocument.Bookmarks
If Len(oBM.name) = 1 Then
oVars("varLetter").Value = oBM.name
End If
Next oBM
For Each oStory In ActiveDocument.StoryRanges
oStory.Fields.Update
If oStory.StoryType <> wdMainTextStory Then
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
oStory.Fields.Update
Wend
End If
Next oStory
Set oStory = Nothing

If you want all the Bookmark letters present in the document to be listed
then you would need the following variation

Dim oVars As Variables
Dim oBM As Bookmark
Dim sList As String
Dim oStory As Range
sList = ""
Set oVars = ActiveDocument.Variables
For Each oBM In ActiveDocument.Bookmarks
If Len(oBM.name) = 1 Then
sList = sList & oBM.name & _
Chr(44) & Chr(32)
End If
Next oBM
sList = Left(sList, Len(sList) - 2)
sList = Left(sList, Len(sList) - 3) & Chr(32) _
& Chr(38) & Chr(32) & Right(sList, 1)
oVars("varLetter").Value = sList
For Each oStory In ActiveDocument.StoryRanges
oStory.Fields.Update
If oStory.StoryType <> wdMainTextStory Then
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
oStory.Fields.Update
Wend
End If
Next oStory
Set oStory = Nothing

http://www.gmayor.com/installing_macro.htm
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
U

UlfHJensen

Thanks Graham,
Your first paragraph assumptions are indeed correct and hence the macro
would probably work. I'll give it a try.
 
U

UlfHJensen

Hi Graham,
I have now tried but -even though the code executes flawlessly - nothing
shows in varLetter. I tried to single step the code and it appears the
statement If Len(oBM... never goes True depsite letters A and B in the
bookmarks respektively, i.e. varLetter show display a "B".
I have treid to troubleshoot myself but I am not that into VBA -yet. I am
not sure what the second part (the oStory-part) actually is intended to do.

Giving my basic idea a re-evaluation, I think probably the easiest way is
(for me, that is) to address directly the bookmarks and assigning (Set?) the
varLetter value from these. I have only 7 allowable revision letters so
having 7 Ifs or similar would not be to confusing. Of course I would have to
clear the contents of varLetter prior to checking for new revision.

Any help on this?
 
G

Graham Mayor

The macro checks the names of the bookmarks and not the values they contain.
The macro is simply looking for bookmarks called A, B, C etc which it loads
in turn into the variable each overwriting the previous letter, and as they
are in alphabetical order, the last letter is the one that the variable
retains ... until you add another bookmark and re-run the macro.
If you want to send me a sample of your document (use the link on the home
page of my web site - and remove any sensitive data) I would be able to see
exactly what you are doing and maybe suggest a workaround to achieve what
you want. At the moment I am not 100% clear on how you are using the version
information - the bookmark(s) and the 'field'. It may, for example not be
necessary top use a bookmark at all - you could simply write the letter to a
docvariable with a macro and reproduce it with a docvariable field
or
If there is only one letter in the document at a time, format that letter
with a unique character style (that can be the same as the underlying style
except for the name) and reproduce the content of the style - the letter -
in the header using a Styleref field.

If you merely want to reflect the revision letters in the variable then the
following macro will do that - up to a maximum of 7 letters A- G. Run this
macro in the the last saved version of the document. When you save it
increments the letter and saves the document with the same filename in the
same folder with the addition of the incremented letter e.g.

C:\Path\filename revision A.doc

Sub FileSave()
Dim oDoc As Document
Dim oVars As Variables
Dim oStory As Range
Dim sFname As String
Set oDoc = ActiveDocument
Set oVars = oDoc.Variables
On Error Resume Next
If oVars("varLetter").Value = "" Then
oVars("varLetter").Value = " "
End If
If Len(oDoc.Path) = 0 Then
oDoc.Save
End If
sFname = Left(oDoc.FullName, Len(oDoc.FullName) - _
(Len(oDoc.FullName) - InStrRev(oDoc.FullName, ".") + 1))
Select Case oVars("varLetter").Value
Case " ": oVars("varLetter").Value = "A"
Case "A"
oVars("varLetter").Value = "B"
sFname = Replace(sFname, " Revision A", "")
Case "B"
oVars("varLetter").Value = "C"
sFname = Replace(sFname, " Revision B", "")
Case "C"
oVars("varLetter").Value = "D"
sFname = Replace(sFname, " Revision C", "")
Case "D"
oVars("varLetter").Value = "E"
sFname = Replace(sFname, " Revision D", "")
Case "E"
oVars("varLetter").Value = "F"
sFname = Replace(sFname, " Revision E", "")
Case "F"
oVars("varLetter").Value = "G"
sFname = Replace(sFname, " Revision F", "")
Case Else: MsgBox "Maximum permitted revision already added." & vbCr & _
"Revision letter will not be updated" & vbCr & _
"Document will not be saved", vbInformation, "Revisions"
Exit Sub
End Select
For Each oStory In oDoc.StoryRanges
oStory.Fields.Update
If oStory.StoryType <> wdMainTextStory Then
While Not (oStory.NextStoryRange Is Nothing)
Set oStory = oStory.NextStoryRange
oStory.Fields.Update
Wend
End If
Next oStory
Set oStory = Nothing
oDoc.SaveAs sFname & " Revision " & oVars("varLetter").Value
End Sub


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - 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