PDA

View Full Version : [SOLVED:] Inserting word file as text into email body using VBA



Tbomb65
05-29-2017, 01:13 PM
Team,

I am trying to add into the body of an already open email (Be it a reply or a new email) at whichever point the cursor currently is an "attachment as text" using VBA. In this case the text is recorded in word.

Effectively I want to automate doing the Attach File > Insert As Text command and specify a file path so that I can create a button that allows a one click solution

I have found lots of code that adds it as an attachment to the email, or even creates a new email but nothing specifically for what I am wanting.

Hoping to utilise this line of code which I found for a template creation macro which changes the source file destination address based on the user environments name. Kinda cool I thought, but without that I will manage fine.




Set newItem = Application.CreateItemFromTemplate("C:\Users\" & Environ("UserName") & "\AppData\Roaming\Microsoft\Templates\example.docx")


Any help would be great.

Cheers,

gmayor
05-29-2017, 08:47 PM
I told you how to do something almost identical yesterday. All that is required is to substitute the file insertion for the object insertion, and you can get the syntax by recording the insertion in Word. The only proviso is that Word document format is not identical to HTML e-mail format and thus there are likely to be formatting anomalies, depending what is in the inserted document.


Option Explicit

Sub InsertWordDocument()
Dim oRng As Object
Const strFilename As String = "C:\Path\DocName.docx" 'The Document file to be inserted

On Error GoTo ErrHandler
If TypeName(ActiveWindow) = "Inspector" Then
'ensure that the cursor is in the body of the message
If ActiveInspector.IsWordMail And ActiveInspector.EditorType = olEditorWord Then
'Set a range to the cursor position
Set oRng = ActiveInspector.WordEditor.Application.Selection
'Insert the file at the range
oRng.InsertFile _
fileName:=strFilename, _
Range:="", _
ConfirmConversions:=False, _
Link:=False, _
Attachment:=False
End If
End If
lbl_Exit:
Exit Sub
ErrHandler:
Beep
Resume lbl_Exit
End Sub

Tbomb65
05-30-2017, 06:41 AM
Thank you, I knew the change would be easy from yesterday but my simple skills couldnt workout what to change the wording to in the insert section. The template I have the formatting looks almost identical so win there.