Problem with UserForm Initialization

P

Patrick C. Simonds

I have tried Many variants of the code below, but to no avail. It is part of
a UserForm Initialization, designed to change OptionButton2 to true of there
is an X in Text Box 16 of the active document.


ActiveDocument.Shapes("Text Box 16").Select
Selection.ShapeRange.TextFrame.TextRange.Select

If Selection.Text = "X" Then
OptionButton2.Value = True

End If
 
H

Helmut Weber

Hi Patrick,

try

If Selection.Text = "X" & chr(13) Then

Still better not to select anything, like:

Dim s As String
s = ActiveDocument.Shapes(1).TextFrame.TextRange
' shapes(1) is just for my convenience
s = Left(s, Len(s) - 1) ' cut off paragraph mark if you like
if s = .................

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
P

Patrick C. Simonds

Thanks for your help. Here is the final code:

Dim s As String
s = ActiveDocument.Shapes("Text Box 16").TextFrame.TextRange
s = Left(s, Len(s) - 1) ' cut off paragraph mark if you like
If s <> x Then
OptionButton2.Value = True
End If


You also solved another problem I had not begun to address. That was when I
was populating the TextBoxes on my UserForm, I was getting the paragraph
mark:

s = ActiveDocument.Shapes("Text Box 217").TextFrame.TextRange
s = Left(s, Len(s) - 1) ' cut off paragraph mark if you like
TextBox1.Text = s
 
P

Patrick C. Simonds

Is there any way to alter the code so that it can place text into the Text
Box on the document? This would of course be a separate process. The code
you provided was it populate the UserForm during UserForm Initialization,
but it was so clean I am wondering if t would not do better than the code I
came up with to fill in the document. Here is a sample of what I came up
with. As you can see I do not know how to avoid using Select.

If OptionButton2.Value = True Then 'Mrs.
ActiveDocument.Shapes("Text Box 16").Select
Selection.Text = "X"
End If
 
H

Helmut Weber

Hi Patrick,

like this:

ActiveDocument.Shapes(1).TextFrame.TextRange.Text = "X"

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

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