+ Reply to Thread
Results 1 to 6 of 6

Getting compile error 'type mismatch'

Hybrid View

mso3 Getting compile error 'type... 10-14-2015, 12:19 AM
Trebor76 Re: Getting compile error... 10-14-2015, 12:50 AM
mso3 Re: Getting compile error... 10-14-2015, 01:31 AM
Trebor76 Re: Getting compile error... 10-14-2015, 01:43 AM
mso3 Re: Getting compile error... 10-14-2015, 02:17 AM
mso3 Re: Getting compile error... 10-14-2015, 09:27 AM
  1. #1
    Forum Contributor
    Join Date
    06-29-2014
    Location
    India
    MS-Off Ver
    2010 Windows 10 64 bits
    Posts
    1,269

    Getting compile error 'type mismatch'

    Hi friends,

    I’m getting compile error ‘type mismatch’

    I want to send mails on the occasion of birthday using mail merge.

    Any help will be highly appreciated.

    Thanking you in anticipation.
    Attached Files Attached Files
    Last edited by mso3; 10-14-2015 at 12:27 AM.

  2. #2
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Getting compile error 'type mismatch'

    Hi mso3,

    There were a number of errors:

    • Wrong declaration of variables - i needs to a Long variable not an Object
    • Missing variables - OutApp was not defined (this is why Option Explicit is so useful)
    • There were a number of missing End If statements to close off your IF clauses (this is why indenting is so useful)

    The code below now starts without issue but I can't run it from my machine.

    Regards,

    Robert

    Option Explicit
    Sub SendDocAsMsg()
    
        Dim x
        Dim Msg1 As String
        Dim wd As Word.Application
        Dim doc As Word.Document
        Dim itm As Outlook.MailItem
        Dim ID As String
        Dim blnWeOpenedWord As Boolean
        Dim i As Long
        Dim OutApp As Object
        On Error Resume Next
        
        Set wd = GetObject(, "Word.Application")
        If wd Is Nothing Then
            Set wd = CreateObject("Word.Application")
            blnWeOpenedWord = True
        End If
        
        Set doc = wd.Documents.Open(Filename:="C:\Birthday.docx", ReadOnly:=True)
        Set itm = doc.MailEnvelope.Item
        
        Worksheets("Access").Activate
        
        Const clngSTART As Long = 8
        
        With Sheets("Access")
            x = .Range(.Cells(clngSTART, "O"), .Cells(.Cells(Rows.Count, "P").End(xlUp).Row + 1, .Cells(clngSTART, Columns.Count).End(xlToLeft).Column)).Value
        End With
        
        For i = 1 To UBound(x)
            If IsDate(x(i, 2)) Then
                If Month(x(i, 2)) = Month(Date) And Day(x(i, 2)) = Day(Date) Then
                    If Cells(clngSTART + i - 1, "R") <> "-" Then
                        MsgBox x(i, 1) & "'s Birthday Today!", 64
                        'Create Outlook object
                        Set OutApp = CreateObject("Outlook.Application")
                        Msg1 = x(i, 1)
                        'Create Mail Item and view before sending
                        With OutApp.CreateItem(0)
                            .To = Cells(clngSTART + i - 1, "R")
                            .BCC = ""
                            .Subject = "Happy Birthday"
                            .Send
                        End With
                        doc.Close wdDoNotSaveChanges
                        If blnWeOpenedWord Then
                            wd.Quit
                        End If
                    End If
                End If
            End If
        Next i
        
        Set doc = Nothing
        Set itm = Nothing
        Set wd = Nothing
        
        MsgBox "Done!", 64
    
    End Sub
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  3. #3
    Forum Contributor
    Join Date
    06-29-2014
    Location
    India
    MS-Off Ver
    2010 Windows 10 64 bits
    Posts
    1,269

    Re: Getting compile error 'type mismatch'

    Hi Robert,

    Thank you very much.
    Now the mails are delivering but the message body is blank.
    Everything else is ok.
    How to get a message in message body from word document?

    Thank you and have a nice day.
    Sincerely,

    mso3

  4. #4
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Getting compile error 'type mismatch'

    How to get a message in message body from word document?
    Not really sure - perhaps try setting up a bookmark in Word which the code can then use as the body of the email. Should be plenty of references on the net about this.

    Good luck with it.

    Robert

  5. #5
    Forum Contributor
    Join Date
    06-29-2014
    Location
    India
    MS-Off Ver
    2010 Windows 10 64 bits
    Posts
    1,269

    Re: Getting compile error 'type mismatch'

    Hi,

    I search on the net but didn't find a proper solution for it.

    Friends, Please suggest me a amendment in code to get a message in message body from main word document.

    Waiting to receive a solution soon.

    Thanking you,

  6. #6
    Forum Contributor
    Join Date
    06-29-2014
    Location
    India
    MS-Off Ver
    2010 Windows 10 64 bits
    Posts
    1,269

    Re: Getting compile error 'type mismatch'

    Hi Robert,
    Now I'm getting message in message body but loosing the formatting and not getting a name of person after the word 'Dear ,'.

    Option Explicit
    
    Sub SendDocAsMsg()
    
    Dim x
    Dim Msg1 As String
    Dim wd As Word.Application
    Dim doc As Word.Document
    Dim itm As Outlook.MailItem
    Dim ID As String
    Dim blnWeOpenedWord As Boolean
    Dim i As Long
    Dim OutApp As Object
    On Error Resume Next
    
    Set wd = GetObject(, "Word.Application")
    If wd Is Nothing Then
    Set wd = CreateObject("Word.Application")
    blnWeOpenedWord = True
    End If
    Set doc = wd.Documents.Open _
    (Filename:="C:\BIRTHDAY.docx", ReadOnly:=True)
    Set itm = doc.MailEnvelope.Item
    
    Worksheets("Access").Activate
    
    Const clngSTART As Long = 8
    
    With Sheets("Access")
    x = .Range(.Cells(clngSTART, "O"), .Cells(.Cells(Rows.Count, "P").End(xlUp).Row + 1, .Cells(clngSTART, Columns.Count).End(xlToLeft).Column)).Value
    End With
    
    For i = 1 To UBound(x)
      If IsDate(x(i, 2)) Then
          If Month(x(i, 2)) = Month(Date) And Day(x(i, 2)) = Day(Date) Then
    If Cells(clngSTART + i - 1, "R") <> "-" Then
    MsgBox x(i, 1) & "'s Birthday Today!", 64
    '   Create Outlook object
    Set OutApp = CreateObject("Outlook.Application")
    Msg1 = x(i, 1)
    'Create Mail Item and view before sending
    'With OutApp.CreateItem(0)
    With itm
    .To = Cells(clngSTART + i - 1, "R")
    .BCC = ""
    .Subject = "Happy Birthday"
    .Send
    End With
    doc.Close wdDoNotSaveChanges
    If blnWeOpenedWord Then
    wd.Quit
    End If
    End If
    End If
    End If
    Next i
    
    Set doc = Nothing
    Set itm = Nothing
    Set wd = Nothing
    
    MsgBox "Congratulation! The mails sent successfully.", 64
    
    End Sub
    Guidance please!

    Thank you and have a nice time.

+ 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. [SOLVED] Getting compile error type mismatch
    By mso3 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 08-16-2015, 05:03 AM
  2. [SOLVED] Getting compile error 13 type mismatch
    By mso3 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-31-2015, 06:42 AM
  3. [SOLVED] Type Mismatch Compile Error: Can't figure Out
    By Pauly723 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-05-2015, 12:51 PM
  4. [SOLVED] Compile error: Type mismatch
    By jacob@thepenpoint in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 07-10-2014, 09:41 AM
  5. Compile Error - Type Mismatch
    By Will B in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-30-2012, 11:17 AM
  6. Compile Error - Type Mismatch in VBA
    By Hotmail in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-03-2009, 01:05 AM
  7. [SOLVED] Help: Compile error: type mismatch: array or user defined type expected
    By lvcha.gouqizi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-31-2005, 05:05 PM

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