Locating a specific text box in a section.

R

Robin

I have a section 1 of a document that contains 2 text boxes:

- Text box 1 contains data fields
- Text box 2 contains other data fields in a table.

I want to toggle text box 2 so I can display or hide it via a macro. But how
do I locate that specific text box (2)? I canm easily hode/show all the text
boxes in one operation, but I don't want to ever hide text box 1?

thanks
Robin
 
G

Greg Maxey

Select and Name the textbox:
Sub ScratchMaco()
Selection.ShapeRange.Name = "TextBoxToBeHidden"
End Sub

Hide a named shape with code:

Sub ScratchMacoII()
ActiveDocument.Shapes("TextBoxToBeHidden").Visible = msoFalse
End Sub
 
R

Robin

Hi Greg,

I thought about doing that, but it measn I have to on-the-fly select and
name the text box every time I want to hide it - correct? Then the issue is
how do I select just that text box programatically, as I see it I am shifting
the problem of getting the correct shape index fpom deletion to selection? I
would expect that I could delete the textbox by name if I could name it using
the standard Word application interface and not via VBA, but the properties
don't allow that (as far as I can see).

thanks
Robin
 
G

Greg Maxey

Robin,

No you would not have to reselect it each time. You would name the textbox
shape only once when you create it using the first macro. After it is named
you could use the second procedure to set the .visible attribute to the
named shape repeatedly
 
R

Robin

Greg I think we are looking at it from two different aspects. The text box is
inserted into a header via a macro. I have no way of actually selecting this
text box to give it a name - it's inserted by the maco but the text box is a
via building block itself. It's as if I am in a catch 22 situation.

Robin
 
G

Greg Maxey

Add it and name it with code then show and hide as you wish:

Sub ScratchMaco()
Dim oTB As Shape
With ActiveDocument.Sections(1)
Set oTB = .Headers(wdHeaderFooterPrimary). _
Shapes.AddTextbox(Orientation:=msoTextOrientationHorizontal, _
Left:=50, Top:=50, Width:=100, Height:=50, _
Anchor:=.Headers(wdHeaderFooterPrimary).Range)
End With
oTB.Name = "MyTBToHideOrShowAtWill"
End Sub
Sub ScratchMaco2()
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary). _
Shapes("MyTBToHideOrShowAtWill").Visible = msoFalse
End Sub
Sub ScratchMaco3()
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary). _
Shapes("MyTBToHideOrShowAtWill").Visible = msoTrue
End Sub
 
R

Robin

OK Greg, that works. Now I have added a table into the text box and included
the text box (with its table) into the text Box Gallery as a Building Block.
I need to use a text box here because the table must "float" on the page.
When the building block is applied the text box doesn't have any name that I
specify, because I'm no longer creating it, and doing a gweneric text box
hide will also hide those text boxes that should not be hidden, so I'm in a
bit of a bind here ... :-(

thanks
Robin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top