PDA

View Full Version : Search For a Textbox



heedaf
03-08-2016, 10:44 AM
I'm using word 2010. I've got a textbox at the top of each page that needs to be updated for what is changed on THAT page. I've tried using Find.execute method but it is a hit or miss if it finds the textbox. So what I need is a macro that when something is changed on a page it will go to the textbox at the top of the page and change the text in it. Is there a way to tie in range find method with shapes?
Can someone point me in the right direction?

gmaxey
03-08-2016, 06:33 PM
What kind of textbox?

heedaf
03-09-2016, 03:16 PM
Not sure! I select the textbox option fron the insert menu.

gmaxey
03-09-2016, 07:57 PM
I don't know how you expect a macro to know what text on a page was changed, but to write text to a textbox you can name it and then write to the named shape:


Sub ScratchMacro1()
'Select the textbox and give it a name.
Selection.ShapeRange(1).Name = "Page 1 Textbox"
lbl_Exit:
Exit Sub
End Sub
Private ScratchMacro2()
'Now you can call it by name and write text to it.
ActiveDocument.Shapes("Page 1 Textbox").TextFrame.TextRange.Text = "Put this text in the box"
lbl_Exit:
Exit Sub
End Sub

heedaf
03-09-2016, 08:41 PM
Sorry! I've got a macro that will change a line on a page so I want add to this macro that checks the text box at the top of the current page and if it has the wrong value update it to the correct value. My problem is that I can't figure out an easy way to find the text box at the top of the current page. I could name it as gmaxey stated above but if "Page 1 Textbox" changes and is on page 2 that could be a problem. I realize shapes are in not really tied to a page but I would really hate to search through 700 pages of shapes to find the right one. Is there something that will tell me what page a textbox is on? That way I could easily go find the correct textbox? I need to have something in the header area that is specific to that page and a textbox is the only thing I can think of that will work. I'm open to any suggestions.

gmaxey
03-11-2016, 05:51 AM
Maybe something like:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oPage As Page
Dim oRec As Rectangle
Dim oShp As Shape
For Each oPage In ActiveWindow.ActivePane.Pages
For Each oRec In oPage.Rectangles
If oRec.RectangleType = wdShapeRectangle Then
Set oShp = oRec.Range.ShapeRange(1)
With oShp
If .Type = msoTextBox Then
If 1 <> 2 Then 'Your erroneous conditions
.TextFrame.TextRange.Text = "Your corrected text"
End If
End If
End With
End If
Next
Next
lbl_Exit:
Exit Sub
End Sub