Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 08-13-2009, 03:13 AM
DanMcCoy DanMcCoy is offline Accessing new doc from form launched by template Windows XP Accessing new doc from form launched by template Office 2007
Novice
Accessing new doc from form launched by template
 
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
  #2  
Old 08-13-2009, 03:58 AM
DanMcCoy DanMcCoy is offline Accessing new doc from form launched by template Windows XP Accessing new doc from form launched by template Office 2007
Novice
Accessing new doc from form launched by template
 
Join Date: Aug 2009
Posts: 2
DanMcCoy is on a distinguished road
Default I have found the answer: ActiveDocument

Since posting I have found the answer anyway; I replaced "ThisDocument" with "ActiveDocument" and it works.
Reply With Quote
Reply

Tags
form, template, thisdocument, visual basic

Thread Tools
Display Modes


Similar Threads
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
Accessing new doc from form launched by template accessing out from NAS bradleyh10 Outlook 1 05-30-2009 11:52 PM
Accessing new doc from form launched by template Accessing oulook 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

Other Forums: Access Forums

All times are GMT -7. The time now is 04:54 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft