Results 1 to 8 of 8

Thread: [RESOLVED] Use Text Fields instead of Input Box

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2008
    Posts
    170

    Resolved [RESOLVED] Use Text Fields instead of Input Box

    Hi Everyone,
    Can anyone help me to use Textbox fields instead of Input box. Here is the line of code that accepts values from the user by poping an inputbox. The number of times the input box pops-up depends on the number of docvariables that i have declared inside my word documents.
    I have declared 2 docvariables (Name and Age) on my ms word temple.

    Please note that this is not the complete code but if required i will post it here.

    Code:
    VariableValue = InputBox("Enter value for: " & Variable)
    AddNewVariable Variable, VariableValue
    With the above code the Input box pops up and ask for the value of Name.
    Then again it pops up and ask for the value of Age.
    Then it places these two values in the word document exactly where i declared the docvariables.

    But i want to use the fieds on my form. Ok i will explain a bit further!!

    I have a form and have two text fields (Name and Age). Now all i want to do is that what ever i type inside these fields, these values are placed inside my word document.

    Thanx a lot
    Cheers!!!!!!
    irfi

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Use Text Fields instead of Input Box

    Are you doing this within the "VB Editor" inside Word?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2008
    Posts
    170

    Re: Use Text Fields instead of Input Box

    No actually from vb6...

  4. #4

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2008
    Posts
    170

    Re: Use Text Fields instead of Input Box

    Automate MS-Word????

    OK Let me post the entire code which is working fine and my only problem is to use textfields on my vb6 form instead of inputbox.

    If anyone wishes to try this code please first create ms word file and copy these two docvariables anywhere on the page.
    1- {DOCVARIABLE"Name"\*MERGEFORMAT}
    2- {DOCVARIABLE"Age"\*MERGEFORMAT}
    Next press Alt+F9 to hide these docvariables on the page and save the file as irfi.doc in the vb6 application folder.

    Now paste the following code in your VB6 form. Please create a Button on the Form.

    Next When you Run the code it will pop-up two inputbox asking for the value for Name and Age. Once you put the value and click OK it will display message "Finished". Just go to your vb6 folder and you will find a new ms word file named "irfidoc.doc". Just open this file and you will find the values of Name and Age there....

    Code:
    Option Explicit
    Private UsedVariables() As String
    
    Private Sub Command1_Click()
        FillTemplates
    End Sub
    
    Private Sub FillTemplates()
        Dim WordApp As Word.Application
        Dim WordDoc As Word.Document
        Dim i As Integer, j As Integer
        Dim NewResult As String
        
        On Error GoTo ErrHandler
        
        ReDim UsedVariables(0)
        
        Set WordApp = CreateObject("Word.Application")
        Set WordDoc = WordApp.Documents.Open(App.Path & "\irfi.doc")
        
        ' In main body
        Debug.Print "Fields in main body: " & WordDoc.Fields.Count
        For i = 1 To WordDoc.Fields.Count
                
            If WordDoc.Fields(i).Type = wdFieldDocVariable Then
        
                ' Get the text for the field from the user
                NewResult = GetNewResult(WordDoc.Fields(i), WordDoc)
                'Insert New Text into the field
                If NewResult <> "" Then
                    WordDoc.Fields(i).Result.Text = NewResult
                    'WordDoc.Fields(i).Result.Font.Size = 22
                End If
            End If
        Next
        ' lock the document to stop changes
        WordDoc.Protect wdAllowOnlyComments, , "jd837djh82"
        WordDoc.SaveAs App.Path & "\irfidoc.doc"
        WordDoc.Close
        WordApp.Quit
        Set WordDoc = Nothing
        Set WordApp = Nothing
    
        MsgBox "Finished!"
    
    Exit Sub
    ErrHandler:
        
        MsgBox "Unhanled Error: " & Err.Description
    
    End Sub
    
    Private Function GetNewResult(wField As Word.Field, WordDoc As Word.Document) As String
    
        Dim StopPos As Long
        Dim Variable As String
        Dim UsedVariable As String
        Dim VariableValue As String
        Dim wRange As Word.Range
        
        Debug.Print wField.Code
        
        ' These three lines strip down the field code to find
        ' out it's name
        StopPos = InStrRev(wField.Code, "\*")
        Variable = Left(wField.Code, StopPos - 3)
        Variable = Right(Variable, Len(Variable) - 14)
        
        ' Check this field hasn't already appeared in this
        ' document.
        If CheckUsedVariable(Variable) Then
                      
            VariableValue = GetVariableValue(Variable)
            
        Else
                       
                    ' Get the value of the field from the user
                 VariableValue = InputBox("Enter value for: " & Variable)
    
                  AddNewVariable Variable, VariableValue     
        End If       
    
    ' I WANT TO  REPLACE THE ABOVE LINE OF CODE WITH:
    ' VariableValue = Text1.text  ' FOR NAME   
    'VariableValue = Text2.text   ' FOR AGE
    'BUT IN THE MS WORD IT PUTS THE VALUE OF TEXT2.TEXT ON BOTH DOVARIABLES i.e Name and Age. 
        
        GetNewResult = VariableValue
            
    End Function
    
    Private Function GetVariableValue(Variable As String) As String
    Dim i As Integer
    
        For i = 0 To UBound(UsedVariables)
            If Left(UsedVariables(i), Len(Variable)) = Variable Then
                GetVariableValue = Right(UsedVariables(i), Len(UsedVariables(i)) - Len(Variable))
                Exit For
            End If
        Next
        
    End Function
    
    Private Sub AddNewVariable(Variable As String, TheValue As String)
    Dim ArraySize As Integer
    
        ArraySize = UBound(UsedVariables)
        ReDim Preserve UsedVariables(ArraySize + 1)
        UsedVariables(ArraySize) = Variable & TheValue
    
    End Sub
    
    Private Function CheckUsedVariable(Variable As String) As Boolean
    Dim i As Integer
    
        For i = 0 To UBound(UsedVariables)
            If Left(UsedVariables(i), Len(Variable)) = Variable Then
                CheckUsedVariable = True
                Exit For
            End If
        Next
        
    End Function
    Last edited by irfi; Jan 3rd, 2010 at 03:18 AM.

  6. #6
    PowerPoster CDRIVE's Avatar
    Join Date
    Jul 2007
    Posts
    2,620

    Re: Use Text Fields instead of Input Box

    Quote Originally Posted by irfi View Post
    Automate MS-Word????

    ' I WANT TO REPLACE THE ABOVE LINE OF CODE WITH:
    ' VariableValue = Text1.text ' FOR NAME
    'VariableValue = Text2.text ' FOR AGE
    'BUT IN THE MS WORD IT PUTS THE VALUE OF TEXT2.TEXT ON BOTH DOVARIABLES i.e Name and Age.
    [/CODE]
    Have you considered declaring VariableValue as an array?
    Code:
    VariableValue(0) = Text1.text  ' FOR NAME   
    VariableValue(1) = Text2.text   ' FOR AGE
    <--- Did someone help you? Please rate their post. The little green squares make us feel really smart!
    If topic has been resolved, please pull down the Thread Tools & mark it Resolved.


    Is VB consuming your life, and is that a bad thing??

  7. #7
    Hyperactive Member jp26198926's Avatar
    Join Date
    Sep 2008
    Location
    General Santos City, Philippines
    Posts
    310

    Re: Use Text Fields instead of Input Box

    hehee how bout like this..?

    Code:
    Private Function GetNewResult(wField As Word.Field, WordDoc As Word.Document) As String
    
        Dim StopPos As Long
        Dim Variable As String
        Dim UsedVariable As String
        Dim VariableValue As String
        Dim wRange As Word.Range
        
        Debug.Print wField.Code
        
        ' These three lines strip down the field code to find
        ' out it's name
        StopPos = InStrRev(wField.Code, "\*")
        Variable = Left(wField.Code, StopPos - 3)
        Variable = Right(Variable, Len(Variable) - 14)
        
        ' Check this field hasn't already appeared in this
        ' document.
        If CheckUsedVariable(Variable) Then
                      
            VariableValue = GetVariableValue(Variable)
            
        Else
                 ' Get the value of the field from the user
                 'VariableValue = InputBox("Enter value for: " & Variable)
           
                 If Variable = "Name" Then
                    VariableValue = Text1.Text
                 ElseIf Variable = "Age" Then
                    VariableValue = Text2.Text
                 End If
    
                 AddNewVariable Variable, VariableValue
        End If
    
    ' I WANT TO  REPLACE THE ABOVE LINE OF CODE WITH:
    ' VariableValue = Text1.text  ' FOR NAME
    'VariableValue = Text2.text   ' FOR AGE
    'BUT IN THE MS WORD IT PUTS THE VALUE OF TEXT2.TEXT ON BOTH DOVARIABLES i.e Name and Age.
        
        GetNewResult = VariableValue
            
    End Function
    "More Heads are Better than One"

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Nov 2008
    Posts
    170

    Re: Use Text Fields instead of Input Box

    Thanx jp26198926!!! That solved it...I have rated your post.
    Thanx and Cheers
    irfi

Posting Permissions

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



Click Here to Expand Forum to Full Width