Results 1 to 3 of 3
  1. #1
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    UK
    Posts
    239
    Thanks
    4
    Thanked 1 Time in 1 Post

    Ascertaining whether a variable has been declared (VBA Word 2000 SR1a)

    On creating a document from a template, the user is presented with a Userform to capture input for the cover page and headers and footers. After the user has clicked OK on the userform, I know that each variable has a value as otherwise OK returns an error.

    I am trying to write some code that checks the variables have been declared, looked at another way, OK has been clicked and the input validated.

    This will enable me to create an If Then Else statement where one routine will run if OK has been successfully clicked and all the variables have a value and another routine will run if not all the variables have been created. This will occur if the user clicks Cancel the first time they use the form.

    The code below works when the variables do have a value, displaying the msgbox True which I have put in as a quick guide to what the code is doing. However, when I click Cancel with a new document, instead of getting the msgbox False, I get "Runtime error 5825 The object has been deleted". My guess is that this is because the variables don't exist at that time and so the check cannot be run.

    Am I on the right track and does anyone have any pointers or ideas as to how I can achieve the desired result?

    Thanks in advance.

    Private Sub Cancel_Click()
    OKToCancel = True
    If ActiveDocument.Variables("DocType") = Null Then OKToCancel = False
    If ActiveDocument.Variables("VersionNo") = Null Then OKToCancel = False
    If ActiveDocument.Variables("Date") = Null Then OKToCancel = False
    If OKToCancel = True Then
    frmReport.Hide
    MsgBox "True"
    Else
    frmReport.Hide
    MsgBox "False"
    ActiveWindow.Close
    End If
    End Sub

  2. #2
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Ascertaining whether a variable has been declared (VBA Word 2000 SR1a)

    The easiest thing to do is to loop through all variables in the document comparing the names to the ones you are looking for, you can then check that you found all three
    <pre>Private Sub Cancel_Click()
    Dim varTemp As Variable
    Dim intVarCount As Integer
    For Each varTemp In ActiveDocument.Variables
    Select Case varTemp.Name
    Case "DocType", "VersionNo", "Date"
    intVarCount = intVarCount + 1
    End Select
    Next varTemp
    If intVarCount = 3 Then
    frmReport.Hide
    MsgBox "True"
    Else
    frmReport.Hide
    MsgBox "False"
    ActiveWindow.Close
    End If
    End Sub
    </pre>


    StuartR

  3. #3
    3 Star Lounger
    Join Date
    Feb 2001
    Location
    UK
    Posts
    239
    Thanks
    4
    Thanked 1 Time in 1 Post

    Re: Ascertaining whether a variable has been declared (VBA Word 2000 SR1a)

    That's it! Thank you so much.

Posting Permissions

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