I want to:
Select all emails from a certain sender
Copy the body of the email to a new Word document
Save the word document to a specific directory
Clear the clipboard
I'd like to know what else I need to do, especially in the the FindSetAside() and SaveToDicrectory() functions.
Sub FindSetAside() 'find all set-aside emails
End Sub
Sub PasteToWord()
Dim Word As Word.Application
Dim Doc As Word.Document
Dim activeMessage As Outlook.MailItem 'the email to copy
Dim activeBody As String
If TypeName(ActiveExplorer.Selection.Item(1)) = "MailItem" Then
'get the active email
Set activeMessage = ActiveExplorer.Selection.Item(1)
'setup Word
Set Word = CreateObject("Word.Application")
WordApp.Visible = True
setDoc = Word.Documents.Add
'Copy selection to document
activeMessage.GetInspector().WordEditor.Range.FormattedText.Copy
Doc.Range.Paste
Call ClearClipboard
End If
End Sub
Sub SaveToDirectory() 'Save the Word Document to the correct directory
End Sub
Public Sub ClearClipboard()
Dim Data As New DataObject
Data.SetText Tex:=Empty
Data.PutInClipboard
End Sub
I am using Outlook 2010. I am also considering adding some code to send the Word document as an attachment to specific emails, but perhaps that is irrelevant to this question.
Using Explorer.Selection won't help if you need to execute a search for certain emails (unless you use Explorer.Search, which executes a search in the UI). Take a look at this to help determine the search method you need to use:
https://msdn.microsoft.com/EN-US/library/ff869846.aspx
Then it is just a matter of traversing the returned collection and accessing the MailItem objects within.
To save the Word document, use Document.SaveAs2.
Related
I'm trying to create a macro in Outlook to allow me to select text and convert that text to 'code' (Courier New, black). I'm using Outlook for Microsoft 365 which (I think) uses the Word editor...
I've copied some code I found in another answer and tweaked it, but no dice, and I fear I'm missing something obvious.
Public Sub FormatSelectedText()
Dim objItem As Object
Dim objInsp As Outlook.Inspector
Dim objWord As Word.Application
Dim objDoc As Word.Document
Dim objSel As Word.Selection
On Error Resume Next
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
With objSel
.Font.Name = "Courier New"
.Font.Color = RGB(0, 0, 0)
End With
End If
End If
End If
End Sub
I'm not a VBA guy, so it's likely something obvious :) I'm not even sure if it's a problem with the code or with the general macro support.
FWIW, in the past I wrote a separate VBA script to throw a warning message when I send an email to multiple people with different email domains (to avoid accidental data cross-sharing) which works fine, so I know a little bit...
To be clearer, I have added this code into a macro within the ThisOutlookSession object
I then open a new email and add some random text, select some of the text and run the macro, but the text in the email body doesn't change.
The code works correctly. You just need to add a COM reference to the Word object model to be able to use the Word object model in Outlook VBA macros.
In VBA editor window, click the “Tools” button in the menu bar.
Then, from the drop down list, select the “References” option.
The “References – Project 1” dialog box will display.
In this dialog box, you can pull the scrolling bar down until you locate what you need, in your case it is “Microsoft Word 16.0 Object Library”.
Mark the checkbox in front of the required entry and click “OK”.
Now you have added the Word object library reference successfully.
I'd like to remove the following phrase from the beginning of every email from sources other than a specific domain:
"This is an EXTERNAL email. Do not click links or open attachments unless you validate the sender and know the content is safe."
I am trying to delete this phrase from each email.
I grabbed some code online from another person trying to do something similar.
Sub Del()
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
Dim search As String
search = "This is an EXTERNAL email. Do not click links or open attachments unless you validate the sender and know the content is safe."
Dim para As Paragraph
For Each para In Document.Paragraphs
Dim txt As String
txt = para.Range.Text
If InStr(txt, search) Then
para.Range.Delete
End If
Next
End Sub
I get
Run-time error '91'
on line 9 Set Document.
that works a lot easier within Outlook:
sub delete_part_of_text()
Dim search As String
search = "This is an EXTERNAL email. Do not click links or open attachments unless you validate the sender and know the content is safe."
If Not TypeOf Item Is mailitem Then Exit Sub
Item.HTMLBody = Replace(Item.HTMLBody, search, "")
end sub
of course you have to adapt this - should this run automatically on each incoming mail oder are you going to call this when needed for one/some mails? That changes the call of the procedure, but the princip abobe works.
Max
I didn't find this question anywhere, so I don't if it's even possible because we can't record macro in Outlook.
But, I'd like to clear all text formatting in my e-mail that I'm replying by a macro in Outlook.
Since I'm using olReply.HTMLBody and building all the body with a Loop and some if and elses, I couldn't format the body in it.
Example:
Clear all text formatting - Outlook
Thanks!
I'm not sure how your code looks like but here is quick example using ClearFormatting Method Make sure you click Reply or ReplyAll and select the text you are trying to format then run the code
Option Explicit
Public Sub Exmple()
Dim wDoc As Word.Document
Dim rngSel As Word.selection
If Application.ActiveInspector.EditorType = olEditorWord Then
Set wDoc = Application.ActiveInspector.WordEditor ' use WordEditor
Set rngSel = wDoc.Windows(1).selection ' Current selection
rngSel.ClearFormatting
End If
Set wDoc = Nothing
End Sub
Is there any method in Outlook VBA equivalent to Excel's FollowHyperlink?
I made an Excel VBA macro that posts the text in the clipboard to a specific site using FollowHyperlink method.
I want to do the same thing with Outlook VBA but I couldn't find the method.
Any method that does the same thing as simply as FollowHyperlink is quite fine.
Here is the Excel VBA version of a function that post the content of the clipboard to Google translation. I want to make an Outlook version of this one.
Public Sub GoogleTranslate_2EN()
Dim clipBoard As New DataObject
With clipBoard
On Error Resume Next
.GetFromClipboard
Dim targetText As String
targetText = .GetText
On Error GoTo 0
End With
Dim URL As String
URL = "https://translate.google.co.jp/?hl=en&tab=wT#auto/en/" & targetText
ThisWorkbook.FollowHyperlink Address:=URL, newWindow:=False, AddHistory:=True
End Sub
I've decided to use Word.Application object and use "FollowHyperlink" from a Word object. It worked!
Now I can Google translate emails in Germany to English as soon as I receive them. :-)
Public Sub ClipboardGoogleTranslate_2EN()
Dim wdApp As Word.Application
Set wdApp = CreateObject("Word.Application")
wdApp.Run "ClipboardGoogleTranslate_2EN"
wdApp.Quit
Set wdApp = Nothing
End Sub
I am currently in the process of anonymizing emails with vba for a project.
Currently when an email is received I have created a rule which moves the email into a folder, thereafter I run a script using the getlast function, in that specific folder, where I display the newly received email and copy the content of the email and then i paste this into a new email and send to a particular email address. This effectively removes the identifying features of the email.
The final movement to the Rubik's Cube is to find the email signature and replace with blank, in other words delete the signature. I will have the email signatures to do this, however it would be great if someone someone could help with this...
I found this solution.
'I also had to add Word in tools/references of VBA and then Call DeleteSig(msg) after displaying the item.
'http://www.access-programmers.co.uk/forums/showthread.php?t=259417
Sub DeleteSig(msg As Outlook.MailItem)
Dim objDoc As Word.Document
Dim objBkm As Word.Bookmark
On Error Resume Next
Set objDoc = msg.GetInspector.WordEditor
Set objBkm = objDoc.Bookmarks("_MailAutoSig")
If Not objBkm Is Nothing Then
objBkm.Select
objDoc.Windows(1).Selection.Delete
End If
Set objDoc = Nothing
Set objBkm = Nothing
End Sub
It's not very hard, but depend on some properties of the mail:
Is the mail plain text? or html
if HTML, do you have the signature in html format ? (with tags)
I presume you read your mail with a variable of type Outlook.MailItem
' Definition of your mail
Dim OutMail As Outlook.MailItem
dim signature as string
...
with OutMail
.HTMLBody = Replace(.HTMLBody, signature, "")
End with
If the signature you have is not the exact match, it'll be a little more tricky.
I suggest to create a function that detect the signature and return the HTMLBody without it