Option Button to display Text

L

LittleAnn

Hi

I just have a simple question, but I cant seem to figure it out. I have a
letter template which I have created a userform through VBA to gather all the
relevant information from user before entering the main body of the text.

The only thing is I have an Option Button on the Userform that the User
clicks if they have enclosures to include with the letter but I cant get the
text "Encl.." to appear at the bookmark in the document itself once the user
as clicked that option.

How do I do this.

Thanks
 
G

Gordon Bentley-Mix

Ann,

In it's simplest form, something like this should work:

If optEnclosures.Value = True Then
ActiveDocument.Bookmarks("Enclosures").Range.Text = "Encl."
End If

This would go someplace in the code that builds the document. You may also
want to add some error handling to ensure that the bookmark exists before
trying to write into it.

BTW, is there any reason you are using an option button? Option buttons are
usually used in sets of 3 or more (and usually grouped in a Frame) when the
options are mutually exclusive. For simple "one-or-the-other" situations, a
checkbox is usually a better choice. In addition, a single option button is
problematic in that there is no easy way to "de-select" it once it's
selected. Checkboxes do not present the same problem.

The code for using a checkbox would be the same as the code above with the
name of the option button replaced by the name of the checkbox (i.e.
"chkEnclosures" instead of "optEnclosures").
-----
Cheers!

Gordon

Uninvited email contact will be marked as SPAM and ignored. Please post all
follow-ups to the newsgroup.
 
L

LittleAnn

Gordon

thank you so much for your reply, I think I am slowly getting there, I have
placed the code you gave me at the end of the command button code but am
getting an error message when try to get it working, the code I have is as
below, I know the problem is where I have your code placed but not sure of
where the exact place I should insert it (I have very basic VBA skills).
Thanks again for your help.

Private Sub CommandButton1_Click()
With ActiveDocument
.Bookmarks("Jobnumber").Range _
.InsertBefore JobNumber
.Bookmarks("AuthorIntials").Range _
.InsertBefore AuthorIntials
.Bookmarks("TypistIntials").Range _
.InsertBefore TypistIntials
.Bookmarks("FileRef").Range _
.InsertBefore FileRef
.Bookmarks("AddressBlock").Range _
.InsertBefore AddressBlock
.Bookmarks("Salutation").Range _
.InsertBefore Salutation
.Bookmarks("Subject").Range _
.InsertBefore Subject
.Bookmarks("Author").Range _
.InsertBefore Author
.Bookmarks("CCs").Range _
.InsertBefore CCs
.if chkEncl.Value = True Then _
ActiveDocument.Bookmarks("Enclosures").Range.Text = "Encl.."
End If


End With

UserForm1.Hide

End Sub
 
J

James

Ann,

You have a syntax error in your code, probably as a result of copying and
pasting Gordon's code.

It should be:

If (chkEncl.Value = True) Then
.Bookmarks("Enclosures").Range.Text = "Encl.."
End If

Or, since all your previous steps use .InsertBefore, you could write:

If (chkEncl.Value = True) Then
.Bookmarks("Enclosures").Range.InsertBefore "Encl..."
End If

This assumes that the name of the bookmark is "Enclosures" and that the
bookmark exists in the document.

James Coulter
 
L

LittleAnn

Hi James

Thanks for your reply I now have the below code built into the command
button of the User form but I get an error any time I test it and it has the
" Then " highlighted in red, is this the wrong statement for the code.

Thanks again

Code is now:
Private Sub CommandButton1_Click()
With ActiveDocument
.Bookmarks("Jobnumber").Range _
.InsertBefore JobNumber
.Bookmarks("AuthorIntials").Range _
.InsertBefore AuthorIntials
.Bookmarks("TypistIntials").Range _
.InsertBefore TypistIntials
.Bookmarks("FileRef").Range _
.InsertBefore FileRef
.Bookmarks("AddressBlock").Range _
.InsertBefore AddressBlock
.Bookmarks("Salutation").Range _
.InsertBefore Salutation
.Bookmarks("Subject").Range _
.InsertBefore Subject
.Bookmarks("Author").Range _
.InsertBefore Author
.Bookmarks("CCs").Range _
.InsertBefore CCs
.If (chkEncl.Value = True) Then _
.Bookmarks("Encl").Range _
.InsertBefore "Encl.."

