Results 1 to 3 of 3
  1. #1
    New Lounger
    Join Date
    Feb 2003
    Location
    Texas, USA
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Templates with 1 Prompt Window (2002/SP2)

    I'm basically VBA illiterate but have managed to create several Word templates with 1prompt window (form) that has several text boxes. Through copying an example in an old Word 97 manual, I've figure out that you need 4 components to achieve this goal: 1) A form, 2) VBA code for the form, 3) Code in the "This Document" object, and 4) a code in Module1. The templates seem to work fine EXCEPT for 1 thing. If a document is created based on 1 of these templates, then the user MUST close that document before creating a new document based on that same template. So I've inserted a message box to warn users about that. The Word 97 manual doesn't explain any of the code and perhaps there's another book out there that I can purchase that can help explain this better. In any case, can anyone help me with this dilemma? I have a feeling my problem lies within the code in "This Document" but I've tried everything my inexperienced brain can think of. I would be ever so grateful for any help! <img src=/S/confused.gif border=0 alt=confused width=15 height=20>

  2. #2
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    Brussels, Brussel, Belgium
    Posts
    159
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Templates with 1 Prompt Window (2002/SP2)

    hi,

    please post your code so we can investigate <img src=/S/exclamation.gif border=0 alt=exclamation width=15 height=15>

    in the mean time i will make a WAG (a crystal ball smilie would be appropriate here) <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    you can put code either in the document_new event sub of the thisdocumentclass module, with calls to external subs or functions if necessary, or you can put it all in a module (the sub that executes when you start a new document from the template must be called autonew, and be public)

    there is a small problem with userforms. if you start a new document bases on a template for which you previously already created a new document that is still opened, the userform sometimes duplicates the values of the controls that you typed in when you created the first document. this is because when you use the userform1.show method, a hidden global instance of the form is created, which sometimes causes the userform to retain its control values when you leave a previously created document opened. to prevent this, declare a local userform object variable and then instatiate the userform by means of this variable. here's some small example code to demonstrate (premise : your userform is called 'Userform1'):

    sub InstantiateLocalisedUserform()
    dim MyLocalUserform as Userform1
    set mylocaluserform=new userform1
    mylocaluserform.caption="it seems to work now."
    mylocaluserform.show
    end sub

    ofcourse, you can put this code in the document_new() event sub of the thisdocument class module of the template.

    greetings,

  3. #3
    New Lounger
    Join Date
    Feb 2003
    Location
    Texas, USA
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Templates with 1 Prompt Window (2002/SP2)

    Hi Pieter!

    Aah yes - you want code - great!

    All code is listed separately. CODE FOR THE FORM is as follows. I did encounter the "small problem" you addressed. Note the code for the MsgBox alludes to that. Have not had a chance to try your solution to this yet but will do so very soon!

    Private Sub OkButton_Click()
    Dim OKToclose As Integer

    OKToclose = True
    If Len(Me.txtTo.Text) = 0 Then OKToclose = False
    If Len(Me.txtFrom.Text) = 0 Then OKToclose = False
    If Len(Me.txtRe.Text) = 0 Then OKToclose = False
    If OKToclose = True Then
    Me.Hide
    CreateCTWMemoTemplate = True
    Selection.EndKey Unit:=wdStory
    MsgBox "WARNING!! Before creating a new MEMO document, you MUST CLOSE this one. You CAN NOT have any MEMO documents open when creating a new one. If you do, the old document prompt information will be replaced with the new information!!", vbOKOnly + vbExclamation, "WARNING! THIS IS YOUR ONLY NOTICE!"
    Else
    MsgBox "Please fill in all blanks or click Cancel.", vbOKOnly + vbExclamation, "Missing Information"
    End If
    End Sub
    Private Sub CancelButton_Click()
    Me.Hide
    CreateCTWMemoTemplate = False
    End Sub

    Private Sub UserForm_Click()

    End Sub

    CODE FOR "THIS DOCUMENT" object is as follows:

    Private Sub Document_New()
    Dim fld As Field

    Call UserForm_Initialize
    CTWMemoTemplate.Show
    If CreateCTWMemoTemplate = True Then
    CTWMemoTemplate.Hide
    ActiveDocument.Variables("To") = CTWMemoTemplate.txtTo.Text
    ActiveDocument.Variables("From") = CTWMemoTemplate.txtFrom.Text
    ActiveDocument.Variables("Re") = CTWMemoTemplate.txtRe.Text
    For Each fld In ActiveDocument.Fields
    fld.Update
    fld.Unlink
    Next
    ActiveDocument.Activate
    End If
    End Sub
    Private Sub UserForm_Initialize()
    CTWMemoTemplate.txtTo.Text = ""
    CTWMemoTemplate.txtFrom.Text = ""
    CTWMemoTemplate.txtRe.Text = ""
    CTWMemoTemplate.txtTo.SetFocus

    End Sub

    CODE FOR MODULE1 is a simple 1 liner as follows:

    Public CreateCTWMemoTemplate As Integer

    That's all the code I have thus far. <img src=/S/brickwall.gif border=0 alt=brickwall width=25 height=15>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •