Link to Outlook with file embedded in e-mail

jimamj

Board Regular
Joined
Feb 1, 2006
Messages
64
I want to create a hyperlink that generates a new e-mail with a word form embedded in the e-mail. Is there a way to do this? Any help would be great. Thanks.
 
Okay... try this... it's a little bit hokey (you'll see what I mean when you run it) but it's the best I could do without inviting al sorts of security isues.... this whole mess can probably be accomplished more elegantly in Word. Just a thought. I don't remember if you said why you are automating this from Excel.

Execute Edit_Word_Doc to start the sequence...

Rich (BB code):
Dim WordApp As Word.Application
Dim Word_Doc As Word.Document

Sub Edit_Word_Doc()

    Dim myBar As CommandBar
    
    Dim mb As VbMsgBoxResult
    
    mb = MsgBox("I am about to open a word document for you to edit." & vbCrLf & "Continue?", vbYesNo, "Open Word")
    
    If mb = vbNo Then
    
        Exit Sub
        
    End If
    
    On Error Resume Next
    CommandBars("Custom_Email").Delete
    On Error GoTo 0

    'create a new commandbar and name Custom_Email.
     Set myBar = CommandBars.Add(Name:="Custom_Email", Position:=msoBarFloating, Temporary:=False)

    'add a menu item to the new commandbar
    With myBar
        .Controls.Add Type:=msoControlButton
        
        'assign button face
        .Controls(1).FaceId = 363

        'assign tooltip
        .Controls(1).TooltipText = "Send This Document As Mail"
        
        'Assign subroutines to be called
        .Controls(1).OnAction = "Embed_Doc_In_new_mail"
        
        'show the toolbar
        .Visible = True
    End With
    
    MsgBox "Notice the new floating toolbar." & vbCrLf & "When you have completed editting the Word Form, return here and press the button on the new toolbar.", , "New Toolbar"
    
    'create a new instance of Word... doesn't matter if there is already
    'an instance running, unlike Outlook, Word allows multiple instances
    Set WordApp = CreateObject("word.application")
    Set Word_Doc = WordApp.Documents.Open("C:\Temp\word.doc")
    
    'we want the user to be able to SEE the word document
    Word_Doc.Windows(1).Visible = True
    

End Sub

Sub Embed_Doc_In_new_mail()
    'for support contact paul.sasur@hs.utc.com
    'tools->references->Microsoft Outlook 11.0 Object Library
    'tools->references->Microsoft Word 11.0 Object Library
    
    Dim OutApp As Outlook.Application
    Dim OutMail As Outlook.MailItem
    Dim RecipName As String
    
    Dim OLbCreated As Boolean
    
    On Error Resume Next
    CommandBars("Custom_Email").Delete
    
    a = Word_Doc.Name
    
    On Error GoTo 0
    
    If IsEmpty(a) Then
    
        Exit Sub
        
    End If

    
    'ignore any errors
    On Error Resume Next
    'attempt to capture an existing instance of Outlook
    Set OutApp = GetObject(, "Outlook.Application")
    're-set to stop on errors
    On Error GoTo 0
    
    'test to see if we successfully captured an existing instance of Outlook
    If OutApp Is Nothing Then
    
        'if no instance of outlook was found, create one
        Set OutApp = CreateObject("Outlook.Application")
        'remember that we created a new instance of outlook
        OLbCreated = True
        
    End If
    
    'create a new mail item (message)
    Set OutMail = OutApp.CreateItem(olMailItem)
    
    'set properties of the New Mail Item (Message)
    With OutMail
        .To = "John_Doe@yahoo.com"
        .CC = ""
        .BCC = ""
        .Subject = "File Embedded"
        
        'Body could be text, here we set the Content of an Existing Hardcodesd File
        .Body = Word_Doc.Content
       
        'use EITHER Send or Display Method, not both... send attempts to send in the
        'background, while Display will show the message, requiring the user to
        'click send
'        .Send
        .display
        
    End With
    
    'quit Outlook if we created a new instance, and reset object and item to nothing
    If OLbCreated Then OutApp.Quit
    Set OutMail = Nothing
    Set OutApp = Nothing
    
    'quit the new instance of Word we created, and set object to nothing
    Word_Doc.Close False
    WordApp.Quit False

    Set Word_Doc = Nothing
    Set WordApp = Nothing

End Sub
 
Upvote 0

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
I actually was going to do this out of word if possible. I just got your code. I am going to try and use it now. Thanks again for so much help.
 
Upvote 0

Forum statistics

Threads
1,215,374
Messages
6,124,569
Members
449,173
Latest member
Kon123

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