I was just thinking- is there a way to use an IF statement to place the
first
macro into the second one? For example, the code would run the first
macro
only if there was no docvariable field already in the document,
otherwise,
it
would just increment the already-existing field.
Lastly, is there any way to auto-update the field at the end of the
macro?
Thanks!
:
Assuming that the 001 is the revision number, the following code will
replace it with a { DOCVARIABLE varRevision \# 00# } field that will
display
the value that is assigned to the variable that it creates in the
document
named varRevision
Dim myrange As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(Findtext:="[0-9]{8}*[0-9]{8}", Forward:=True,
MatchWildcards:=True, Wrap:=wdFindStop) = True
Set myrange = Selection.Range
Selection.Collapse wdCollapseEnd
myrange.Start = myrange.End - 3
With ActiveDocument
.Variables("varRevision").Value = Val(myrange.Text)
.Fields.Add myrange, wdFieldEmpty, "DOCVARIABLE
varRevision
\#
00#"
End With
Loop
End With
You could incorporate that code into that in the article "Find &
ReplaceAll
on a batch of documents in the same folder" at:
http://www.word.mvps.org/FAQs/MacrosVBA/BatchFR.htm
so that it would go through all of the documents in a folder setting a
document variable in each document to the current revision number and
replacing the revision number with a docvariable field that displayed
the
revision number.
Then, when you want to update the revision, you would have the
following
code in a macro in a global template and running it would increment
the
value of the variable varRevision in the active document by one:
Then have the following code in a macro that is run when you click on
a
toolbar button
With ActiveDocument.Variables("varRevision")
.Value = .Value + 1
MsgBox "Revision number increased to " & Format(.Value, "00#")
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
Yes, but to be more specific, let's say it's the following, starting
with
the
asterisk: *99999999*99999001
Where "9" represents a number and "*" is literally an asterisk. It
will
definitely be the only 18 digit sequence like this anywhere in the
document.
Thanks!
:
Is it always a nine digit number or a 12 digit number and are they
likely
to
be the only 9 or 12 digit numbers in the document (at least without
thousands separators)?
Depending upon your answer to these questions, it may be possible
to
have
just one macro for all of the documents.
--
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
That was my own fault- XXX and ZZZ are numbers; it is a numeric
string.
But
the other numbers would remain the same at all times, so perhaps
there's a
way to isolate only those numbers?
The trouble is that, with so many documents (each has different
numbers),
I'd have to alter the code for each document... Thanks for
helping
with
this!
:
To: JAnderson,
Of course, if your footer contains other 12 character words
where
characters
5-7 are numeric, then ignore this message.
' IncrementRevisionNumber
' Looks for a 12 character word where characters
' 5-7 contitue the revision number (ex. XXXX002ZZZZZ).
' It looks for this number in the footer.
' Once found, it increments the number.
Sub IncrementRevisionNumber()
Dim oRange As Range
Dim sStr As String
Dim nNumber As Long
Dim locRange As Range
Set locRange = Selection.Range
Set oRange =
ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
For Each aWord In oRange.Words
If Len(aWord.Text) = 12 Then
sStr = Mid(aWord.Text, 5, 3)
If IsNumeric(sStr) Then
nNumber = CLng(sStr) + 1
sStr = CStr(nNumber)
If Len(sStr) = 1 Then sStr = "00" & sStr
If Len(sStr) = 2 Then sStr = "0" & sStr
If Len(sStr) = 4 Then sStr = "000"
oRange.Start = aWord.Start + 4
oRange.End = aWord.Start + 7
oRange.Select
oRange.Text = sStr
Exit For
End If
End If
Next aWord
locRange.Select
ActiveWindow.View.Type = wdPrintView
MsgBox "Revision Number increased to " & sStr
End Sub
I return the view to Print view, change this line to your
preference.
For
example, if you prefer normal view, change wdPrintView to
wdNormalView.
Steven Craig Miller