?
Solved

Form to Word Document

Posted on 2003-03-19
13
Medium Priority
?
228 Views
Last Modified: 2008-02-01
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
Comment
Question by:gixxer1020
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
13 Comments
 
LVL 9

Expert Comment

by:pauloaguia
ID: 8169394
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
 

Author Comment

by:gixxer1020
ID: 8169534
Can I place the text in a specific place, like a bookmark or something similar?
0
 
LVL 9

Expert Comment

by:pauloaguia
ID: 8169666
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 9

Expert Comment

by:pauloaguia
ID: 8234323
Hi Edwin, any progress here?
0
 

Author Comment

by:gixxer1020
ID: 8246355
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
 
LVL 9

Expert Comment

by:pauloaguia
ID: 8246385
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
 
LVL 9

Accepted Solution

by:
pauloaguia earned 200 total points
ID: 8248527
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
 

Author Comment

by:gixxer1020
ID: 8255869
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
 
LVL 9

Expert Comment

by:pauloaguia
ID: 8256123
>>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
 

Author Comment

by:gixxer1020
ID: 8258118
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
 
LVL 9

Expert Comment

by:pauloaguia
ID: 8259750
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
 
LVL 9

Expert Comment

by:pauloaguia
ID: 8329190
Glad you could work it out. Thanks for the grade. :)
0
 

Expert Comment

by:vdhavala
ID: 22745457
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question