End If

End With

UserForm1.Hide

End Sub
 
J

Jay Freedman

Remove the dot before the 'If' in that same line.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
J

Jay Freedman

Jay said:
Remove the dot before the 'If' in that same line.

Also remove the underscore from the end of that line (after 'Then'). It
should look like this:

If (chkEncl.Value = True) Then
.Bookmarks("Encl").Range _
.InsertBefore "Encl.."
End If
 
L

LittleAnn

Below is my coding I have and have changed the option Button to a check box
and it still is not working for me, should I be using the Initialize or
option explicit code also to get it to work????? Not too good with VBA,
still trying to learn :)

Private Sub CommandButton1_Click()
Dim oRng As Word.Range
Dim oBM As Bookmarks
Set oBM = ActiveDocument.Bookmarks
Set oRng = oBM("Jobnumber").Range
oRng.Text = JobNumber.Text
oBM.Add "Jobnumber", oRng
Set oRng = oBM("AuthorsIntials").Range
oRng.Text = AuthorsIntials.Text
oBM.Add "AuthorsIntials", oRng
Set oRng = oBM("TypistIntials").Range
oRng.Text = TypistIntials.Text
oBM.Add "TypistIntials", oRng
Set oRng = oBM("FileRef").Range
oRng.Text = FileRef.Text
oBM.Add "FileRef", oRng
Set oRng = oBM("AddressBlock").Range
oRng.Text = AddressBlock.Text
oBM.Add "AddressBlock", oRng
Set oRng = oBM("Salutation").Range
oRng.Text = Salutation.Text
oBM.Add "Salutation", oRng
Set oRng = oBM("Subject").Range
oRng.Text = Subject.Text
oBM.Add "Subject", oRng
Set oRng = oBM("Author").Range
oRng.Text = Author.Text
oBM.Add "Author", oRng
Set oRng = oBM("CCs").Range
oRng.Text = CCs.Text
oBM.Add "CCs", oRng
If (Encl.Value = True) Then
Set oRng = oBM("Encl").Range
oBM.Add "Encl", oRng
Me.Hide
End If

End Sub

Thanks again for your help.
 
J

Jay Freedman

Assuming that you changed the name of the check box from Checkbox1 to Encl, and
also assuming that when the check box is checked you want the code to insert the
abbreviation "Encl." at the bookmark named "Encl", then you need to change these
lines near the end of your code
If (Encl.Value = True) Then
Set oRng = oBM("Encl").Range
oBM.Add "Encl", oRng
Me.Hide
End If

to this:

If (Encl.Value = True) Then
Set oRng = oBM("Encl").Range
oRng.Text = "Encl."
End If
Me.Hide

Here are some things to notice:

- The line Me.Hide should not be between the If and the End If. You want the
userform to hide (which causes control to return to the macro that called the
userform) regardless of whether the check box is checked or not. The way you had
it before, if the check box wasn't checked then the userform would never go
away.

- Proper indenting of code (for example, the lines between If and End If) helps
greatly in seeing how the flow of execution will go. When everything is jammed
against the left margin, it's much harder to understand.

- The line oBM.Add "Encl", oRng in your code would create a bookmark named
Encl that covers the range oRng -- but there is already a bookmark with that
name and range, so this line effectively does nothing. Instead, you need the
line that assigns the text "Encl." to oRng.Text.

- The Option Explicit statement doesn't have anything to do with why your macro
isn't working. It causes VBA to show an error message if you try to use a
variable name that hasn't been defined by a Dim statement (or, for certain
purposes, by a Public statement or a Private statement, but those are more
advanced than you need). Read
http://www.word.mvps.org/FAQs/MacrosVBA/DeclareVariables.htm to find out about
this.

- The procedure named UserForm_Initialize also doesn't have anything to do with
why your macro isn't working. It runs when the userform is first prepared to
show on the screen. In your case, you don't have anything that needs to be done
at that time, so you don't need the procedure at all.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 
L

LittleAnn

Works perfectly!!!! Thanks a million you explained everything alot easier
for me to understand.

Thanks really appreciate it.
 

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