View Single Post
 
Old 08-13-2009, 03:13 AM
DanMcCoy DanMcCoy is offline Windows XP Office 2007
Novice
 
Join Date: Aug 2009
Posts: 2
DanMcCoy is on a distinguished road
Default Accessing new doc from form launched by template

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
In the code for the form, I have the following code...

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
The template also contains the two strings the above code is looking for in the correct places (<<Product>> and <<Development>>)

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
Reply With Quote