Link to home
Start Free TrialLog in
Avatar of gixxer1020
gixxer1020Flag for United States of America

asked on

Form to Word Document

I have created a custom Form containing textboxes in VBA and need to transfer the information that will be input on the Form to the Word document it's attached to? How??

thanx

edwin
Avatar of pauloaguia
pauloaguia
Flag of Portugal image

You mean something like

Private Sub CommandButton1_Click()
    Dim rng As Range
   
    Set rng = ThisDocument.Range(0)
    rng.Text = TextBox1.Text
    ThisDocument.Paragraphs.Add rng
    Set rng = Nothing
    Hide
End Sub

?
This places the text at the begining of the document replacing everything else...

Hope this helps

PAulo
Avatar of gixxer1020

ASKER

Can I place the text in a specific place, like a bookmark or something similar?
This will add the text to the beggining of the bookmark:

Private Sub CommandButton1_Click()
    Dim rng As Range
    With ThisDocument.Bookmarks("bookmarkName")
        Set rng = ThisDocument.Range(.Start, .Start)
    End With
    rng.Text = TextBox1.Text
   
    Set rng = Nothing
    Hide
End Sub


Sorry if the examples are not the best. I'm used to Excel and the object model is slighlty different. I'm taking the opportunity to learn how to do this in Word. But if you ask for something very specific I'm sure I'll be able to get there.

Paulo
Hi Edwin, any progress here?
Paulo,

The code works great, it places the item right at the beginning of the bookmark. Now all I need is for it to replace what is already in the bookmark. In other words, when I change the data in the form, currently it is adding the new data to the beginning of what was previously placed in the document through the form. I have played around a bit with the

        Set rng = ThisDocument.Range(.Start, .Start)

part of your code but have been unsuccesful thus far.
Any additional help is greatly apprexciated.

thanx
edwin

Just a wild guess: have you tried
ThisDocument.Range(.Start,.End)
? (you probably have, just checking...)
Anyway, I'll take a look at this later tonight - about 4h-5h from now (I'm currently at the office and can't spend much time on this right now).
ASKER CERTIFIED SOLUTION
Avatar of pauloaguia
pauloaguia
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Paulo,

I just used your

Set rng = ThisDocument.Range(.Start, .End - 1)

It leaves a blank space at the end of the bookmark but replaces everything ewlse inside it. Now I just need the Form to hold & save the information that was last input into it.

thanx
edwin
>>It leaves a blank space at the end of the bookmark
That's probably because the bookmark has a space at the end. For the last char to be removed you need to use the second part of the function as well (that removes the last char). At least for me it worked.

>>I just need the Form to hold & save the information that was last input into it
You mean you don't want the form to disappear? Just remove that "Hide" line from the end of the clic event of the button. Not sure what you mean about that part about saving... you mean if an year from now you open that form again it should show you the same value? Or the value of whatever is in the bookmark (considering you can change it by hand, for instance).

Paulo
I would like the it if if a year from now you open that form again it should show you the same value. No one will have the ability to edit the document so whatever is in the bookmarks should always be what is in the Form

thanx
In that case it's just a matter of filling in the text box by using the Initialize event of the form. Something like

Sub UserForm_Initialize()
    TextBox1.Text = ThisDocument.Range(.Start, .End).Text
End Sub

Paulo
Glad you could work it out. Thanks for the grade. :)
Avatar of vdhavala
vdhavala

Hi!

I have the same requirement and was successfully able to add the values from the userform onto my document. But I have the same problem that edwin had..."The code works great, it places the item right at the beginning of the bookmark. Now all I need is for it to replace what is already in the bookmark. In other words, when I change the data in the form, currently it is adding the new data to the beginning of what was previously placed in the document through the form. "

I tried with

With ThisDocument.Bookmarks("bookmark1")
            Set rng = ThisDocument.Range(.Start, .End - 1)
        End With
        txt = Me.Controls("txtCriteria" & i).Value & vbCrLf
        rng.Text = txt
        With ThisDocument.Bookmarks("bookmark1")
            Set rng = ThisDocument.Range(.End - 1, .End)
        End With

but I get the error:
"Value out of Range"

Any thoughts on how I can resolve this? I need the word template to have just the values in the userform and not append each time..

Thanks!!