Outllok insert word as Message Body

Billucky

New Member
Joined
Dec 15, 2011
Messages
29
Hello there,

I have a code that sents an email to the reciepents I want, with the attachement i need

The problem is tha in the message of the mail I want after this:
[FONT=&quot]To: [/FONT]<o:p></o:p>Recipient1
[FONT=&quot]ATTN: [/FONT]<o:p></o:p>Attn
[FONT=&quot]Dear [/FONT]<o:p></o:p>Recipient2


to input a message from a specific word document

So we have:

With OutMail
.To = cell.Value
.Subject = Subj
.body = "To:" & " " & Recipient1 & " " & vbNewLine & vbNewLine _
& "ATTN:" & " " & Attn & vbNewLine & vbNewLine & _
"Dear" & " " & Recipient2 _
HERE THE TEXT FROM THE WORD DOCUMENT MUST BE ENTERED

Can anybody please provide some help?
 
nice code, Simon

Bill, as written, you will need to reference:

Microsoft Word #.# Object Library
Microsoft Outlook #.# Object Library

'~~~~~~~~~ Compile ~~~~~~~~~

Whenever you change code, references, or switch versions or operating environment, you should always compile and save before executing.

from the menu in a VBE (module) window: Debug, Compile

Fix any errors on the yellow highlighted lines.
Add needed references and remove missing references if necessary
(Tools, References...)

keep compiling until nothing happens (this is good!) -- then Save

~~~~~ also be sure to use Option Explicit at the top of each module so variables that are not declared or are misspelled will be picked up

Option Explicit ' require variable declaration

If this was not done when other code was written, you will probably need to DIM some variables -- best to do that anyway
 
Upvote 0

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
I have tested it in a pc that has word and outlook 2007

AND IT WORKS PERFECT.......!!!!!!!!!

Simon, Thank you soooooo much for this.....!!!!!!
Thank you to Crystal for your efforts.
 
Upvote 0
ohhhhhhhh........I cannot understand this

It worked perfectly fine for 4-5 times but now suddenlty it says

Run-Time error '91'
Object variable or with block variable not set.
For the outInsp.WordEditor.Content.Paste part of the code.....

Can you guess whats wrong???
 
Upvote 0
I restarted Outlook and it worked again perfectly!!
I dont know what was wrong...

Anyway thanks again !!!
 
Upvote 0
perhaps this:
Set wdApp = Word.Application

should be:
Set wdApp = GetObject(,"Word.Application")

Likewise with Outlook

As it is now, the current instance is not being used because the syntax is not right -- as a result, new instances are being created each time ...
 
Upvote 0
Hi Crystal,

interesting point, I think that GetObject should probably always be used when binding objects to avoid uncertainty and incorrect use. I think my code was left as it is because I copied the Word error trap for the Outlook part which I shouldn't have done (I'll explain below). As it stands the Outlook part is redundant and needs a GetObject statement to be useful. My understanding of the Set outApp = Outlook.Application line is that it will use the active version of outlook if there is one, otherwise create its own. This can be tested with the following code

Code:
Sub testOut()

Dim outApp As Outlook.Application


    Set outApp = Outlook.Application
    
    MsgBox outApp.ActiveExplorer


End Sub

When Outlook is open this should display "Inbox" or "Drafts" or "Calendar" etc but when there isn't, it will initiate an instance of Outlook but because there is no active explorer will fail.

However, my experience with Word (2007 at least) is different in that "Set wdApp = Word.Application" does act like GetObject. For example try creating a new word document and type "Hello" (or something else) in the document then run this code from Excel

Code:
Sub testWd()

Dim wdApp As Word.Application


    Set wdApp = Word.Application
    
    MsgBox wdApp.ActiveDocument.Range


End Sub

and you should get the word "Hello" pop up, close Word and run again and this will error.

My understanding could be wrong.

