MS Office Forum / Word / Programming / July 2007
loop, populate an array and replace?
|
|
Thread rating:  |
Karina - 25 Jul 2007 06:56 GMT 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 document (I want to now go and replace this Figure *123* with the sequential number, so it becomes Figure 3-sequential number. It's not always going to be a Figure 3.....That number depends on where it is in the document and is pulled in from an external application.)
GRAPHIC
Figure 3-Figure*123* is captioned (I went through and replaced Figure *123* with a sequential number)
The Figure *123* is referenced multiple times throughout the document, so I have to find the text all over the place. It may be before or after the caption or in some other random place.
Here is my 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 [\*]*[\*]" '(This is what I want to capture as the oldValue) .MatchWholeWord = True .MatchWildcards = True
While .Execute MyRange.Text = " - " & Format(startTabNum, "00") & " - " '(This is what I want to capture as the newValue) startTabNum = startTabNum + 1 MyRange.Collapse Direction:=wdCollapseEnd
Wend End With End If
Next para
End Sub
I tried putting data into an array, but then my data wouldn't replace. Can anyone help????? Is there a better way to do this?
old man - 25 Jul 2007 20:40 GMT Hi Karina,
I am not sure what you want to do. I wrote this code (using the selection object - using the range object as you did is the way to go but just to show what I would do.) Its almost always faster to use ranges instead of selections but I wonder if the string only occurs several times if it pays to examine every paragraph.
In the code below I search for any paragraph with a particuler style and then search for the string and then put the old and new string in two dynamic arrays.
Then at the msgbox point you can go through the document again and replace each string with the corresponding new string.... Your note was a bit confusing as to what you are trying to do...
Old Man
Sub findtxt
Dim str1 As String Dim more1 As Boolean Dim oldstrings() Dim newstrings() Dim numstings As Integer Dim i1 As Integer
' assumes option base 1 is in start of module! numstrings = 0 'set up search.. With Selection.Find .Text = "" .Replacement.Text = "" .Forward = True .Wrap = wdFindStop .Format = True .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.WholeStory more1 = True Do While more1 = True Selection.Find.ClearFormatting Selection.Find.Style = ActiveDocument.Styles("Heading 3") Selection.Find.Text = "" If Selection.Find.Execute = True Then Selection.Find.ClearFormatting ' put whatever regex you are searching for.... Selection.Find.Text = "what" If Selection.Find.Execute Then str1 = Selection.Range.Text 'manipulate str1 and put results in str2 - notice I am using array cntr.. numstrings = numstrings + 1 str2 = str1 & numstrings 'now save old and new strings ReDim Preserve oldstrings(numstrings) oldstrings(numstrings) = str1 ReDim Preserve newstrings(numstrings) newstrings(numstrings) = str2 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 str1 = "Old" & vbTab & "New" & vbCrLf For i1 = 1 To UBound(oldstrings) str1 = str1 & oldstrings(i1) & vbTab & newstrings(i1) & vbCrLf Next MsgBox str1, vbInformation End Sub
> 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 [quoted text clipped - 59 lines] > I tried putting data into an array, but then my data wouldn't replace. > Can anyone help????? Is there a better way to do this? Russ - 26 Jul 2007 10:29 GMT 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 [quoted text clipped - 59 lines] > I tried putting data into an array, but then my data wouldn't replace. > Can anyone help????? Is there a better way to do this?
 Signature Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
Karina - 27 Jul 2007 03:56 GMT 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 [quoted text clipped - 64 lines] > > I tried putting data into an array, but then my data wouldn't replace. > > Can anyone help????? Is there a better way to do this? Russ - 27 Jul 2007 09:04 GMT 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 [quoted text clipped - 171 lines] >>> I tried putting data into an array, but then my data wouldn't replace. >>> Can anyone help????? Is there a better way to do this?
 Signature Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
Karina - 27 Jul 2007 14:18 GMT 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 [quoted text clipped - 179 lines] > >>> I tried putting data into an array, but then my data wouldn't replace. > >>> Can anyone help????? Is there a better way to do this? Russ - 29 Jul 2007 00:21 GMT 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 [quoted text clipped - 200 lines] >>>>> I tried putting data into an array, but then my data wouldn't replace. >>>>> Can anyone help????? Is there a better way to do this?
 Signature Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
Russ - 29 Jul 2007 01:14 GMT 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, > [quoted text clipped - 251 lines] >>>>>> I tried putting data into an array, but then my data wouldn't replace. >>>>>> Can anyone help????? Is there a better way to do this?
 Signature Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
Russ - 29 Jul 2007 01:34 GMT Oops, I mistakenly left "Strong" style in last subroutine message:
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) If Right(strCapOrig, 1) Like vbCr Then strCapOrig = Trim(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, > If the original found caption text variable, strCapOrig, contains a [quoted text clipped - 295 lines] >>>>>>> I tried putting data into an array, but then my data wouldn't replace. >>>>>>> Can anyone help????? Is there a better way to do this?
 Signature Russ
drsmN0SPAMikleAThotmailD0Tcom.INVALID
Karina - 30 Jul 2007 15:16 GMT Russ,
Thanks so much! I am trying this out right now and will let you know what happens.
> Oops, I mistakenly left "Strong" style in last subroutine message: > [quoted text clipped - 278 lines] > >>>>>>> in > >>>>>>> from an external application.) Karina - 31 Jul 2007 02:48 GMT Russ,
That is awesome! I would have never figured out how to search for the text when I already searched for it once. I inherited this DOORS output, and I'm working hard to change things, but not before this post-process is in place.
I tweaked things a bit, and played around with different options. I'm still modifying a little, but this is what I ended up with:
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") .Text = "Figure [\*]*[\*]"
.MatchWholeWord = True .MatchWildcards = True While .Execute = True strCapOrig = Trim(aRange.Text) With CaptionLabels(wdCaptionFigure) .IncludeChapterNumber = True .ChapterStyleLevel = 1 .Separator = wdSeparatorHyphen .NumberStyle = wdCaptionNumberStyleArabic End With
aRange.InsertCaption Label:=wdCaptionFigure aRange.Delete capNumber = capNumber + 1 Set aRange2 = ActiveDocument.Content With aRange2.Find .Text = strCapOrig While .Execute = True aRange2.InsertCrossReference ReferenceType:=wdCaptionFigure _ , ReferenceKind:=wdEntireCaption, _ ReferenceItem:=capNumber, InsertAsHyperlink:=True, _ IncludePosition:=False Wend End With
Wend End With
End Sub
Thanks a bunch!
> Russ, > [quoted text clipped - 276 lines] > > >>>>>>> > > >>>>>>> Paragraph text references caption.FIG Figure 3-Figure *123* in my main
|
|
|