|
-
Jan 2nd, 2010, 10:38 AM
#1
Thread Starter
Addicted Member
[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
-
Jan 2nd, 2010, 11:14 AM
#2
Re: Use Text Fields instead of Input Box
Are you doing this within the "VB Editor" inside Word?
-
Jan 2nd, 2010, 03:28 PM
#3
Thread Starter
Addicted Member
Re: Use Text Fields instead of Input Box
-
Jan 2nd, 2010, 03:35 PM
#4
Re: Use Text Fields instead of Input Box
You'll need to automate MS Word - searching this forum for something like "word automation" will return many pages with samples.
Here is one of them.
-
Jan 3rd, 2010, 03:15 AM
#5
Thread Starter
Addicted Member
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.
-
Jan 3rd, 2010, 09:43 AM
#6
Re: Use Text Fields instead of Input Box
 Originally Posted by irfi
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?? 
-
Jan 3rd, 2010, 10:14 AM
#7
Hyperactive Member
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"
-
Jan 3rd, 2010, 12:29 PM
#8
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|