![]() |
#1
|
|||
|
|||
![]()
Hello Everybody
I am new here. I'm also new to Office Visual Basic. Here's what I'm aiming for... I would like to have a word template that contains a visual basic form containg fields for inputing data about the new document. When a user creates a new document based on that template, I'd like the form to launch, the user would fill in the fields and press an OK button, and then the code in the form would put the relavent data in the correct places in the new document. Here's what I've done... I've got the template looking nice, and I've created the form. In the Project Explorer tree, I right-clicked "ThisDocument", and clicked "View Code". I then entered the following code... Code:
Private Sub Document_New() GetDetailsFRM.Show End Sub Code:
Private Sub CancelBTN_Click() GetDetailsFRM.Hide End Sub Private Sub OKBTN_Click() ReplaceProduct ReplaceDevelopment GetDetailsFRM.Hide End Sub Private Sub ReplaceProduct() Set d = ThisDocument With d.Content.Find .ClearFormatting .Text = "<<Product>>" .Replacement.Text = ProductTXT.Text .Replacement.ClearFormatting .Execute Replace:=wdReplaceAll, Forward:=True End With End Sub Private Sub ReplaceDevelopment() Set d = ThisDocument With d.Content.Find .ClearFormatting .Text = "<<Development>>" .Replacement.Text = DevelopmentTXT.Text .Replacement.ClearFormatting .Execute Replace:=wdReplaceAll, Forward:=True End With End Sub Here's my problem... If I run the form from within the visual basic editor by pressing the run icon, it works correctly. However, I've put the template in my template directory, and then tried to create a new document based on it, but it doesn't work. The form launches, but after pressing the OK button the text is not replaced. I suspect that the problem lies with the use of "ThisDocument" in the code. Is the "ThisDocument" variable still pointing at the template, rather than the new document? If so, how do I get at the new document? Thankyou in advance for any help |
#2
|
|||
|
|||
![]()
Since posting I have found the answer anyway; I replaced "ThisDocument" with "ActiveDocument" and it works.
|
![]() |
Tags |
form, template, thisdocument, visual basic |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
OFFICE(Word,Excel..)-crash/close when DIALOG BOX launched of insert image,open file | buggingme | Office | 1 | 05-30-2010 12:18 AM |
![]() |
bradleyh10 | Outlook | 1 | 05-30-2009 11:52 PM |
![]() |
lozababi | Outlook | 1 | 05-08-2009 06:13 AM |
Fax Form Template | Pabs | Word | 0 | 03-20-2009 04:27 PM |
Needs Help on accessing mailitems from C#. Plz. | welcome2devilsworld | Outlook | 0 | 04-30-2007 09:01 AM |