Results 1 to 4 of 4
  1. #1
    2 Star Lounger
    Join Date
    Jan 2003
    Posts
    187
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Application.GetAddress - Cancel (Word 2000)

    I have the following code in a macro to get the address from user's Outlook contacts list and put the address into letter where cursor is resting. The macro works fine, but when the user clicks on "Cancel" in the GetAddress dialog, the program won't cancel--the dialog stays active until something is selected and the OK button is clicked. Is there a snipped of code I could add to the following code that would end the program if the user clicks the "Cancel" button in GetAddress dialog? Many thanks for any help anyone can give me!...Mary

    Sub TypeContactAddress()
    Dim sToName As String
    Call AddBookmark
    If MsgBox("Retrieve Recipient Information from Address Book?", vbQuestion Or vbYesNo, "coversheet generator") = vbYes Then
    Do
    sToName = Application.GetAddress
    End If

    Loop Until sToName <> "" ' loop until user selects an address
    End If

    Selection.HomeKey unit:=wdStory
    Selection.GoTo what:=wdGoToBookmark, Name:="bAddress"
    Selection.TypeText Text:=sToName
    Selection.HomeKey unit:=wdStory
    End Sub

    Sub AddBookmark()
    With ActiveDocument.Bookmarks
    .Add Range:=Selection.Range, Name:="bAddress"
    .DefaultSorting = wdSortByName
    .ShowHidden = False
    End With
    End Sub

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

    Re: Application.GetAddress - Cancel (Word 2000)

    This is because you explicitly require that sToName is not "" in the Do ... Loop. It contains a superfluous "End If", by the way. Try this:

    If MsgBox("Retrieve Recipient Information from Address Book?", vbQuestion Or vbYesNo, "coversheet generator") = vbYes Then
    sToName = Application.GetAddress
    If sToName = "" Then
    MsgBox "You didn't select anything."
    Exit Sub
    End If
    End If

  3. #3
    2 Star Lounger
    Join Date
    Jan 2003
    Posts
    187
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Application.GetAddress - Cancel (Word 2000)

    Many thanks, Hans! Your solution took care of the problem, as usual. (I still don't understand why the Application.GetAddress dialog's "Cancel" button did not work to cancel the program and close the dialog, but not to worry--I'm just happy your code solution works.). Until next time....Mary

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

    Re: Application.GetAddress - Cancel (Word 2000)

    In your original code (minus the superfluous "End If")

    Do
    sToName = Application.GetAddress
    Loop Until sToName <> "" ' loop until user selects an address

    clicking Cancel in Application.GetAddress causes it to return an empty string "", so the loop test sToName <> "" evaluates to False, and sToName = Application.GetAddress is executed again. This code doesn't allow the user to exit gracefully, it keeps on displaying the dialog until the user selects an address.

Posting Permissions

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