How can I write code to copy the subject line from a new message in Outlook and paste it into an existing word document.
Thank You
Harley
Printable View
How can I write code to copy the subject line from a new message in Outlook and paste it into an existing word document.
Thank You
Harley
In Outlook Go to Tools > Macro > Visual Basic Editor.
Add a reference to Word, go to Tools > References > select "Microsoft
Word x.x Object Library".
Then add the following code.
Code:Private Sub Application_NewMail()
Dim oNS As Outlook.NameSpace
Dim oInbox As Outlook.MAPIFolder
Dim oEmail As Outlook.MailItem
Dim oWord As Word.Application
Dim i As Integer
Dim sSub As String
Set oNS = Application.GetNamespace("MAPI")
Set oInbox = oNS.GetDefaultFolder(olFolderInbox)
Set oWord = New Word.Application
oWord.Documents.Open "C:\EmailSubjects.doc"
i = oInbox.Items.Count
Do While i > 0 And oInbox.UnReadItemCount > 0
DoEvents
'oWord.Visible = True
If oInbox.UnReadItemCount > 0 Then
Set oEmail = oInbox.Items.Item(i)
sSub = oEmail.Subject
If oEmail.UnRead = True Then
oEmail.UnRead = False
oWord.Selection.EndKey wdStory
oWord.Selection.TypeText "Subject: " & sSub & vbNewLine
Endif
Set oEmail = Nothing
End If
Set oInbox = Nothing
Set oInbox = oNS.GetDefaultFolder(olFolderInbox)
i = i - 1
Loop
oWord.Quit True
Set oWord = Nothing
Set oInbox = Nothing
Set oNS = Nothing
End Sub