Results 1 to 4 of 4
  1. #1
    New Lounger
    Join Date
    Jan 2003
    Location
    Fort Worth, Texas, USA
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help with VBA code (WordXP)

    <img src=/S/igiveup.gif border=0 alt=igiveup width=31 height=23>
    Can someone please help me with my code? I have a document that requests 4 fields. After they are filled in, I want the document to print, then save, then blank out fields 3 and 4 and re-request different information for fields 3 & 4. The user will be printing multiple documents, but only those two fields will be changed. Thanks in advance to anyone and everyone that can help

  2. #2
    New Lounger
    Join Date
    Jan 2003
    Location
    Fort Worth, Texas, USA
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Need help with VBA code (WordXP)

    Sorry, I forgot to include the code:

    Private Sub Document_New()

    'declare vars
    Dim strCrimCrt As String
    Dim strCrtNum As String
    Dim strCaseNum As String
    Dim strDefName As String
    Dim intAnswer As Integer

    'set name vars to inputs
    strCrimCrt = InputBox("Enter ""CRIMINAL"" or the Numbered Court.")
    strCrtNum = InputBox("Enter the Court Number, (i.e., THREE or " & _
    "leave blank).")
    strCaseNum = InputBox("Enter the Case Number.")
    strDefName = InputBox("Enter Defendant's Name.")


    'set field results to name vars
    ActiveDocument.FormFields("Text1").Result = strCrimCrt
    ActiveDocument.FormFields("Text2").Result = strCrtNum
    ActiveDocument.FormFields("Text3").Result = strCaseNum
    ActiveDocument.FormFields("Text4").Result = strDefName

    'Print the current document
    ActiveDocument.PrintOut Range:=wdPrintCurrentPage

    'Save the current document
    ActiveDocument.Save

    'Open message box. Ask question. Blank out fields 3 & 4.
    'Request different text for fields 3 & 4. Print document &
    'save. Then loop until intAnswer is NO.
    Do While intAnswer = vbYes
    intAnswer = MsgBox("Do you want to create another Order Adopting " & _
    "Actions of Magistrate?", vbQuestion + vbYesNo)
    ActiveDocument.FormFields("Text3").Result = ""
    ActiveDocument.FormFields("Text4").Result = ""
    strCaseNum = InputBox("Enter the Case Number.")
    strDefName = InputBox("Enter Defendant's Name.")
    ActiveDocument.PrintOut Range:=wdPrintCurrentPage
    ActiveDocument.SaveAs FileName:="C:tempDoc1.doc"

    Loop

    If intAnswer = vbNo Then
    ActiveDocument.Close
    End If

    End Sub

    Thanks.

  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Need help with VBA code (WordXP)

    In the first place, when you start your loop

    Do While intAnswer = vbYes

    intAnswer hasn't been set yet, so the condition is False, and the loop isn't run even once. You can omit the variable intAnswer and test directly:

    Do While MsgBox("Do you want to create another Order Adopting " & _
    Actions of Magistrate?", vbQuestion + vbYesNo) = vbYes

    You don't need the test for intAnswer = vbNo later on either, because you can only get there if the user clicked "No".

    In the second place, you ask for new values of Case Number and Defendant, but you don't store them in the Form Fields. Copy the following lines into the loop, after the InputBox instructions and before the PrintOut command:

    ActiveDocument.FormFields("Text3").Result = strCaseNum
    ActiveDocument.FormFields("Text4").Result = strDefName

    In the third place, after the first time, you keep on saving the modified document to "C:tempDoc1.doc", so each new version will overwite the previous one. Perhaps you should invoke the SaveAs dialog:

    Dialogs(wdDialogFileSaveAs).Show

  4. #4
    New Lounger
    Join Date
    Jan 2003
    Location
    Fort Worth, Texas, USA
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Need help with VBA code (WordXP)

    Thanks, Hans. You make it seem so easy. <img src=/S/kiss.gif border=0 alt=kiss width=34 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
  •