VBA Outlook How to add hyperlink into email body - vba

This macro adds hyperlink to email :
Sub test_add_hyperlink()
Dim NewMail As Outlook.MailItem
Set NewMail = Application.ActiveInspector.CurrentItem
NewMail.HTMLBody = "<HTML><BODY><A href=http://www.someaddress.com>URL_TEXT</A></BODY></HTML>" & NewMail.HTMLBody End Sub
but how to add hyperlink in place where active cursor is ? I ask beacause I would like to add hyperlink not at the front of message, but where my currently writing message.
The hyperlink I would like to add is the hyperlink to file which is currently copied to Windows' clipboard, this part I've written, but I can't figure out how to place it not at the front of email, but in place where active cursor is. I think that macro based emulation of Windows' keypressing is one of the directions to follow.

This describes how to paste at the selection.
http://www.slipstick.com/developer/code-samples/paste-formatted-text-vba/
Sub PasteFormattedClipboard()
Dim objItem As Object
Dim objInsp As Outlook.Inspector
Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim objSel As Word.Selection
Set objItem = Application.ActiveInspector.CurrentItem
Set objInsp = objItem.GetInspector
Set objDoc = objInsp.WordEditor
Set objWord = objDoc.Application
Set objSel = objWord.Selection
objSel.PasteAndFormat (wdFormatOriginalFormatting)
Set objItem = Nothing
Set objInsp = Nothing
Set objDoc = Nothing
Set objWord = Nothing
Set objSel = Nothing
End Sub

Sub InsertHyperlinkAtCursorPositon()
On Error GoTo finish
strLink = "http://www.outlookcode.com"
strLinkText = "Get Outlook code samples here"
Set objInsp = Application.ActiveInspector
Set objMsg = objInsp.CurrentItem
Set objDoc = objInsp.WordEditor
Set objSel = objDoc.Windows(1).Selection
If objMsg.BodyFormat <> olFormatPlain Then
objDoc.Hyperlinks.Add objSel.Range, strLink, _
"", "", strLinkText, ""
Else
objSel.InsertAfter strLink
End If
finish:
End Sub

Related

How to insert a content control checkbox into an Outlook task using Word VBA?

I would like add a working checkbox (content control?) to an existing task, like a list of sub-tasks, that could just be checked off.
Within Outlook I referenced the Microsoft Word 16.0 Object Library, and I have tried the suggestions at http://www.vboffice.net/en/developers/use-word-macro-in-outlook/ and https://www.slipstick.com/developer/word-macro-apply-formatting-outlook-email/ without success.
I tried
Option Explicit
Public Sub Checkbox()
Dim objItem As Object
Dim objInsp As Outlook.Inspector
' Add reference to Word library
' in VBA Editor, Tools, References
Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim objSel As Word.Selection
On Error Resume Next
' Reference the current Outlook item
Set objItem = Application.ActiveInspector.CurrentItem
If Not objItem Is Nothing Then
If objItem.Class = olMail Then
Set objInsp = objItem.GetInspector
If objInsp.EditorType = olEditorWord Then
Set objDoc = objInsp.WordEditor
Set objWord = objDoc.Application
Set objSel = objWord.Selection
' Formatting code goes here
Selection.Range.ContentControls.Add (wdContentControlCheckBox)
End If
End If
End If
Set objItem = Nothing
Set objWord = Nothing
Set objSel = Nothing
Set objInsp = Nothing
End Sub
I have also tried
Public Sub Check2()
Dim Ins As Outlook.Inspector
Dim Document As Word.Document
Dim Word As Word.Application
Dim Selection As Word.Selection
Set Ins = Application.ActiveInspector
Set Document = Ins.WordEditor
Set Word = Document.Application
Set Selection = Word.Selection
Selection.Range.ContentControls.Add (wdContentControlCheckBox)
End Sub
The first one didn't do anything, as I recall.
The second one showed
Run-time error '445'".
Object doesn't support this action
Your VBA code works correctly:
Public Sub Check2()
Dim Ins As Outlook.Inspector
Dim Document As Word.Document
Dim Word As Word.Application
Dim Selection As Word.Selection
Set Ins = Application.ActiveInspector
Set Document = Ins.WordEditor
Set Word = Document.Application
Set Selection = Word.Selection
Selection.Range.ContentControls.Add (wdContentControlCheckBox)
End Sub
You just needed to add a reference to the Word object library (Tools -> References):

Using Word VBA outlook email body is blank

