PDA

View Full Version : Solved: Replacing Text in All TextBoxes



clhare
02-19-2009, 09:55 AM
I have a document with several textboxes in it. I need to replace the text in any textbox in the document who's name starts with "WS". Is there a way to loop through all the text boxes in the document and change the text value by macro? The textboxes may be in the footers, so the macro has to be able to find them there as well.

fumei
02-19-2009, 10:21 AM
As usual, please state what "textboxes" actually means.

Are these textboxes inserted with Insert > Textbox?

Are they text formfields?

As you mention that textboxes may be in the footers, I suspect they could be Shapes (Insert > Textbox), as it not common to put formfields in footers. But in that case, what are you doing to make them have a name that starts with "WS"?

clhare
02-19-2009, 10:47 AM
Sorry....

These are mainly in the footers (there's one in the First Page footer, another in the Even Page footer, and another in the Odd page footer). If there's an additional section in the document that is not linked to the previous section, then there's another set of 3 text boxes.

I'm not exactly sure how they were created (someone else did it along time ago). In order to do anything with them while in the document, I have to bring up the Visual Basic toolbar and click on the Design Mode button. Then in order to update the text in the textbox, I have to right click on it, select Properties, and then change the value in the Text property. All the textboxes have names that start with "WS" followed by a number (which could be 1, 11, 111, 2111--it's weird the way Word numbers additional text boxes). I think "WS" was the initials of the guy that set this up originally.

We've always used the following code to update the value of the 3 textboxes, but the problem is that now we're being asked to remove sections by macro and if the section is removed that did not link the footers, Word crashes when the macro can't find one of the textboxes. Also, if there are more textboxes than we've accounted for in the macro, not all of them will be updated.


' Insert values into text boxes
ActiveDocument.txtWS.Text = ""
ActiveDocument.txtWS.Text = str2DBarcode1
ActiveDocument.txtWS1.Text = ""
ActiveDocument.txtWS1.Text = str2DBarcode1
ActiveDocument.txtWS2.Text = ""
ActiveDocument.txtWS2.Text = str2DBarcode1



I have no idea how to do this so it replaces the Text value no matter how many of these "WS" textboxes there are in the document.

fumei
02-19-2009, 12:32 PM
Good heavens! OK, they are ActiveX textboxes.

"All the textboxes have names that start with "WS" followed by a number"

Please careful with things. The above statement is NOT true. Their names do not start with "WS", they start with "txt".

What, exactly, are you trying to do?

It looks like you are trying to put the SAME text into all the ActiveX textboxes.

If that is indeed what you are trying to do, then say so. It should not be too difficult, although the use of textboxes like this is awful. Further, deleting Sections via code can be tricky, so I do understand your confusion.

clhare
02-19-2009, 01:43 PM
Yes, I do want to put the exact same text in every one of these textboxes. I agree--they are horrible, but we use them for special barcodes and I'm stuck with them. While they've always been easy to deal with using the code from my last message, they are a nightmare when there are more than I thought and I haven't accounted for them all in the macro, or when I remove a section and the ones I have accounted for aren't there anymore! If macro lists one that is no longer there, Word crashes!

clhare
02-24-2009, 09:31 AM
I just had an "A-ha" moment and remembered a similar macro that searches the document for any of these textboxes and then creates a separate document where it lists all the "names" of the textboxes that were found. I used that code and made a few small changes to it and it looks like it's working!

Here's what I ended up with:

Dim oSection As Word.Section
Dim oFooter As Word.HeaderFooter
Dim oShape As Word.Shape
Dim oActiveX As TextBox
' Replace text in each textbox in footers
For Each oSection In ActiveDocument.Sections()
For Each oFooter In oSection.Footers
For Each oShape In oFooter.Shapes
If oShape.Type = msoOLEControlObject Then
Set oActiveX = oShape.OLEFormat.Object
oActiveX.Text = str2DBarcode1
Set oActiveX = Nothing
End If
Next
Next
Next

I'm not sure what "Set oActiveX = Nothing" is used for. It's in the original code, so I didn't want to remove it.

fumei
02-24-2009, 10:17 AM
"I'm not sure what "Set oActiveX = Nothing" is used for. "

Don't worry about it. Technically, it is used to explicitly destroy/release the memory address assigned to the object.

Set oActiveX = oShape.OLEFormat.Object

VBA explicitly assigns an address block to that object

Set oActiveX = Nothing

VBA explicitly releases that address block

Whether it is fully needed, or not, is a debate that has been raging for some time.

VBA is reasonably well behaved, and IMO, regarding child objects of an application, you possibly can get away with ignoring Set object = Nothing. HOWEVER, I am of the opinion that application objects themselves (instances) should always be explicitly set to nothing when you are done.