Macro to increment field by one- suggestions?

J

JAnderson

Greetings,

I'm working on a large document (with many section breaks), and I would like
to have a field in the footer which is "Revision Number". I don't want to
use Word's RevNum field because that increments each time a user saves the
document. (I don't want to increment the field just because I'm fixing a
typo.)

This isn't supposed to be an iron-clad system, but I just want a way that,
after I've made significant changes to my document, I can click a toolbar
macro, increment the value by one, and have a message box tell me "Revision
Number increased to XYZ". Ideally, I could also keep the field editable, so
I could just type in "001" whenever I wanted to start the numbering over (for
whatever reason).

Any ideas or resources? It's been a long time since I programmed VBA from
scratch. Thanks!
 
D

Doug Robbins - Word MVP

Use

ActiveDocument.Variables("Revision").Value = 0

In a macro to set an initial value for the variable Revision

Then have the following code in a macro that is run when you click on a
toolbar button

With ActiveDocument.Variables("Revision")
.Value = .Value + 1
MsgBox "Revision number increased to " & .Value
End With

You can have the variable displayed in the document by using a { DOCVARIABE
Revisionv} field.
--
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
 
J

Jay Freedman

Greetings,

I'm working on a large document (with many section breaks), and I would like
to have a field in the footer which is "Revision Number". I don't want to
use Word's RevNum field because that increments each time a user saves the
document. (I don't want to increment the field just because I'm fixing a
typo.)

This isn't supposed to be an iron-clad system, but I just want a way that,
after I've made significant changes to my document, I can click a toolbar
macro, increment the value by one, and have a message box tell me "Revision
Number increased to XYZ". Ideally, I could also keep the field editable, so
I could just type in "001" whenever I wanted to start the numbering over (for
whatever reason).

Any ideas or resources? It's been a long time since I programmed VBA from
scratch. Thanks!

Create a custom document property (on the Custom tab of the File > Properties
dialog) named PrivateRevNum, and insert a DocProperty field in the footer to
display its value.

Use the following as the toolbar macro:

Sub UpdatePrivateRevNum()
Const PropName = "PrivateRevNum"
Dim strRevNum As String
Dim revNum As Long
Dim myRg As Range

On Error Resume Next
With ActiveDocument
strRevNum = .CustomDocumentProperties(PropName).Value
If Err.Number = 5 Then
' property isn't set yet so add it
.CustomDocumentProperties.Add _
Name:=PropName, LinkToContent:=False, _
Type:=msoPropertyTypeString, _
Value:="001"
End If

revNum = Val(strRevNum)
If revNum = 0 Then
' non-numeric value, so fix it
.CustomDocumentProperties(PropName).Value = "001"
revNum = 1
End If

' now it's safe to increment
.CustomDocumentProperties(PropName).Value = _
Format(revNum + 1, "000")

' update the fields
For Each myRg In .StoryRanges
Do
myRg.Fields.Update
Set myRg = myRg.NextStoryRange
Loop Until myRg Is Nothing
Next
End With

End Sub

If you want to reset the value, just go back to the File > Properties dialog and
modify it.
 
S

StevenM

My code is not as sophisticated as the others, but nonetheless:

Sub IncrementRevisionNumber()
Dim hfRange As Range
Dim nPos As Long
Dim nNumber As Long
Dim sStr As String

Set hfRange =
ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
nPos = InStr(1, hfRange.Text, "Revision Number:")
If nPos > 0 Then
nPos = InStr(nPos, hfRange.Text, ":")
hfRange.Start = hfRange.Start + nPos
sStr = CStr(hfRange.Text)
nNumber = CLng(sStr) + 1
sStr = CStr(nNumber)
If Len(sStr) = 1 Then sStr = "00" & sStr
If Len(sStr) = 2 Then sStr = "0" & sStr
hfRange.Text = " " & sStr
Else
hfRange.Collapse wdCollapseStart
hfRange.Text = "Revision Number: 001"
End If
End Sub

Run the macro once to start counting, and run it again every time you want
to increment. If you want to reset it back to one, you could double-click on
the footer and change the number to read 001 or anything else.

Steven Craig Miller
 
J

JAnderson

That's very elegant, thank you! My challenge now is that I have a lot of
documents (100+) that I want to start using with this field. Thus, my
challenge is to create a global macro. Thus, a few more questions:

1. Rather than run the separate code to assign a value to the "Revision"
variable, is there any way that the macro can use an IF statement to create a
variable (and assign a value of 0) if none exists?

