PDA

View Full Version : [SOLVED:] Active X textbox control



Chunk
10-01-2015, 10:31 AM
I have a userform that the user will fill out a text box:

tbox_Title

When the command button is pressed, on the userform, a Word document opens that contains the following Active X textbox:

doc_RR_tbox_Title

I cannot get the text, from the userform, to populate the Word doc textbox.

Any ideas?

Here is the code I am trying to use:

Set wrdApp = CreateObject("Word.Application")
Set wrdNNDF = wrdApp.Documents.Open("C:\Review.doc")

wrdNNDF.Activate
'wrdApp.Visible = True
wrdApp.Selection.Find.ClearFormatting
wrdApp.Selection.Find.Replacement.ClearFormatting

FindWhat = Array("AAA", "CCC", "DDD")
ReplaceWith = Array(tbox_Location.Text, tbox_Date.Text, tbox_Comment.Text)

For i = 0 To 2
With wrdApp.Selection.Find
.Text = FindWhat(i)
.Replacement.Text = ReplaceWith(i)
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
wrdApp.Selection.Find.Execute Replace:=wdReplaceAll
Next i

''''''this is where it fails
doc_RR_tbox_TGI.Text = TESTMASTER.tbox_Title.Text


wrdNNDF.PrintOut
wrdNNDF.Close SaveChanges:=wdDoNotSaveChanges
wrdApp.Quit
Unload Me
End Sub

Thanks in advance.

Chunk

SamT
10-01-2015, 12:11 PM
You liked that little double Array loop, eh?

I don't know Word, but try either


.doc_RR_tbox_TGI.Text = TESTMASTER.tbox_Title.Text 'Note leading dot

Or


wrdNNDF.doc_RR_tbox_TGI.Text = TESTMASTER.tbox_Title.Text

gmayor
10-01-2015, 09:35 PM
I assume that this code is run from Excel and that the userform name is TESTMASTER and the code is the code associated with the command button? In which case the following does work. (You will need to change the command button name).

Note that it works much faster if it doesn't have to open Word, so I have added code to use the open version if available. The code uses late binding to Word so it doesn't require a reference to the Word object library. I don't think that personally I would use DOC format or an Active X text box, but you may not have the option to change.


Private Sub CommandButton1_Click()
Dim wrdApp As Object
Dim wrdNNDF As Object
Dim FindWhat As Variant
Dim ReplaceWith As Variant
Dim i As Long
Dim bStarted As Boolean

Me.Hide
On Error Resume Next
Set wrdApp = GetObject(, "Word.Application")
If Err Then
Set wrdApp = CreateObject("Word.Application")
bStarted = True
End If
On Error GoTo 0
wrdApp.Visible = True

Set wrdNNDF = wrdApp.Documents.Open("C:\Review.doc")
wrdNNDF.Activate
'wrdApp.Visible = True
wrdApp.Selection.Find.ClearFormatting
wrdApp.Selection.Find.Replacement.ClearFormatting

FindWhat = Array("AAA", "CCC", "DDD")
ReplaceWith = Array(Me.tbox_Location.Text, Me.tbox_Comment.Text, Me.tbox_Comment.Text)

For i = 0 To 2
With wrdNNDF.Range.Find
.Text = FindWhat(i)
.Replacement.Text = ReplaceWith(i)
.Forward = True
.Wrap = 1
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=2
End With
Next i

wrdNNDF.doc_RR_tbox_TGI.Text = TESTMASTER.tbox_Title.Text

wrdNNDF.PrintOut
wrdNNDF.Close SaveChanges:=0
If bStarted Then wrdApp.Quit
Unload Me
End Sub

Chunk
10-05-2015, 09:51 AM
gmayor,

That worked perfect. Thanks for the error addition, that was a pain.