Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hide Show form field in Word via VB 2

Status
Not open for further replies.

remeng

Technical User
Jul 27, 2006
504
0
16
US
Hi All;

I have been struggling with a very small issue with a form I am creating in Word 2010. This is most likely a quick element error.

Problem:

When a radio button is selected in a Word 2010 form, I want to hide or show a field based on this selection.

Current Steps Taken:

1) Set form field bookmark name to Return_Address
2) Created code with if then statement to control a "locked" form box. (working correctly)

Pack_Return = radio button
Pack_Lost = Radio Button
Pack_Both = Radio Button
TexBox1 = locked text box (no data entry allowed and working)
Return_Address = Form Field (named via bookmark command not working)

How can I show Return_Address and allow data entry when Pack_Return is selected and hide it and lock it blank when the other two radio buttons are selected. I know that there is a syntax error in the code.

Thanks,

Mike

Code:
Private Sub Pack_Return_Click()

Call TextBox1_Change

End Sub

Private Sub Pack_Lost_Click()

Call TextBox1_Change

End Sub

Private Sub Pack_Both_Click()

Call TextBox1_Change

End Sub

Private Sub TextBox1_Change()

 Dim message As String

        If Pack_Return = True Then
                TextBox1 = "TO BE CONFIRMED BY SUPPLIER"
            
                TextBox1.BackColor = &HFFFF&
                Bookmarks("Return_Address").Hidden = True   '<--- Know code error.  Need solution for this
                
            
        Else:   TextBox1 = "N / A"
                TextBox1.BackColor = &HFFFFFF
                
                Return_Address.ShowHidden = False  '<--- Know code error.  Need solution for this

Application.ScreenUpdating = True

End If
        
End Sub
 
Hi Mike,

you are close. But: "Bookmarks" is only defined if you specify which document you are querying, and "Hidden" is a property of the Font object which is accessible through Range:

Code:
ActiveDocument.Bookmarks("Return_Address").Range.Font.Hidden = True

Best,
MakeItSo

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Thank you. That was the answer.
 
Don't you think MakeItSo deserves a star for the help provided....? [ponder]

Have fun.

---- Andy

There is a great need for a sarcasm font.
 
Thanks to both of you! [thumbsup2]

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Hi Guys. Ok something is a little screwy now. The show / hide isn't working.

So I had to rebuild the document due to issues in another section.

I created the bookmark AppendixD. Under Advanced Options I turned on Show Bookmarks. I can see that the bookmark was properly created. Also, when I use jump to bookmark it goes to the correct place.


I used the same code as before:

Code:
ActiveDocument.Bookmarks("AppendixD").Range.Font.Hidden = True

I then used the refresh code to update the page:

Code:
Application.ScreenUpdating = True

All of the code

Code:
Private Sub test2()

ActiveDocument.Bookmarks("AppendixD").Range.Font.Hidden = True

Application.ScreenUpdating = True


End Sub

Where did I mess up?

Thanks!

Mike
 
Ok so here is a status update. This is stranger I think.

so I added some code to assist in testing this issue:

Code:
Private Sub test2()

ActiveDocument.Bookmarks("AppendixD").Range.Font.Hidden = True

'ActiveDocument.Bookmarks("AppendixD").Range.Font.Hidden = False

ActiveDocument.TablesOfContents(1).Update

Application.ScreenUpdating = True


End Sub

The Table of Contents is showing the change and properly updating, but the actual text is not being hidden.

Any ideas?

Mike
 
Simply toggling a font's hidden attribute doesn't guarantee whatever you've hidden won't be seen or, even if you can't see it, that it won't be printed.

You can toggle the on-screen visibility via the ¶ symbol on the Ribbon's home tab (or via a macro), but you'd also need to toggle the 'print hidden text' option also to guard against the content being printed. The downside is that making those changes affects all Word documents, so you'd need to restore the settings when you're done and/or the user switches to another document. It's another whole can of worms when multiple documents are open...

Ultimately, to manage this properly, you need to scrap the hidden-formatting idea and conditionally insert/delete the formfield. That way, there's no messing with Word settings and the like. For code to do that kind of thing, see:
Cheers
Paul Edstein
[MS MVP - Word]
 
Hi Paul,

This process has worked on multiple documents even if multiple documents are open. My solution does work since when I print the document the correct sections are hidden. The screen is not refreshing the document though. This is the current issue.

Mike
 
Hi Mike,

what Paul is referring to is this:
Do you see the hidden texts with a fine dotted underline?
That would mark them as hidden. In that case simply click on File => Options => Display/View, uncheck the "HIdden Text" checkbox. That should solve your display issue.

MakeItSo

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Oh my God you Guys Rock!!!

What would cause that check box to be enabled? Previously it wasn't checked. Any ideas?

Thank you so much!

Mike
 
Not sure but I have seen this happen occasionally with Font.Hidden being set via macro.
I was never able to consistently repeat the behaviour so I am not sure about the cause.
It should be enough to add this line just before re-enabling ScreenUpdating:
Code:
ActiveDocument.ActiveWindow.View.ShowAll = False

Cheers,
MakeItSo

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
How can I activate the
Code:
 ActiveDocument.ActiveWindow.View.ShowAll = False
when the document opens? I want to prevent all users from having to go through the pain I did.

Thanks,

Mike
 
Create a sub "AutoOpen" and put it there.
Code:
Sub AutoOpen()
ActiveDocument.ActiveWindow.View.ShowAll = False 
End Sub

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
[/quote]This process has worked on multiple documents even if multiple documents are open. My solution does work since when I print the document the correct sections are hidden. The screen is not refreshing the document though. This is the current issue.[/quote]
All that means is that, in the past, you have had Word configured to not show hidden text but lately it hasn't been.
In that case simply click on File => Options => Display/View, uncheck the "HIdden Text" checkbox. That should solve your display issue.
As would toggling the on-screen visibility via the ¶ symbol on the Ribbon's home tab - which is what I suggested in my previous reply.

Regardless, while this might have worked to date for the OP, it just goes to demonstrate the point I made about its unreliability.
How can I activate the
Code:
ActiveDocument.ActiveWindow.View.ShowAll = False
when the document opens? I want to prevent all users from having to go through the pain I did.
Foisting such changes to Word's settings upon anyone who uses your document is rude. You stand to incur considerable opprobrium when people realise you're messing with how they use Word. I have given you a link to an implementation that avoids any such changes to Word's settings. Ignore it at your peril.

Cheers
Paul Edstein
[MS MVP - Word]
 
Hi Paul,

I agree. You are absolutely correct in your recommendation. Is there a way to capture their setting, save it, change it for this document only, then on close revert it to the previous setting?

I am not sure if I will even use the code, but it is something for me to learn about.

Thanks!

Mike
 
It's not just a matter of capturing for your document and restoring the settings when they exit (for which you could use a Document_Close macro). You should also be restoring their settings whenever they switch to another document without exiting (then reverting again if they switch back to your document). The coding for this is quite involved - requiring the creation and activation of a class module to deploy Word's WindowActivate and WindowDeactivate events and testing which document is active - and is far more complex than the code in the link I gave you.

Cheers
Paul Edstein
[MS MVP - Word]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top