PDA

View Full Version : Mutiple Documents Opened



Scooby Doo
10-28-2010, 01:36 PM
I created a document with a User Form attached to it. When the user double clicks on this file, it will create a predefined document and it will show a form that asks the user to enter some information. Once all of the information had been entered into the form, the user would then click on the print button which would take all of the information entered into the User Form and place them into the predefined document at the bookmark locations defined in the predefined document. After this, the predefined document with all of the inputted information would then be sent to the printer for printing.

In a situation when the user has one or more documents already opened and then the user clicks the document with the User Form attached to it, Word will create the new predefined document. Now, there are at least two documents opened. When the user enters all of the required information in the User Form and then click the print button, the user will get a message telling the user that the information can't be transcribed to the predefined document. I assume this problem is due to the fact that the user has several documents opened and Word doesn't know which document to put the entered information in. How can I use VBA to direct Word to place the information in the correct document when the user has several documents open.


Thanks.



:rotlaugh::rotlaugh:

fumei
10-29-2010, 10:15 AM
Fairly easily. And is an example of good practice. I do not know your exact code, but in the template (I think it is a template), it seems its Document_New also displays the userform.

"When the user double clicks on this file, it will create a predefined document and it will show a form that asks the user to enter some information."

OK. Then in Document_New (which creates a new document), make the new document a document object. Once that is done you can ALWAYS refer to it directly. No matter how many other documents are open, and better yet - the document does NOT have to be active. Important though, make it a PUBLIC object.

Public ActionThisGuy As Document

Sub Document_New()
Set ActionThisGuy = ActiveDocument
Myuserform.Show
End Sub


Now you can do anything you want to the new document (in your userform code, or anywhere else) by pointing to it explicitly.

In your userform code, say you want to put the text in a textbox txtClient into a bookmark in the new document:
Sub DoingSomething()

ActionThisGuy.Bookmark("Whatever").Range.Text = txtClient.Text
End Sub

Again, it is important to realize that you are actioning an explicit object - the document Set as ActionThisGuy. The actions are performed on THAT object, whether it is Active, or not; whether there one or fifteen other documents open.

Scooby Doo
10-29-2010, 03:23 PM
Your solution works. Thanks.