• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

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
0
gixxer1020
Asked:
gixxer1020
  • 8
  • 4
1 Solution
 
pauloaguiaCommented:
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
0
 
gixxer1020Author Commented:
Can I place the text in a specific place, like a bookmark or something similar?
0
 
pauloaguiaCommented:
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
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
pauloaguiaCommented:
Hi Edwin, any progress here?
0
 
gixxer1020Author Commented:
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

0
 
pauloaguiaCommented:
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).
0
 
pauloaguiaCommented:
Actually that replaces the text but deletes the bookmark as well. A weird approach but that seems to work is replace the first part of the text and then delete the last character:

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


Hope this helps

Paulo
0
 
gixxer1020Author Commented:
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
0
 
pauloaguiaCommented:
>>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
0
 
gixxer1020Author Commented:
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
0
 
pauloaguiaCommented:
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
0
 
pauloaguiaCommented:
Glad you could work it out. Thanks for the grade. :)
0
 
vdhavalaCommented:
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!!
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 8
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now