Pass variables from Outlook to Word

  • Thread starter Thread starter Matthew
  • Start date Start date
M

Matthew

I am calling a word macro from an Outlook macro, and want to pass a couple
of variables as shown below.

I think the variables need to be defined inside the Outlook function, as the
variables are generated from the date the e-mail message was created. I am
calling it with a "message rule".

Not sure what methods are available; could I call the macro directly and
pass the variables directly; or write the variables to a text document, call
the word macro, and have it check the document?

Any suggestions would be welcome.

Matthew

Complete Outlook code below. Variables I want to pass are Folder and fName.

Sub SaveAttachment(Item As Outlook.MailItem)
On Error GoTo GetAttachments_err
'MsgBox "Script is running. " & Item.SenderName
Dim ns As NameSpace
Dim Inbox As MAPIFolder
'Dim Item As Object
Dim Atmt As Attachment
Dim FileName As String
Dim Folder As String
Dim fName As String
Dim i As Integer
Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
Set fName = Format(Item.CreationTime, "yyyy mm dd") & Right(Atmt.FileName,
4)
i = 0

If Item.SenderName = "Operator Account" Then
Set Folder = "Wood Magazine\"
ElseIf Item.SenderName = "Lisa Stram" Then
Set Folder = "CWB\"
ElseIf Item.SenderName = "LEADlink" Then
Set Folder = "American Woodworker\"
'ElseIf Item.SenderName = "Alycia Schulz" Then
'Folder = "Alycia Schulz\"
Else
MsgBox "You need to install user " & Item.SenderName
Set Folder = ""
End If

For Each Atmt In Item.Attachments
' This path must exist!
FileName = "C:\My Documents\Sales Leads\" & Folder & fName
Atmt.SaveAsFile FileName
i = i + 1
Next Atmt

MsgBox "Script has run." & vbCrLf & "I found " & i & " attached files.",
vbInformation, "Message"

Set WordApp = CreateObject("Word.Application")
' Create a new, empty document.
Set WordDoc = WordApp.Documents.Open("C:\Documents and Settings\Matthew
Osborne\Desktop\Doc1.doc")
WordDoc.Application.Visible = True
'Word Macro set to Auto_Open

' Clear memory
GetAttachments_exit:
Set Atmt = Nothing
Set Item = Nothing
Set ns = Nothing
Exit Sub
' Handle errors
GetAttachments_err:
MsgBox "An unexpected error has occurred." _
& vbCrLf & "Please note and report the following information." _
& vbCrLf & "Macro Name: GetAttachments" _
& vbCrLf & "Error Number: " & Err.Number _
& vbCrLf & "Error Description: " & Err.Description _
, vbCritical, "Error!"
Resume GetAttachments_exit

End Sub
 
Never mind; I got it figured out.

Code, if anybody is interested:

Open "C:\My Documents\Sales Leads\ToPrint.txt" For Append As #1
Write #1, Folder; fName
Close #1

Matthew
 
I imagine that the Word method Application.Run should do just what you want.
Watch out if you want the macro to RETURN a value to your Outlook macro. You
need Word 2000 or later to do that.

-Tom
 
Back
Top