punak wrote: Tue Feb 06, 2024 5:55 am
I desperately need some code to find and replace the text in these text boxes.
Text boxes exist in a separate collection from the main text body, the Shapes collection. See
https://learn.microsoft.com/en-us/offic ... ord.shapes and
https://learn.microsoft.com/en-us/offic ... word.shape
Shapes are addressable via name:
Shapes("TextBox 1")
Or there is a count property and an iterator:
Shapes.Count
Shapes.Item(1)
You can change the text via the .TextFrame.TextRange.Text property, see
https://learn.microsoft.com/en-us/offic ... .textframe
There's a VBA example showing how to use search and replace in text boxes at
https://office-watch.com/2023/word-replace-text-boxes/
but this could be tricky to get to work unless you are experienced and confident with both the Word COM api and COMate. I suggest reading this item, if you haven't already:
https://learn.microsoft.com/en-us/offic ... nd-methods
You could convert this by using the CreateEnumeration command, see COMateObject Class → General Methods → CreateEnumeration in the COMate help file and 'Demo_CollectionEnumeration.pb' in the COMate examples folder.
I don't have a PC with Microsoft Office installed any more so I'm sorry I can't test this but, hopefully, this is a place to start:
Code: Select all
EnableExplicit
; See https://learn.microsoft.com/en-us/office/vba/api/office.msoshapetype
#msoTextBox = 17
; See https://learn.microsoft.com/en-us/office/vba/api/word.wdreplace
#wdReplaceAll = 2
#wdReplaceNone = 0
#wdReplaceOne = 1
IncludeFile "COMatePlus.pbi"
Define.COMateObject appWord, docThis, objShape, objTextRange
Define.COMateEnumObject colShapes
Define.I ShapeType
Define.S strCommand, strFind, strReplace
strFind = "ReplaceThis"
strReplace ="WithThis"
; Obtain an application object.
appWord = COMate_CreateObject("Word.Application")
If appWord = 0
Debug "Failed to create application object."
Debug COMate_GetLastErrorDescription()
End
EndIf
appWord\SetProperty("Application\Visible=#True")
Debug "Visible:" + COMate_GetLastErrorDescription()
; Open the document.
strCommand = "Documents\Open('c:\testIN.doc')"
docThis = appWord\GetObjectProperty(strCommand)
If docThis = 0
Debug "Failed to open the document."
Debug COMate_GetLastErrorDescription()
End
EndIf
; Get the shapes collection.
colShapes = docThis\CreateEnumeration("Shapes")
If colShapes = 0
Debug "Failed to get the shapes collection."
Debug COMate_GetLastErrorDescription()
End
EndIf
; Start iteration.
objShape = colShapes\GetNextObject()
While objShape
ShapeType = objShape\GetIntegerProperty("Type")
Debug "Type: " +COMate_GetLastErrorDescription()
If ShapeType = #msoTextBox
; Find and replace.
; See https://learn.microsoft.com/en-us/office/vba/api/word.find.execute
objTextRange = objShape\GetObjectProperty("TextFrame\TextRange")
Debug "TextFrame\TextRange: " + COMate_GetLastErrorDescription()
strCommand = "Find\Execute('" + strFind + "', #Opt, #Opt, #Opt, #Opt, #Opt, #Opt, #Opt, #Opt, " +
"'" + strReplace + "', " + StrU(#wdReplaceAll) + ", #Opt, #Opt, #Opt, #Opt)"
objTextRange\Invoke(strCommand)
Debug "Find\Execute : " + COMate_GetLastErrorDescription()
EndIf
; Next iteration.
objTextRange\Release()
objShape\Release()
objShape = colShapes\GetNextObject()
Wend
; Tidy up
colShapes\Release()
docThis\Release()
appWord\Release()