I made the following vba script in outlook and it works fine when I get the body of the email. I moved the script to word vba and now when I get the email body its empty. I can access the subject and other fields fine but the email body field is blank. How can I access the body of the email?
Dim appOutlook As Object
Dim olNs As Object
Dim olFolder As Object
Dim olItem As Object
Dim iRow As Integer
Dim email_body As String
' Get/create Outlook Application
On Error Resume Next
Set appOutlook = GetObject(, "Outlook.Application")
If appOutlook Is Nothing Then
Set appOutlook = CreateObject("Outlook.Application")
End If
On Error GoTo 0
Set olNs = appOutlook.GetNamespace("MAPI")
Set olFolder = olNs.GetDefaultFolder(6) _
.Parent.Folders("folder2") ' 6 == Inbox for some reason
For iRow = 1 To olFolder.Items.Count
Next iRow
For Each myItem In olFolder.Items
myItem.Display
Dim Email As Outlook.MailItem
Set Email = appOutlook.ActiveInspector.CurrentItem
myItem.Close olDiscard
'Word document
Dim wdApp As Word.Application
Set wdApp = CreateObject("Word.Application")
Dim wdDoc As Word.Document
Set wdDoc = wdApp.Documents.Add
wdDoc.Activate
Dim wdRange As Word.Range
Set wdRange = wdDoc.Range(0, 0)
email_body = Email.Body
In the following code you iterate over all items in the folder and display each item in a new inspector window in Outlook:
For Each myItem In olFolder.Items
MsgBox myItem.Body
There is no need to call the Display method to get the actual mail item. Instead, you can use the existing reference.

Save email body to Word document

My goal is copy and past body of active email from Outlook to the MS Word, and save Word to specified destination.
Code
Dim objMail as Outlook.MailItem
Dim objWord As Object
Dim objDocument As Object
Dim objFSO As Object
Dim objTextStream As Object
Set objMail = Application.ActiveInspector.CurrentItem
Set objWord = CreateObject("Word.Application")
Set objDocument = objWord.Documents.Add
objMail.GetInspector().WordEditor.Range.FormattedText.Copy
objDocument.Range.Paste
Its a right way ?
You can check, if you really selected an email (either within the list or opened) and copy its formatted body like this:
Private Sub CopyEMailBodyToWord()
Dim objOutlook As Outlook.Application
Dim objMail As Object 'Outlook.MailItem, but has to be checked later
Dim objWord As Object
Dim objDocument As Object
Set objOutlook = Outlook.Application
Select Case TypeName(objOutlook.ActiveWindow)
Case "Explorer" ' get current item in list view
Set objMail = objOutlook.ActiveExplorer.Selection.Item(1)
Case "Inspector" ' get open item
Set objMail = objOutlook.ActiveInspector.CurrentItem
End Select
If objMail.Class = olMail Then
Set objWord = GetObject(, "Word.Application")
If objWord Is Nothing Then Set objWord = CreateObject("Word.Application")
Set objDocument = objWord.Documents.Add
' copy formatted body:
objMail.GetInspector.WordEditor.Range.FormattedText.Copy
objDocument.Range.Paste
' or copy text only:
'objDocument.Range.Text = objMail.Body
With objWord.FileDialog(msoFileDialogSaveAs)
.Title = "Save ..."
.InitialFileName = objWord.Options.DefaultFilePath(wdDocumentsPath) & _
"\" & objMail.Subject & ".docx"
If .Show <> False Then
objDocument.SaveAs _
FileName:=.SelectedItems(1), _
AddToMru:=False
End If
End With
End If
End Sub
Is this what you are trying to do?
Option Explicit
Public Sub Example()
Dim Email As Outlook.MailItem
Set Email = Application.ActiveInspector.CurrentItem
'Word document
Dim wdApp As Word.Application
Set wdApp = CreateObject("Word.Application")
Dim wdDoc As Word.Document
Set wdDoc = wdApp.Documents.Add
wdDoc.Activate
Dim wdRange As Word.Range
Set wdRange = wdDoc.Range(0, 0)
'Add email to the document
wdRange.Text = Email.Body
wdApp.Visible = True
wdDoc.SaveAs2 FileName:="C:\Temp\Example.docx", FileFormat:= _
wdFormatXMLDocument, CompatibilityMode:=15
End Sub
You may also wanna work with ActiveWindow.Class to avoid any error on your CurrentItem

Formatting appointment body

