one small snippet of code

dspel79082

Board Regular
Joined
Sep 29, 2012
Messages
125
Hello all, I am in need of one small snippet of code. I am using several of Ron Debruin's email routines to send emails from Excel through Outlook. No problems, but I have the need to insert a quick part from Outlook into the email that the code is composing.
The code has created the email, addressed it, filled in the subject and the greeting. While still working with the mailitem and before '.send' I need to insert a quick part from Outlook from NormalEmail template from category 'General'. The quick part name is 'Test'.
I've tried other approaches. It has to be a Quick Part, not a template or any other method.

Can anyone give me that one little line of code to insert the quick part into the email right after the body?
Here is the applicable part of the code:
With OutMail
.To = cell.Value
.cc = ccto
.bcc = bccto
.Subject = mysubject
.Body = "Hello " & Cells(cell.Row, "D").Value & "," _
& vbNewLine & vbNewLine & _
Range("i1").Value _
& vbNewLine & Range("i2").Value & vbNewLine _
& vbNewLine & "Thank you," & vbNewLine & "My Name" & vbNewLine _
& "My Dept." & vbNewLine & "My Phone Number"
& "My Dept." & vbNewLine & "My ext"
.Attachments.Add ("N:\(Z) Shared Folders\" & cell.Offset(0, 1).Value)
.send


Thanks to anyone who cares to help.

<tbody>
</tbody><colgroup><col></colgroup>
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Unfortunately Quick Parts aren't a simple snippet of code, I've added some flesh (most of it as an example) to your code. You need to add a reference to the Word Object Library for this to work.

Code:
Sub quickPart()    
    Dim outApp As Outlook.Application
    Dim outMail As Outlook.MailItem
    Dim wdDoc As Word.Document
    Dim wdTemp As Word.Template
    Dim i As Integer

    Set outApp = Outlook.Application
    Set outMail = outApp.CreateItem(olMailItem)
    
    With outMail
        .To = Cell.Value
        .CC = ccto
        .BCC = bccto
        .Subject = mysubject
        .Body = "Hello " & Cells(Cell.Row, "D").Value & "," _
        & vbNewLine & vbNewLine & _
        Range("i1").Value _
        & vbNewLine & Range("i2").Value & vbNewLine _
        & vbNewLine & "Thank you," & vbNewLine & "My Name" & vbNewLine _
        & "My Dept." & vbNewLine & "My Phone Number" _
        & "My Dept." & vbNewLine & "My ext"
        .Attachments.Add ("N:\(Z) Shared Folders\" & Cell.Offset(0, 1).Value)
        .Display
    End With
    
    i = Len(Cells(Cell.Row, "D")) + Len(Range("I1")) + Len(Range("I2")) + 12
    
    On Error Resume Next
    Set wdDoc = outApp.ActiveInspector.WordEditor
    Set wdTemp = wdDoc.Application.Templates(1).BuildingBlockEntries("test"). _
        Insert(Where:=wdDoc.Characters(i), RichText:=True)
    On Error GoTo 0
    
    outMail.Send
    
    Set wdTemp = Nothing
    Set wdDoc = Nothing
    Set outMail = Nothing
    Set outApp = Nothing
End Sub

Some parts of your code I've had to guess at, I've also made an assumption that you'll want your quick part after your text but before your sign off. To move this change the variable "i". Here is a link to another thread I commented on about how to manipulate word objects in outlook items, if you want to see a couple of different techniques.

Hope this is of some help, let me know how you get on.

Simon
 
Last edited:
Upvote 0
Simon, you are awesome! That looks like exactly what I need. I will let you know how it goes. Thank you thank you thank you!!
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,537
Members
449,088
Latest member
RandomExceller01

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