Dim as picture | Type mismatch | VBA

YourBroLucas

New Member
Joined
Jul 11, 2022
Messages
29
Office Version
  1. 2016
Platform
  1. Windows
Estimated forum members,

I've created the following macro to generate an outlook email and turn ranges into pictures before pasting them in the mail's body.

The thing is that from time to time (approx. 1 out of 5 attempts), it results in an error message 'Error 13, Type mismatch'.

If said error appeared each time, I'd understand. But a macro working and then not working under the exact same circumstances is beyond me.

The error always occurs here:
VBA Code:
 Set pic2 = shGraph.Pictures.Paste

Please note that I'm using the Outlook and Word 16.0 libraries.

May your knowledge enlighten me :)

Thank you !

VBA Code:
Sub Distribution2()

'[B] Creates an Outlook email with listed recipients
'[C] Fills the email with the correct screenshots & formats

'Excel
Dim wb As Workbook
Dim shSpe As Worksheet
Dim shGraph As Worksheet
Dim shGroup As Worksheet
Dim CopyRange As Range
Dim GraphRange As Range
Dim week As Integer

Set wb = ActiveWorkbook
Set shSpe = wb.Worksheets("YTD SPECIAL")
Set shGraph = wb.Worksheets("YTD Graph")
Set shGroup = wb.Worksheets("YTD GROUPE")
Set CopyRange = shSpe.Range("C6:J30")
Set GraphRange = shGraph.Range("J22:R41")
week = shSpe.Range("D3")

'Outlook
Dim appOutlook As Object
Dim myMail As Outlook.MailItem
Dim docWord

Set appOutlook = CreateObject("Outlook.Application")
Set myMail = appOutlook.CreateItem(olMailItem)
'--------------------------------------------------------
'[A] Updates graphs
'Done automatically on sheet
'--------------------------------------------------------
'[B] Convert ranges to pictures & cut

'Generate email
With myMail
    .To = "ceo@company.com; manager@company.com"
    .Subject = "Report Week W" & week & " COMPANY NAME"
    .CC = "recipient1@company.com; recipient2@company.com; recipient3@company.com; recipient4@company.com"
    .BCC = ""
    .Display

Dim pic As Picture
shSpe.Activate
CopyRange.Copy
Set pic = shSpe.Pictures.Paste
pic.Cut

'Put pictures into email's body and align
    Set docWord = myMail.GetInspector.WordEditor
    With docWord.Range
        .PasteAndFormat wdChartPicture
        .InsertParagraphAfter
        .InsertParagraphAfter
    End With

Dim pic2 As Picture
shGraph.Activate
GraphRange.Copy
Set pic2 = shGraph.Pictures.Paste
pic2.Cut

'Put picture into email's body and align
    Set docWord = myMail.GetInspector.WordEditor
    With docWord.Range
        .PasteAndFormat wdChartPicture
        .InsertParagraphAfter
        .InsertParagraphAfter
    End With

    docWord.Application.Selection.Paragraphs.Alignment = _
    wdAlignParagraphCenter
    shGroup.Activate

End With

End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
First, there's no need to activate your sheet before you copy, paste, and cut. This will slow things down, and could be part of the problem. If removing them doesn't help, try adding DoEvents and pausing the macro for a few seconds after each copy, paste, and cut. Actually, you can easily do both by adding the following code to your module, and calling PauseMacro when needed...

VBA Code:
Sub PauseMacro(ByVal secs As Long)

    Dim endTime As Single
    endTime = Timer + secs
    
    Do
        DoEvents
    Loop Until Timer > endTime

So, for example, let's say we want to pause 3 seconds, you could do something like this...

VBA Code:
GraphRange.Copy

PauseMacro 3 'seconds

Set pic2 = shGraph.Pictures.Paste

PauseMacro 3 'seconds

pic2.Cut

PauseMacro 3 'seconds

Actually, you might find that a 1 second delay might suffice.

Hope this helps!
 
Upvote 0
Solution

Forum statistics

Threads
1,224,814
Messages
6,181,126
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top