I am trying to schedule a meeting automatically from Excel.
It is simple, unless you are trying to format the body text.
I made some research about GetInspector.
It looks like I have to copy the text from another place, but the commands I found are incorrect or not functional. Even trying to format it as RTF, but .RTFBody is not a property of the AppointmentItem object
Find my code:
Dim oApp As Object
Dim oMail As Object
Set oApp = CreateObject("Outlook.Application")
Set oMail = oApp.CreateItem(1)
With oMail
.Subject = ""
.Location = ""
'.Start =
'.Duration =
.body = " < not formattable text >"
.display
End With
Set oApp = Nothing
Set oMail = Nothing
I once asked this question, and got an answer here (http://www.slipstick.com/developer/code-samples/paste-formatted-text-vba/)
Try the following. You will need to set a reference to the Word Object Model, and the formatted text should be stored in the clipboard, and note, you have to have the .display line before the .body line for there to be something to work with:
Dim oApp As Object
Dim oMail As Object
Set oApp = CreateObject("Outlook.Application")
Set oMail = oApp.CreateItem(1)
With oMail
.Subject = ""
.Location = ""
'.Start =
'.Duration =
' .body = " < not formattable text >"
.display
End With
Dim objItem As Object
Dim objInsp As Outlook.Inspector
Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim objSel As Word.Selection
Set objItem = oMail ' Application.ActiveInspector.currentItem
Set objInsp = objItem.GetInspector
Set objDoc = objInsp.WordEditor
Set objWord = objDoc.Application
Set objSel = objWord.Selection
objSel.PasteAndFormat (wdFormatOriginalFormatting)
'objSel.PasteAndFormat (Word.WdRecoveryType.wdFormatOriginalFormatting)
Set objItem = Nothing
Set objInsp = Nothing
Set objDoc = Nothing
Set objWord = Nothing
Set objSel = Nothing
Set oApp = Nothing
Set oMail = Nothing
Hth

Programmatically change properties in email body in Outlook with VBA

I have an email ready to be sent in Outlook 2013
I want to scan the body of the email for bold text (i.e., bold characters) and change its color to red
(nice to have) Exclude from the macro the signature
I put together the code below but still not working. Any ideas?
Public Sub FormatSelectedText()
Dim objItem As Object
Dim objInsp As Outlook.Inspector
' Add reference to Word library
' in VBA Editor, Tools, References
Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim objSel As Word.Selection
On Error Resume Next
'Reference the current Outlook item
Set objItem = Application.ActiveInspector.CurrentItem
If Not objItem Is Nothing Then
If objItem.Class = olMail Then
Set objInsp = objItem.GetInspector
If objInsp.EditorType = olEditorWord Then
Set objDoc = objInsp.WordEditor
Set objWord = objDoc.Application
Set objSel = objWord.Selection
Set objChar = Characters.Selection
' replace the With block with your code
With objChar
' Formatting code goes here
'.Font.Size = 18
If .Font.Bold = True Then
.Font.Color = wdColorBlue
End If
.Font.Color = wdColorRed
'.Font.Italic = True
'.Font.Name = "Arial"
End With
For Each Char In Characters.Selection
If Char.Font.Bold Then
Char.Font.Color = RGB(0, 0, 255) 'TextRGBTmp
End If
Next Char
For Each Char In Characters.Selection
If Not Char.Font.Bold And Char.Font.Color = RGB(0, 0, 255) Then
Char.Font.Color = RGB(0, 0, 0)
End If
Next Char
End If
End If
End If
Set objItem = Nothing
Set objWord = Nothing
Set objSel = Nothing
Set objInsp = Nothing
End Sub
This is a follow up to question: Programmatically change font properties in email body
first of all: don't use On Error Resume Next when you're trying to debug your code. It makes your life harder.
second: use Option Explicit at the beginning of the module. With that option enabled, VBA will show you every variable that's not initialized (some bugs only occur from misspellings).
I've corrected your code, so it works for me:
Public Sub FormatSelectedText()
Dim objOutlook As Outlook.Application ' i used this because im working in MS Access
Dim objItem As Object
Dim objInsp As Outlook.Inspector
' Add reference to Word library
' in VBA Editor, Tools, References
Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim objSel As Word.Selection
Dim objChar As Object
Dim Char As Object
'Reference the current Outlook item
Set objOutlook = GetObject(, "Outlook.Application")
Set objItem = objOutlook.ActiveInspector.CurrentItem
If Not objItem Is Nothing Then
If objItem.Class = olMail Then
Set objInsp = objItem.GetInspector
If objInsp.EditorType = olEditorWord Then
Set objDoc = objInsp.WordEditor
Set objWord = objDoc.Application
Set objSel = objWord.Selection
Set objChar = objSel.Characters ' this wasn't initialized
' replace the With block with your code
' With objChar ' you don't Need this block because objChar is an array and it throws an error when you try to use this code on the whole objChar object
' ' Formatting code goes here
' '.Font.Size = 18
' If .Font.Bold = True Then
' .Font.color = wdColorBlue
' End If
' .Font.color = wdColorRed
' '.Font.Italic = True
' '.Font.Name = "Arial"
' End With
For Each Char In objSel.Characters
If Char.Font.Bold Then
Char.Font.color = rgb(255, 0, 0) 'TextRGBTmp (the rgb was filled backwards, so the text became blue. i fixed it.
End If
Next Char
' the code of the second For Each was not neccessary.
End If
End If
End If
Set objItem = Nothing
Set objWord = Nothing
Set objSel = Nothing
Set objInsp = Nothing
End Sub