Results 1 to 4 of 4

Thread: Opening Outlook and attaching a specific file

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Posts
    1,384

    Opening Outlook and attaching a specific file

    How can I open outlook/or the default mail service to a new outgoing mail that has a specified file attached?
    Mel

  2. #2
    Member
    Join Date
    Aug 2002
    Posts
    43
    Here's some code I wrote for an emailer class that uses Outlook. It allows to, cc, bcc, subject, body and one attachment (multiple attachments could be sent by passing an array instead of just a string).

    VB Code:
    1. Option Explicit
    2.  
    3. Public Event ErrorMessage(ErrorNumber As Integer, ErrorMessage As String, Source As String)
    4.  
    5. Private objOutlook As New Outlook.Application
    6. Private objOutlookMail As Outlook.MailItem
    7. Private objOutlookRecip As Outlook.Recipient
    8.  
    9. Public Function Send(RecipientAddress As String, Optional CC As String, Optional BCC As String, Optional Subject As String, Optional MsgBody As String, Optional Attachment As String) As Boolean
    10. On Error GoTo ComposeErr
    11.    
    12.     Dim RecipientKnown As Boolean
    13.     Dim FinalToAddress As String
    14.     Dim FinalCCAddress As String
    15.     Dim FinalBCCAddress As String
    16.     Dim Recips() As String
    17.     Dim i As Integer
    18.     Dim Dirs() As String
    19.    
    20.     With objOutlookMail
    21.        
    22.         Recips = Split(Trim(RecipientAddress), ";")
    23.         For i = 0 To UBound(Recips)
    24.             .Recipients.Add Recips(i)
    25.             .Recipients.Item(Recips(i)).Type = olTo
    26.             RecipientKnown = .Recipients.Item(Recips(i)).Resolve
    27.             If RecipientKnown Then
    28.                 If UBound(Recips) > 0 Then
    29.                     FinalToAddress = FinalToAddress & ";" & Recips(i)
    30.                 Else
    31.                     FinalToAddress = Recips(i)
    32.                 End If
    33.             End If
    34.         Next i
    35.        
    36.         If Trim(CC) <> "" Then
    37.             ReDim Recips(0)
    38.             Recips = Split(Trim(CC), ";")
    39.             For i = 0 To UBound(Recips)
    40.                 .Recipients.Add Recips(i)
    41.                 .Recipients.Item(Recips(i)).Type = olCC
    42.                 RecipientKnown = .Recipients.Item(Recips(i)).Resolve
    43.                 If RecipientKnown Then
    44.                     If UBound(Recips) > 0 Then
    45.                         FinalCCAddress = FinalCCAddress & ";" & Recips(i)
    46.                     Else
    47.                         FinalCCAddress = Recips(i)
    48.                     End If
    49.                 End If
    50.             Next i
    51.         End If
    52.  
    53.         If Trim(BCC) <> "" Then
    54.             ReDim Recips(0)
    55.             Recips = Split(Trim(BCC), ";")
    56.             For i = 0 To UBound(Recips)
    57.                 .Recipients.Add Recips(i)
    58.                 .Recipients.Item(Recips(i)).Type = olBCC
    59.                 RecipientKnown = .Recipients.Item(Recips(i)).Resolve
    60.                 If RecipientKnown Then
    61.                     If UBound(Recips) > 0 Then
    62.                         FinalBCCAddress = FinalBCCAddress & ";" & Recips(i)
    63.                     Else
    64.                         FinalBCCAddress = Recips(i)
    65.                     End If
    66.                 End If
    67.             Next i
    68.         End If
    69.        
    70.         If Left$(Trim(FinalToAddress), 1) = ";" Then FinalToAddress = Mid$(Trim(FinalToAddress), 2)
    71.         If Left$(Trim(FinalCCAddress), 1) = ";" Then FinalCCAddress = Mid$(Trim(FinalCCAddress), 2)
    72.         If Left$(Trim(FinalBCCAddress), 1) = ";" Then FinalBCCAddress = Mid$(Trim(FinalBCCAddress), 2)
    73.        
    74.         If Trim(FinalToAddress) <> "" Then
    75.             .To = Trim(FinalToAddress)
    76.             .CC = Trim(FinalCCAddress)
    77.             .BCC = Trim(FinalBCCAddress)
    78.             .Importance = olImportanceHigh
    79.             .Subject = Trim(Subject)
    80.             .Body = MsgBody
    81.             If Trim(Attachment) <> "" Then
    82.                 Dirs = Split(Attachment, "\")
    83.                 .Attachments.Add Attachment, olByValue, 1, Dirs(UBound(Dirs))
    84.             End If
    85.             .Send
    86.         Else
    87.             Send = False
    88.             RaiseEvent ErrorMessage(1001, "No known recipient addresses found.", "Email:Send")
    89.             Exit Function
    90.         End If
    91.     End With
    92.    
    93.     Send = True
    94.    
    95.     Exit Function
    96.  
    97. ComposeErr:
    98.    
    99.     Send = False
    100.     RaiseEvent ErrorMessage(Err.Number, Err.Description, "Email:Send")
    101.  
    102. End Function
    103.  
    104. Private Sub Class_Initialize()
    105.  
    106.     Set objOutlookMail = objOutlook.CreateItem(olMailItem)
    107.    
    108. End Sub


    Hope this helps.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2001
    Posts
    1,384
    Hi mcsd02,
    Thanks for the reply. I found this myself and it suits my needs better .
    Code:
        If (fso.FileExists(rsQuoteHdr!FileLocation)) Then
            Dim olapp As New Outlook.Application
            Dim olMail As Outlook.MailItem
            Dim myAttachment As Outlook.Attachments
    
            'Create a new mail object form the
            'Outlook98 Application object
            Set olMail = olapp.CreateItem(olMailItem)
            Set myAttachment = olMail.Attachments
            myAttachment.Add "" & rsQuoteHdr!FileLocation
            olMail.Body = strMessage & vbNewLine & vbNewLine
            'Tell Outlook to send this message
            olMail.Display
        
            'Be kind to your environment and clean
            'up your unused objects
            Set olMail = Nothing
            Set olapp = Nothing
            
        Else
            msg = MsgBox("No file detected, please SaveAs first", vbOKOnly & vbInformation, "No file found")
            Call SaveAs_Click
        End If
    Thanks anyway
    Mel

  4. #4
    Member
    Join Date
    Aug 2002
    Posts
    43
    Here's some code I wrote for an emailer class that uses Outlook. It allows to, cc, bcc, subject, body and one attachment (multiple attachments could be sent by passing an array instead of just a string).

    VB Code:
    1. Option Explicit
    2.  
    3. Public Event ErrorMessage(ErrorNumber As Integer, ErrorMessage As String, Source As String)
    4.  
    5. Private objOutlook As New Outlook.Application
    6. Private objOutlookMail As Outlook.MailItem
    7. Private objOutlookRecip As Outlook.Recipient
    8.  
    9. Public Function Send(RecipientAddress As String, Optional CC As String, Optional BCC As String, Optional Subject As String, Optional MsgBody As String, Optional Attachment As String) As Boolean
    10. On Error GoTo ComposeErr
    11.    
    12.     Dim RecipientKnown As Boolean
    13.     Dim FinalToAddress As String
    14.     Dim FinalCCAddress As String
    15.     Dim FinalBCCAddress As String
    16.     Dim Recips() As String
    17.     Dim i As Integer
    18.     Dim Dirs() As String
    19.    
    20.     With objOutlookMail
    21.        
    22.         Recips = Split(Trim(RecipientAddress), ";")
    23.         For i = 0 To UBound(Recips)
    24.             .Recipients.Add Recips(i)
    25.             .Recipients.Item(Recips(i)).Type = olTo
    26.             RecipientKnown = .Recipients.Item(Recips(i)).Resolve
    27.             If RecipientKnown Then
    28.                 If UBound(Recips) > 0 Then
    29.                     FinalToAddress = FinalToAddress & ";" & Recips(i)
    30.                 Else
    31.                     FinalToAddress = Recips(i)
    32.                 End If
    33.             End If
    34.         Next i
    35.        
    36.         If Trim(CC) <> "" Then
    37.             ReDim Recips(0)
    38.             Recips = Split(Trim(CC), ";")
    39.             For i = 0 To UBound(Recips)
    40.                 .Recipients.Add Recips(i)
    41.                 .Recipients.Item(Recips(i)).Type = olCC
    42.                 RecipientKnown = .Recipients.Item(Recips(i)).Resolve
    43.                 If RecipientKnown Then
    44.                     If UBound(Recips) > 0 Then
    45.                         FinalCCAddress = FinalCCAddress & ";" & Recips(i)
    46.                     Else
    47.                         FinalCCAddress = Recips(i)
    48.                     End If
    49.                 End If
    50.             Next i
    51.         End If
    52.  
    53.         If Trim(BCC) <> "" Then
    54.             ReDim Recips(0)
    55.             Recips = Split(Trim(BCC), ";")
    56.             For i = 0 To UBound(Recips)
    57.                 .Recipients.Add Recips(i)
    58.                 .Recipients.Item(Recips(i)).Type = olBCC
    59.                 RecipientKnown = .Recipients.Item(Recips(i)).Resolve
    60.                 If RecipientKnown Then
    61.                     If UBound(Recips) > 0 Then
    62.                         FinalBCCAddress = FinalBCCAddress & ";" & Recips(i)
    63.                     Else
    64.                         FinalBCCAddress = Recips(i)
    65.                     End If
    66.                 End If
    67.             Next i
    68.         End If
    69.        
    70.         If Left$(Trim(FinalToAddress), 1) = ";" Then FinalToAddress = Mid$(Trim(FinalToAddress), 2)
    71.         If Left$(Trim(FinalCCAddress), 1) = ";" Then FinalCCAddress = Mid$(Trim(FinalCCAddress), 2)
    72.         If Left$(Trim(FinalBCCAddress), 1) = ";" Then FinalBCCAddress = Mid$(Trim(FinalBCCAddress), 2)
    73.        
    74.         If Trim(FinalToAddress) <> "" Then
    75.             .To = Trim(FinalToAddress)
    76.             .CC = Trim(FinalCCAddress)
    77.             .BCC = Trim(FinalBCCAddress)
    78.             .Importance = olImportanceHigh
    79.             .Subject = Trim(Subject)
    80.             .Body = MsgBody
    81.             If Trim(Attachment) <> "" Then
    82.                 Dirs = Split(Attachment, "\")
    83.                 .Attachments.Add Attachment, olByValue, 1, Dirs(UBound(Dirs))
    84.             End If
    85.             .Send
    86.         Else
    87.             Send = False
    88.             RaiseEvent ErrorMessage(1001, "No known recipient addresses found.", "Email:Send")
    89.             Exit Function
    90.         End If
    91.     End With
    92.    
    93.     Send = True
    94.    
    95.     Exit Function
    96.  
    97. ComposeErr:
    98.    
    99.     Send = False
    100.     RaiseEvent ErrorMessage(Err.Number, Err.Description, "Email:Send")
    101.  
    102. End Function
    103.  
    104. Private Sub Class_Initialize()
    105.  
    106.     Set objOutlookMail = objOutlook.CreateItem(olMailItem)
    107.    
    108. End Sub


    Hope this helps.

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