I am trying to place text that is located in a textbox in a userform, in
front of a bookmark in a Word document. I am having compiler issues with
regard the syntax portion of the range statement.
Private Sub CommandButton1_Click()
With ActiveDocument
.Bookmarks("Text1").Range_
.InsertBefore TextBox1
.Bookmarks("Text2").Range_
.InsertBefore TextBox2
End With
UserForm1.Hide
End Sub
Jean-Guy Marcil - 01 Mar 2007 20:16 GMT
tm brown was telling us:
tm brown nous racontait que :
> I am trying to place text that is located in a textbox in a userform,
> in front of a bookmark in a Word document. I am having compiler
[quoted text clipped - 11 lines]
>
> End Sub
What is TextBox1? A variable? A control?
If it is a control, It is safer to prefix all references to controls and not
to use default properties (for future compatibility.)
So, you should have Me.TextBox1.Text and Me.TextBox2.Text.
Also, you need a space before the underscore:
.Range _
Finally, do you have an "Unload" statement somewhere in your code (for the
userform)?

Signature
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org
Greg Maxey - 01 Mar 2007 23:40 GMT
Dan,
Suggest you use a complete line and the Me reference as JGM mentioned:
.Bookmarks("Text1").Range.InsertBefore Me.TextBox1
Personnally I avoid the .InsertBefore method and always write the text "in"
the bookmark.
Private Sub CommandButton1_Click()
Dim oBMs As Bookmarks
Dim oRng A Word.Range
Set oBMs = ActiveDocument.Bookmarks
Set oRng = oBMs("Text1").Range
oRng.Text = Me.TextBox1
ActiveDocument.Bookmarks.Add "Text1", oRng
Set oRng = oBMs("Text2").Range
oRng.Text = Me.TextBox2
ActiveDocument.Bookmarks.Add "Text2", oRng
Set oRng = Nothing
Me.Hide 'or Unload Me if you don't have a unload statement elsewhere

Signature
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.
>I am trying to place text that is located in a textbox in a userform, in
> front of a bookmark in a Word document. I am having compiler issues with
[quoted text clipped - 11 lines]
>
> End Sub