Anyway, back on track, it is possible that the code is picking up the wrong inspector (although I'm not sure why it would). I would recommend, as Crystal says, definitely replacing the Set outApp = Outlook.Application with Set OutApp = GetObject(, "Outlook.Application") and doing the same for Word if you want to. If after this you still get an error then post up your full code and I/we'll take a look :)

Simon
 
Last edited:
Upvote 0
Dear Simon,

I dont know what happended with outlook but now your code works everytime i try to use it.
Thats just perfect.

Thank you so much for that.

I wonder (if its not much to ask) if you could help with with this:

The strGreeting is set as String

And my strGreeting actually is

Code:
 strGreeting = To:" & "  " & Recipient1 & "  " & vbNewLine & vbNewLine _                     & "ATTN:" & "  " & ATTN & vbNewLine & vbNewLine & _
                     "Dear" & "  " & Recipient2 & vbNewLine & vbNewLine _
[code]

Everythind is set fine like this:

To:  blahblah  
 
ATTN:  blahblahblah
 
Dear  blahblah

Is there any way to make To: and ATTN: to be [B]bold[/B]? Not their value  I have tried many things but i gues the fact that is set as string (or even HTMLtext) does not take any bold :(. Example

[B]To:[/B]  blahblah  
 
[B]ATTN:[/B]  blahblahblah
 
Dear  blahblah
 
Upvote 0
Hi Bill,

Please see the code below, to set up extra words to be in bold just change the BoldWords array.

Code:
Sub SendWordDoc()

Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim wdSel As Word.Selection
Dim outApp As Outlook.Application
Dim outMailItem As Outlook.MailItem
Dim outInsp As Outlook.Inspector
Dim strGreeting As String
Dim bWdOpen As Boolean
Dim bOutOpen As Boolean
Dim i As Integer
Dim BoldWords(1) As String


On Error Resume Next
    Set wdApp = GetObject(, "Word.Application")
On Error GoTo 0


    If Not wdApp Is Nothing Then
        bWdOpen = True
    Else
        bWdOpen = False
        Set wdApp = New Word.Application
    End If
    
    wdApp.Visible = True
    
    Set wdDoc = wdApp.Documents.Open("C:\filetobesent.docx")
    
    strGreeting = "Dear Blah," & vbNewLine
    
    wdDoc.Characters(1).InsertBefore strGreeting
    
    Set wdSel = wdApp.Selection
    BoldWords(0) = "To"
    BoldWords(1) = "ATTN"
    
    For i = LBound(BoldWords) To UBound(BoldWords)
        With wdSel.Find
           .Replacement.Font.Bold = True
           .MatchWholeWord = True
           .Execute FindText:=BoldWords(i), ReplaceWith:=BoldWords(i), _
            Format:=True, Replace:=wdReplaceAll
        End With
    Next i
    
On Error Resume Next
    Set outApp = GetObject(, "Outlook.Application")
On Error GoTo 0


    If Not outApp Is Nothing Then
        bOutOpen = True
    Else
        bOutOpen = False
        Set outApp = New Outlook.Application
    End If
    
    Set outMailItem = outApp.CreateItem(olMailItem)
    Set outInsp = outMailItem.GetInspector


    outInsp.Display
    wdDoc.Content.Copy
    outInsp.WordEditor.Content.Paste


    With outMailItem
        .To = "joe@bloggs.com"
        .Subject = "word"
        .Send
    End With


    If bOutOpen = False Then outApp.Quit
    wdDoc.Close wdDoNotSaveChanges
    If bWdOpen = False Then wdApp.Quit
    
    Set outInsp = Nothing
    Set outMailItem = Nothing
    Set outApp = Nothing
    Set wdSel = Nothing
    Set wdDoc = Nothing
    Set wdApp = Nothing


End Sub

Hope this helps

Simon
 
Upvote 0

Forum statistics

Threads
1,215,516
Messages
6,125,286
Members
449,218
Latest member
Excel Master

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