Consulting

Results 1 to 4 of 4

Thread: Active X textbox control

  1. #1
    VBAX Regular
    Joined
    Feb 2015
    Posts
    79
    Location

    Active X textbox control

    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
    Last edited by Chunk; 10-01-2015 at 10:55 AM.

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    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
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  4. #4
    VBAX Regular
    Joined
    Feb 2015
    Posts
    79
    Location
    gmayor,

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •