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