Results 1 to 8 of 8

Thread: Mail Merge using Outlook Contacts

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2009
    Location
    Townsville, Qld, Australia
    Posts
    135

    Mail Merge using Outlook Contacts

    I have a user form and some code in an excel file that creates a word document (known as MyNewWordDoc.doc). The user form asks the user whether they want to use e-mail or snail mail to send the letters the code generates. If they choose snail mail, the user is then required to enter their address.

    What I want to do next with it is call up mail-merge, insert a field for the First Name from the Outlook Contacts address book into the document and then let the user choose the contacts to whom the letter is to be sent (or e-mailed) and where the user has chosen e-mail, use the recipients’ e-mail addresses.

    I’m just not too sure how to go about this. How do I call up mail merge – or otherwise use Outlook contacts to solve this problem?

    Thank you

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Mail Merge using Outlook Contacts

    the basic code to assign a data source for a mailmerge document
    ThisDocument.MailMerge.DataSource =

    try recording a macro in word, to generate some sample code for connecting to outlook contacts list
    you can then modify the code, to work in excel with your word object
    make sure to declare any word constants if they are not valid in excel (if you are using late binding of word)

    sorry i can not do for you as i do not have outlook installed
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2009
    Location
    Townsville, Qld, Australia
    Posts
    135

    Re: Mail Merge using Outlook Contacts

    Thanks, Pete. I'm working on it, I'll let you know how I got & if I get something working nicely, I'll post it.

    cheers

    The Dragon

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jun 2009
    Location
    Townsville, Qld, Australia
    Posts
    135

    Re: Mail Merge using Outlook Contacts

    I've got some of the way along with this problem - apologies for the delay in postings but I've had internet dramas. The problem I have now is telling Word where I want it to insert the merge field. This is the code to date:
    Code:
    file_name = "X Mas Letter " & Format(Date, "yyyy") & ".doc"
    
    If Dir(ActiveWorkbook.Path & "\MyNewWordDoc.doc") <> "" Then
            Kill ActiveWorkbook.Path & "\MyNewWordDoc.doc"
            End If
            .SaveAs (ActiveWorkbook.Path & "\" & file_name)
    
    
    If optPost = True Then
    GoTo SnailMailMerge
    ElseIf optEmail = True Then
    GoTo eMailMerge
    End If
    
    SnailMailMerge:
    With ActiveDocument
    
       ActiveDocument.MailMerge.MainDocumentType = wdFormLetters
        WordBasic.MailMergeUseOutlookContacts
    Selection.Collapse Direction:=wdCollapseEnd
        Selection.Find.ClearFormatting
    
    Selection.Find
        .Forward = True
            .Text = "Dear "
        If .Found = True Then
            Selection.Find.Execute
        Selection.MoveRight Unit:=wdCharacter, Count:=1
        WordBasic.MailMergeUseOutlookContacts
        ActiveDocument.MailMerge.Fields.Add Range:=Selection.Range, Name:="First"
        GoTo MergeDone
    End If
        
            End With
            
    '    End If
    '        ActiveDocument.Fields.Add Range:=Selection.Range, Type:=wdFieldMergeField, Text:="""First"""
    
    'End With
    
    eMailMerge:
    
    MergeDone:
    
    '        .Close ' close the document
     '   End With
    So, how do I go about this properly?

    Thanks

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Mail Merge using Outlook Contacts

    you should avoid using the old wordbasic code now, there will be some VBA equivalent
    vb Code:
    1. ActiveDocument.MailMerge.UseAddressBook("olk")

    maybe put bookmarks in the main document to position the mergefields
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6
    New Member
    Join Date
    Dec 2010
    Location
    United States
    Posts
    1

    Re: Mail Merge using Outlook Contacts

    record a macro in word.
    generate some sample code to connect outlook contacts list.
    you can then modify the code, to work in excel with your word object.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jun 2009
    Location
    Townsville, Qld, Australia
    Posts
    135

    Re: Mail Merge using Outlook Contacts

    For rusel and westconn1: The
    [CODE]WordBasic.MailMergeUseOutlookContacts[CODE]
    line came from recording a macro in Word 2007! (I did think it looked a bit odd...)

    I have considered using bookmarks. Thinks: If I insert the bookmark during the insertion of the rest of the text, that should be easier.

  8. #8
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Mail Merge using Outlook Contacts

    Quote Originally Posted by Resource Dragon View Post
    How do I call up mail merge – or otherwise use Outlook contacts to solve this problem?

    Thank you
    Try this...

    Code:
    '~~> Set a reference to the outlook abject library
    Sub GetNames()
        Set OutApp = CreateObject("Outlook.Application")
        Set myNameSpace = OutApp.GetNamespace("MAPI")
        Set myFolder = myNameSpace.GetDefaultFolder(olFolderContacts) 'It errors out
        
        Set myContacts = myFolder.Items
    
        For Each myContact In myContacts
            '~~> You can choose to output this to a combobox?
            MsgBox myContact.FullName
        Next
    End Sub
    Sid
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

Posting Permissions

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



Click Here to Expand Forum to Full Width