Results 1 to 8 of 8
  1. #1
    RasterImage is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Jun 2011
    Location
    UK
    Posts
    34

    Past an image from clipboard to Outlook email

    Hello,

    I want to automate creating an outlook email containing a screenshot of a report. This code does a great job of taking the screenshot I need and copying onto the clipboard, and I'm using the code below to open an email with the text I want in it. I'm just struggling with putting them together -- how can I paste my screenshot into the body of the email, after the text, please? I do not wish to save the clipboard as an image file or have an attachment.
    I could just have the user paste the image in manually, but I'd love to automate it. I'll be so grateful for any help!



    Code:
    ' Procedure : SendEmail
    ' Author    : CARDA Consultants Inc.
    ' Website   : http://www.cardaconsultants.com
    ' Purpose   : Automate Outlook to send emails with or without attachments
    ' Copyright : The following may be altered and reused as you wish so long as the
    '             copyright notice is left unchanged (including Author, Website and
    '             Copyright).  It may not be sold/resold or reposted on other sites (links
    '             back to this site are allowed).
    '
    ' Input Variables:
    ' ~~~~~~~~~~~~~~~~
    ' strTo         To Recipient email address string (semi-colon separated list)
    ' strSubject    Text string to be used as the email subject line
    ' strBody       Text string to be used as the email body (actual message)
    ' bEdit         True/False whether or not you wish to preview the email before sending
    ' strBCC        BCC Recipient email address string (semi-colon separated list)
    ' AttachmentPath    single value or array of attachment (complete file paths with
    '                   filename and extensions)
    '
    ' Revision History:
    ' Rev       Date(yyyy/mm/dd)        Description
    ' **************************************************************************************
    ' 1         2007-Nov-16             Initial Release
    '---------------------------------------------------------------------------------------
    Function SendEmail(strTo As String, strSubject As String, strBody As String, bEdit As Boolean, _
                       Optional strBCC As Variant, Optional AttachmentPath As Variant)
    'Send Email using late binding to avoid reference issues
       Dim objOutlook As Object
       Dim objOutlookMsg As Object
       Dim objOutlookRecip As Object
       Dim objOutlookAttach As Object
       Dim i As Integer
       Const olMailItem = 0
     
       On Error GoTo ErrorMsgs
     
       Set objOutlook = CreateObject("Outlook.Application")
     
       Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
       With objOutlookMsg
          Set objOutlookRecip = .Recipients.Add(strTo)
          objOutlookRecip.Type = 1
     
          If Not IsMissing(strBCC) Then
            Set objOutlookRecip = .Recipients.Add(strBCC)
            objOutlookRecip.Type = 3
          End If
     
          .Subject = strSubject
          .Body = strBody
          .Importance = 1  'Importance Level  0=Low,1=Normal,2=High
     
          ' Add attachments to the message.
          If Not IsMissing(AttachmentPath) Then
            If IsArray(AttachmentPath) Then
               For i = LBound(AttachmentPath) To UBound(AttachmentPath) - 1
                  If AttachmentPath(i) <> "" And AttachmentPath(i) <> "False" Then
                    Set objOutlookAttach = .Attachments.Add(AttachmentPath(i))
                  End If
               Next i
            Else
                If AttachmentPath <> "" Then
                    Set objOutlookAttach = .Attachments.Add(AttachmentPath)
                End If
            End If
          End If
     
          For Each objOutlookRecip In .Recipients
             If Not objOutlookRecip.Resolve Then
                objOutlookMsg.Display
             End If
          Next
     
          If bEdit Then 'Choose btw transparent/silent send and preview send
            .Display
          Else
            .Send
          End If
       End With
     
       Set objOutlookMsg = Nothing
       Set objOutlook = Nothing
       Set objOutlookRecip = Nothing
       Set objOutlookAttach = Nothing
     
    ErrorMsgs:
       If Err.Number = "287" Then
          MsgBox "You clicked No to the Outlook security warning. " & _
          "Rerun the procedure and click Yes to access e-mail " & _
          "addresses to send your message. For more information, " & _
          "see the document at http://www.microsoft.com/office" & _
          "/previous/outlook/downloads/security.asp."
          Exit Function
       ElseIf Err.Number <> 0 Then
          MsgBox Err.Number & " - " & Err.Description
          Exit Function
       End If
    End Function


  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    RasterImage is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Jun 2011
    Location
    UK
    Posts
    34
    Thank you for the link. It works, I'm happy and grateful.
    I'm trying to figure out how the code works but there's some I don't understand. And I can't quite use it as it stands.
    Do you know, is it possible to:
    Paste the clipboard contents only if it is an image?
    Paste the clipboard contents at the end of the body?

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    VB.net can determine what is on Windows clipboard https://docs.microsoft.com/en-us/dot...-the-clipboard

    But for VBA review Windows Clipboard (cpearson.com) - add the Microsoft Forms library with References browse to: c:\windows\system32\FM20.DLL

    I tried to force image to paste at end of body and fails. I used SendKeys to move to end of message body. That does work but image still pastes in front of body.
    Code:
            .Display
            SendKeys "{TAB}{END}", True
            SendKeys "{TAB}{END}", True
            SendKeys "{TAB}{END}", True
            SendKeys "{ENTER}", True
            Set olInsp = .GetInspector
            Set wdDoc = olInsp.WordEditor
            Set oRng = wdDoc.Range
            oRng.Collapse 1
            oRng.Paste
    The only option I can see is to save image to file and then embed the image in body using HTML tags.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,954
    Suggest you put the report into the email as an attachment rather than an inline image in the message body.
    Most email applications now block inline images by default to protect end users from viruses.
    Whilst Outlook does give the option of displaying the images, many programs such as Windows Mail or GMail don't have that option.
    All that is seen is a red X
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

  6. #6
    RasterImage is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Jun 2011
    Location
    UK
    Posts
    34
    The email the application is sending will get printed to pdf, and the company that the pdf goes to insists on having the report in the body of the email (not an attachment) along with the email text for reasons to do with auditing or something? And the image really does need to go at the end of the body.
    The email process will always be strictly on Outlook, so that's not a problem.

    Thanks for all suggestions anyway, thank you June7 for having a go at placing the image, and big thanks for the links which I feel are getting me closer to my goal.

    Moving the pasted screenshot to the end of the body is that last crucial step. If I wanted to use the option June7 suggested at the end, what's the recommended way to save the image to file given that application will be used on many different machines by many different users who very much don't want to be bothered with anything technical?

  7. #7
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,858
    You could ask in http://www.vbaexpress.com/forum/foru...8-Outlook-Help


    If GMayor does not know, you will be out of luck.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  8. #8
    isladogs's Avatar
    isladogs is offline MVP / VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jan 2014
    Location
    Somerset, UK
    Posts
    5,954
    Or you can try my example app at EMail Tester - Mendip Data Systems.
    The HTML example mail includes an inline image. You may be able to adapt the code to suit your needs
    Colin, Access MVP, Website, email
    The more I learn, the more I know I don't know. When I don't know, I keep quiet!
    If I don't know that I don't know, I don't know whether to answer

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 14
    Last Post: 06-19-2020, 03:44 PM
  2. Replies: 4
    Last Post: 07-12-2018, 05:38 AM
  3. Embedding .jpg image to email
    By MTSPEER in forum Programming
    Replies: 6
    Last Post: 08-11-2017, 01:16 PM
  4. Replies: 5
    Last Post: 03-23-2017, 06:38 AM
  5. Replies: 5
    Last Post: 09-14-2015, 07:24 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums