I have inherited a macro that works on a preformatted Word file. Its purpose is to scan through contractual text, and replace certain phrases (a text find and replace). In essence, it operates like this:
FOR i = 1 To wdDoc.Shapes().Count
Set shp = wdDoc.Shapes(i)
If shp.Type = msoTextBox Then
If InStr(shp.TextFrame.TextRange.Text, "Some legalese text") Then
Call updateSectionThree(shp)
End if
' etc.
END IF
NEXT i
Previously, the shapes within the document were all of the type msoTextBox, however a change to the source file now means that most of these shapes are now grouped, of the type msoGroup.
In an attempt to quickly fix this, I attempted to ungroup all objects:
For Each s In wdDoc.Shapes
If s.Type = msoGroup Then s.Ungroup
Next
which I repeated a few times to unnest everything.
The issue is that when I do this, the object positioning of the text boxes within the group is lost; as in, all these ungrouped items then appear at the top of the document and offset against a different margin.
Prior to running the macro, I can see anchor items when I select various text boxes and groups; is it possible to lock the position prior to ungrouping. Or alternatively, capture these values and then reinstate the objects afterwards back to their original relative positions?
Or: is it possible to find and replace text within a grouped set of items (i.e. recursively work my way down a nested group until I reach an msoTextBox item, then find+replace) ? If so; how to do this?
I think either may be solid solutions, but I don't know how to do this.
I had considered simply adjusting the .top property of each text box, but given that replacement text is not always of the same size, I can't see this working. It needs to be relative to other sections of the document.
Any help gladly accepted, and please do comment if you need more details. I've tried to share what I can without confidential info.