2. I want to update the Docvariable field each time the macro is run, but my
documents contain a lot of merge fields. How can I selectively update only
the Docvariable field?

3. I use a formatting switch to display my revision as "001" (\# "000").
The message box has no formatting, so it just appears as "1". It's a small
detail, but is there any way I can format the message box similarly?

Thanks so much!
 
J

JAnderson

Thank you for the reply! I like this code a lot, but in my attempt at
brevity, I left out the fact that I'd be using this on many (100+) documents
with multiple users, so a global macro that users can click might be easier
to implement. I will experiment. Thanks!
 
J

JAnderson

Thanks! This is particularly nice because I don't need to update any fields
after running the macro. The trouble is that I already have a lot of
information in some of my footers, and not every page in my document has a
footer.

In the interest of brevity, I left out how I would particularly be using
this revision number. For various reasons, I have the revision number buried
inside a longer number we use for control purposes. Ex: XXXX001ZZZZZ. As
revisions increase, it would look like XXXX002ZZZZZ, and so forth.

I like the idea posted earlier about using the Docvariable field, because
then I can paste it where I want in the document, but its drawbacks are that
I need to update it somehow without updating every field in my document.
Although, as I think about it, updating every field wouldn't be so bad. What
do you think?
 
D

Doug Robbins - Word MVP

Is the bigger number XXXX001ZZZZZ alphanumeric as the XXXX and ZZZZZ
suggest?

If not, are there likely to be any other 12 digit numeric strings in the
document?

Depending on the answers to these questions, it could be possible to run a
macro that processed all of the documents in a folder and extracted the
existing revision number that is buried in that "bigger number" and used
that revision number to set the initial valueof the document variable and
replaced the number within that "bigger number" with a DOCVARIABLE field to
display the revision number and future updates of it.

--
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
 
S

StevenM

To: JAnderson,

If XXXX002ZZZZZ had sometype of unique identifying mark, it would be easy to
do a search for it and update it incrementally. In your earlier post, you
mentioned that this would be in your footer, that already limits one's
search. Are you using the primary footer, or odd and even page footers? Is it
in the first section, or later sections? Is it the only 12 character word in
the footer? Is XXXX & ZZZZZ numeric or alphanumeric, or what? Give me a few
more details and I'll re-write my code to those specifications. The hard part
is determining that unique identifying characteristic which will allow one to
search for it and know that one will only be modifying that bit of
information. Of course, if you prefer to go with one of the other
suggestions, that's fine too.

Steven Craig Miller
 
S

StevenM

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
 
J

JAnderson

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!
 
S

StevenM

JAnderson wrote: << 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? >>

'
' IncrementRevisionNumber
'
' For example, if we were looking for "XXXX001ZZZZZ"
' "XXXX" is the prefix; "ZZZZZ" is the suffix;
' and 3 is the number of the Revision Number's digits.
'
Sub IncrementRevisionNumber()
Dim sRevNum As String
Dim locRange As Range

Set locRange = Selection.Range

sRevNum = IncrementRevNum("XXXX", 3, "ZZZZZ")

locRange.Select
ActiveWindow.View.Type = wdPrintView
MsgBox "Revision Number increased to " & sRevNum
End Sub
'
' IncrementRevNum looks for a "word" with a "prefix,"
' Revision Number, and "suffix."
' The "prefix" is a string of variable length;
' the "suffix" is a string of variable length;
' the "revision number" can have a variable number of digits.
'
Function IncrementRevNum(ByVal sPrefix As String, ByVal nDigits As Byte,
ByVal sSuffix As String) As String
Dim sRevNum As String
Dim oRange As Range
Dim nCharacters As Integer
Dim nNumber As Integer

nCharacters = Len(sPrefix) + nDigits + Len(sSuffix)
Set oRange =
ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range
For Each aWord In oRange.Words
If Len(aWord.Text) = nCharacters Then
If StrComp(Left(aWord.Text, Len(sPrefix)), sPrefix,
vbBinaryCompare) = 0 Then
If StrComp(Right(aWord.Text, Len(sSuffix)), sSuffix,
vbBinaryCompare) = 0 Then
sRevNum = Mid(aWord.Text, Len(sPrefix) + 1, nDigits)
If IsNumeric(sRevNum) Then
nNumber = CLng(sRevNum) + 1
sRevNum = CStr(nNumber)
If Len(sRevNum) < nDigits Then
sRevNum = String(nDigits - Len(sRevNum), "0") &
sRevNum
ElseIf Len(sRevNum) > nDigits Then
sRevNum = String(nDigits, "0")
End If
oRange.Start = aWord.Start + Len(sPrefix)
oRange.End = oRange.Start + nDigits
oRange.Text = sRevNum
Exit For
End If
End If
End If
End If
Next aWord
IncrementRevNum = sRevNum
End Function

Steven Craig Miller
 
D

Doug Robbins - Word MVP

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
 
J

JAnderson

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!
 
D

Doug Robbins - Word MVP

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
 
J

JAnderson

Thanks! I will get started with some documents and see how it goes. Thanks
again for working this through with me!

Doug Robbins - Word MVP said:
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

JAnderson said:
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!
 
J

JAnderson

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!

Doug Robbins - Word MVP said:
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

JAnderson said:
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!
 
D

Doug Robbins - Word MVP

It won't matter if the docvariable field already exists in the document.
The purpose of the first macro was for it to be run as a batch process to
"initialize" all of the documents in a folder, so that the second macro
could then be used on any of those documents to update the Revision number
when required.

Add a .Range.Fields.Update inside the With ActiveDocument - End With
construction to update the fields in the document

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

JAnderson said:
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!

Doug Robbins - Word MVP said:
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

JAnderson said:
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
 
J

JAnderson

Doug,

Ok, my parameters have changed slightly, so I tried the following code (see
below). The new parameters are that I am trying to search for an EIGHT digit
number, followed by an asterisk. Example: 12345001* , where "001" is going
to be my revision number.

The below code picks up any 8-digit number in my document, even those
without asterisks at the end. I use error handling to 'test' whether or not
the variable exists, which is probably crude.

Sub IncreaseNum()
On Error GoTo Errhandler:
With ActiveDocument.Variables("varRevision")
.Value = .Value + 1
MsgBox "Revision number increased to " & Format(.Value, "00#")
End With

Errhandler:
Dim myrange As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(Findtext:="[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
MsgBox "Revision number set to " &
Format(ActiveDocument.Variables("varRevision").Value, "00#")
Loop
End With


I also wrote this code to Reset the numbering sequence to 001:

Sub Reset()

ActiveDocument.Variables("varRevision").Value = 1
MsgBox "Revision number reset to " &
Format(ActiveDocument.Variables("varRevision").Value, "00#")

End Sub


Any ideas on how I can only grab those 8-digit sequences that end in an
asterisk?? Thanks!

Doug Robbins - Word MVP said:
It won't matter if the docvariable field already exists in the document.
The purpose of the first macro was for it to be run as a batch process to
"initialize" all of the documents in a folder, so that the second macro
could then be used on any of those documents to update the Revision number
when required.

Add a .Range.Fields.Update inside the With ActiveDocument - End With
construction to update the fields in the document

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

JAnderson said:
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!

Doug Robbins - Word MVP said:
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
 
D

Doug Robbins - Word MVP

Use [0-9]{8}\*

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

JAnderson said:
Doug,

Ok, my parameters have changed slightly, so I tried the following code
(see
below). The new parameters are that I am trying to search for an EIGHT
digit
number, followed by an asterisk. Example: 12345001* , where "001" is
going
to be my revision number.

The below code picks up any 8-digit number in my document, even those
without asterisks at the end. I use error handling to 'test' whether or
not
the variable exists, which is probably crude.

Sub IncreaseNum()
On Error GoTo Errhandler:
With ActiveDocument.Variables("varRevision")
.Value = .Value + 1
MsgBox "Revision number increased to " & Format(.Value, "00#")
End With

Errhandler:
Dim myrange As Range
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(Findtext:="[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
MsgBox "Revision number set to " &
Format(ActiveDocument.Variables("varRevision").Value, "00#")
Loop
End With


I also wrote this code to Reset the numbering sequence to 001:

Sub Reset()

ActiveDocument.Variables("varRevision").Value = 1
MsgBox "Revision number reset to " &
Format(ActiveDocument.Variables("varRevision").Value, "00#")

End Sub


Any ideas on how I can only grab those 8-digit sequences that end in an
asterisk?? Thanks!

Doug Robbins - Word MVP said:
It won't matter if the docvariable field already exists in the document.
The purpose of the first macro was for it to be run as a batch process to
"initialize" all of the documents in a folder, so that the second macro
could then be used on any of those documents to update the Revision
number
when required.

Add a .Range.Fields.Update inside the With ActiveDocument - End With
construction to update the fields in the document

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

JAnderson said:
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
 

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