Karina,
If the original found caption text variable, strCapOrig, contains a
paragraph mark at the end, then that could limit what is found for
crossreferences. So I adjusted for that in this revision.
Public Sub RedoFigureCaptions()
Dim aRange As Word.Range
Dim aRange2 As Word.Range
Dim capNumber As Long
Dim strCapOrig As String
Set aRange = ActiveDocument.Content
With aRange.Find
.Style = ActiveDocument.Styles("Strong")
.Format = True
While .Execute = True
strCapOrig = Trim(aRange.Text)
If Right(strCapOrig, 1) Like vbCr Then
strCapOrig = Left(strCapOrig, Len(strCapOrig) - 1)
End If
aRange.InsertCaption Label:="Figure", _
TitleAutoText:="InsertCaption1", _
Title:="", Position:=wdCaptionPositionBelow
aRange.Delete
capNumber = capNumber + 1
Set aRange2 = ActiveDocument.Content
With aRange2.Find
.Text = strCapOrig
While .Execute = True
aRange2.InsertCrossReference ReferenceType:="Figure" _
, ReferenceKind:=wdEntireCaption, _
ReferenceItem:=capNumber, InsertAsHyperlink:=True, _
IncludePosition:=False
Wend
End With
Wend
End With
End Sub
Karina,
I tested this on words styled "Strong", it found those words and created a
new Word Figure caption there. Then it looks for the same found text in the
document and creates a crossreference to that new caption. It loops that way
through the whole main document. (not headers or footers)
The options for captions and crossreferences can be tweaked.
A separate, similar subroutine can made to redo Table captions and
references.
Try it out on a copy of a test document.
Hopefully, this will achieve what I mentioned as a goal. Then future
insertions will be renumbered automatically, after the fields are updated.
This also creates hyperlinks that when clicked on, take you to the Figure.
Ideally you should try to adjust the 'DOORS' application so that it outputs
what you want.
Public Sub RedoFigureCaptions()
Dim aRange As Word.Range
Dim aRange2 As Word.Range
Dim capNumber As Long
Dim strCapOrig As String
Set aRange = ActiveDocument.Content
With aRange.Find
.Style = ActiveDocument.Styles("caption.FIG") 'tested with "Strong"
.Format = True
While .Execute = True
strCapOrig = Trim(aRange.Text)
aRange.InsertCaption Label:="Figure", _
TitleAutoText:="InsertCaption1", _
Title:="", Position:=wdCaptionPositionBelow
aRange.Delete
capNumber = capNumber + 1
Set aRange2 = ActiveDocument.Content
With aRange2.Find
.Text = strCapOrig
While .Execute = True
aRange2.InsertCrossReference ReferenceType:="Figure" _
, ReferenceKind:=wdEntireCaption, _
ReferenceItem:=capNumber, InsertAsHyperlink:=True, _
IncludePosition:=False
Wend
End With
Wend
End With
End Sub
Correct. There are no field codes in the document. That's the initial
approach I wanted to take, but couldn't make it work. I'm incredibly new
to
all of this, and I've read every post and article on captioning and
referencing and numbering and arrays and I just don't get how I can make it
happen, so, I have this code that partly works.
The thing is that no other references will be inserted. This document is
generated from the DOORS application. There will be hundreds of such
documents that intend to be generated and then not updated in Word. All the
updates happen in DOORS and then the documents are regenerated, so all the
processing in Word needs to be automated and transparent to the user. I
realize this is a total hack, but I'm being pressured to make this work with
the current set of constraints.
Any insights? Again, I really appreciate your responses.
:
Karina,
If you play around with Word's captions and crossreferences, you'll see how
easily everything is renumbered when another new caption is inserted
between
others and how the crossreferences to their respective captions change at
the same time. I believe your goal should be to get these plain text
captions and crossreferences turned into real Word captions and
crossreferences. None of these are currently fields now, correct?
They're not real cross-references. The document is generated by another
software application and they hard code the references to the extent that
they are the same as the table/figure caption, but when I have to renumber
the tables/figures, that sameness goes away.
So when I go through the document, I can renumber all the captions ok, but
then I need to create a lookup table to associate the old number and the
new
one.
texttexttexttexttexttexttext
texttexttexttexttext
texttexttexttext
as referenced in Figure *[number1]* and Figure *[number3]*below texttext
texttexttexttexttext
Graphic object
Figure *[number1]* texttexttexttexttexttexttext
exttexttexttexttexttexttext
texttexttexttexttext
texttexttexttext
as referenced in Figure *[number]* below texttext
texttexttexttexttext
Graphic object
Figure *[number2]* texttexttexttexttexttexttext
I incorporated oldman's code, but last I left the computer, the code was
still running. I'm not sure if there's a way to search/replace from the
array without looping through it? I'm incredibly new to all of this Word
VBA, so, I don't always understand what's going on. I really appreciate
all
of your time. Here's the new code:
Sub IncrementCountforTables()
Dim para As Paragraph
Dim startTabNum As String
Dim MyRange As Range
For Each para In ActiveDocument.Paragraphs
If para.Style = "caption.TBL" Then
Set MyRange = Selection.Range
startTabNum = 1
With MyRange.Find
.Text = "Table [\*]*[\*]"
.MatchWholeWord = True
.MatchWildcards = True
While .Execute
TRangeValue = MyRange.Text
ReDim Preserve TABold(startTabNum)
TABold(startTabNum) = TRangeValue
MyRange.Text = Format(startTabNum, "00")
TnewValue = MyRange.Text
ReDim Preserve TABnew(startTabNum)
TABnew(startTabNum) = TnewValue
startTabNum = startTabNum + 1
MyRange.Collapse direction:=wdCollapseEnd
Wend
End With
End If
Next para
For i1 = 1 To UBound(TABold)
string1 = TABold(i1)
string2 = TABnew(i1)
Selection.WholeStory
more1 = True
Do While more1 = True
Selection.Find.ClearFormatting
Selection.Find.Style = ActiveDocument.Styles("Normal")
Selection.Find.Text = ""
If Selection.Find.Execute = True Then
Selection.Find.ClearFormatting
' put whatever regex you are searching for....
Selection.Find.Text = "Table [\*]*[\*]"
If Selection.Find.Execute Then
str1 = Selection.Range.Text
If str1 = string1 Then
Selection.Find.Text = string2
End If
End If
' get to the end of the paragraph
Selection.MoveEnd Unit:=wdParagraph, Count:=1
Selection.Collapse direction:=wdCollapseEnd
' search the rest of the document
Selection.EndKey Unit:=wdStory, Extend:=wdExtend
Else
more1 = False
End If
Loop
Next
End Sub
:
Karina,
If all the captions are cross referenced throughout the document, then
this
webpage shows how to update cross references:
<
http://www.thedoctools.com/index.php?show=wt_cross-reference_trouble>
I have some code to go through all paragraphs and if the paragraph is
styled "caption.FIG" then find some text formatted like this Figure
3-Figure
*123* and replace those numbers with a sequentially numbered list.
What I would like to do is put the values of the old numbers Figure
*123*
and the
new sequential number into an array and then use that data to replace
other
paragraph text.
Paragraph text references caption.FIG Figure 3-Figure *123* in my main