+ Reply to Thread
Results 1 to 5 of 5

Mail Merge and Send Fax via Email

Hybrid View

Gos-C Mail Merge and Send Fax via... 06-17-2015, 04:00 PM
JoeFoot Re: Mail Merge and Send Fax... 06-17-2015, 05:24 PM
Gos-C Re: Mail Merge and Send Fax... 06-17-2015, 08:05 PM
Gos-C Re: Mail Merge and Send Fax... 06-18-2015, 01:55 PM
JoeFoot Re: Mail Merge and Send Fax... 06-18-2015, 03:31 PM
  1. #1
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408

    Mail Merge and Send Fax via Email

    Hi all,

    I am trying to create a macro to automate sending fax requests related to data on a report, and have found the following code online which I tried to modify to meet my need. However, it is not working. It opens the Word template but gives Run-time error'438' Object doesn't support this property or method.

    This is my first attempt at Mail Merge, in general, so I am practically clueless at what I am doing and will appreciate it if someone could help me, please.

    Here is the code:

    Sub AuditFaxRequest()
    Dim bCreatedWordInstance As Boolean
    Dim objWord As Object
    Set objWord = CreateObject("Word.Application")
    Dim objMMMD As Object
    Set objMMMD = CreateObject("Word.Document")
    Dim ProviderNo As String, rng1 As Range
    Dim cDir As String
    Dim r As Long
    Dim lastRow As Long
    Dim ThisFileName As String
    Dim NewFileName As String
    ' Setup filenames
    Const WTempName = "FaxRequest.docx" 'This is the Fax Word template name,
    
    ' Setup directories
    
    cDir = ActiveWorkbook.Path + "\" 'Change if appropriate
    ThisFileName = ThisWorkbook.Name
    lastRow = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
    
    Set rng1 = ActiveSheet.Range(Cells(1, 1), Cells(lastRow, 9))
    ActiveWorkbook.Names.Add Name:="FaxInfo", RefersTo:=rng1
    
    On Error Resume Next
    
    ' Create a Word Application instance
    bCreatedWordInstance = False
    Set objWord = GetObject(, "Word.Application")
    
    If objWord Is Nothing Then
      Err.Clear
      Set objWord = CreateObject("Word.Application")
      bCreatedWordInstance = True
    End If
    
    If objWord Is Nothing Then
        MsgBox "Could not start Word"
        Err.Clear
        On Error GoTo 0
        Exit Sub
    End If
    
    ' Let Word trap the errors
    On Error GoTo 0
    
    ' Set to True if you want to see the Word Doc flash past during construction
    objWord.Visible = False
    
    'Open Word Template
                            Set objMMMD = objWord.Documents.Open(cDir + WTempName)
    objMMMD.Activate
    
    'Merge the data
    With objMMMD
        .MailMerge.OpenDataSource Name:=cDir + ThisFileName, _
        ReadOnly:=True, _
        Connection:="FaxInfo"
    
    '        sqlstatement:="SELECT *  FROM 'FaxData'"   ' Set this as required
        
        For r = 2 To lastRow
            If Cells(r, 9).Value = "Fax sent" Then GoTo nextrow
        'rest of code goes here
        
        With objMMMD.MailMerge  'With ActiveDocument.MailMerge
        
        
            .Destination = wdSendToEmail
        .MailAddressFieldName = "email"
        .MailAsAttachment = True
    '    .MailFormat = wdMailFormatHTML
        .MailSubject = "Prescription Audit"
        .SuppressBlankLines = True
    '    With .DataSource
    '        .FirstRecord = wdDefaultFirstRecord
    '        .LastRecord = wdDefaultLastRecord
    '    End With
        .Execute Pause:=False
          
            .Destination = wdSendToNewDocument
            .SuppressBlankLines = True
         End With
            
                With .DataSource
                .FirstRecord = r - 1
                .LastRecord = r - 1
                .ActiveRecord = r - 1
                End With
                
        .Execute Pause:=False
    
     ' Save new file
    NewFileName = "FaxRequest - " & ProviderNo & ".docx"
    objWord.ActiveDocument.SaveAs cDir + NewFileName
    
    ' Close the Mail Merge Main Document
    objMMMD.Close savechanges:=wdDoNotSaveChanges
    Set objMMMD = Nothing
    
    ' Close the New Mail Merged Document
    If bCreatedWordInstance Then
    objWord.Quit
    End If
    
    0:
    Set objWord = Nothing
    Cells(r, 9).Value = "Fax sent"
    nextrow:
    
    Next r
    End With
    End Sub
    Thank you,
    Gos-C
    Using Excel 2010 & Windows 10
    "It is better to be prepared for an opportunity and not have one than to have an opportunity and not be prepared."

  2. #2
    Registered User
    Join Date
    06-12-2015
    Location
    Maryland, USA
    MS-Off Ver
    2010
    Posts
    83

    Re: Mail Merge and Send Fax via Email

    instead of:

    Set objWord = GetObject(, "Word.Application")
    try:

    Set objWord = CreateObject("Word.Application")
    Got help? Pls give rep.
    If you do R&D learn VBA

  3. #3
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408

    Re: Mail Merge and Send Fax via Email

    Wonderful! Big improvement. Thank you very much for spotting that and pointing it out, JoeFoot. However, it still gave the same error message, "Object doesn't support this property or method."

    It fails here:

    [CODE][/With .DataSource
    .FirstRecord = r - 1
    .LastRecord = r - 1
    .ActiveRecord = r - 1
    End WithCODE]

  4. #4
    Forum Contributor
    Join Date
    09-19-2004
    Location
    Canada
    Posts
    408

    Re: Mail Merge and Send Fax via Email

    It looks like it's the code to have Outlook send all the documents (faxes) as an attachment that is not correct/needed. Can some help me, please?

    Thank you,
    Gos-C
    Last edited by Gos-C; 06-18-2015 at 02:20 PM.

  5. #5
    Registered User
    Join Date
    06-12-2015
    Location
    Maryland, USA
    MS-Off Ver
    2010
    Posts
    83

    Re: Mail Merge and Send Fax via Email

    remove this:

    With .DataSource
    .FirstRecord = r - 1
    .LastRecord = r - 1
    .ActiveRecord = r - 1
    End With

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. How to do a Mail Merge With CC Email
    By Kingswood in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 11-25-2015, 04:25 PM
  2. Automatic send email (without even click send in mail software) with excel vba ??
    By alexnkc in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-17-2013, 08:31 PM
  3. mail merge to email
    By newangel90 in forum Word Formatting & General
    Replies: 1
    Last Post: 12-10-2010, 12:53 PM
  4. .Send Mail - Additional Email Recipient
    By pauldaddyadams in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-12-2009, 06:21 AM
  5. Send Mail Merge Auto-Emails based on date
    By Phillycheese5 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-17-2005, 11:40 AM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1