Troubles adding a macro to Mail Message - help please

G

Guest

Hi,

I wrote a marco that would allow me to create and attach a lotus notes
DocLink to my outlook mail message, while the code works fine as a
macro, it only works when the macro is part of the "Microsoft Outlook
Session" module. What I really want to do with this code is create a
macro button that resides on the mail message document itself.

I have tried opening a message and then creating a macro there (I think
the module is located in "Normal"

Anyway I get the following error when I run the code
"Variable not defined" and it points to the following line of code
Set EmailMessage = Outlook.ActiveInspector.CurrentItem
(highlighting the word Outlook)

I suspect that the message itself does not have access to all the same
procedures and functions as the Session does.



So here is my question, How can I add a command button to the Mail
Message toolbar that will run the following code? Where would I put
this code, and how do I link it to a command button in the mail
message?

Thanks



---------Code------------------------
Option Explicit

Public Sub DocLink()

' Stores the filename provide by the user
Dim strFileName As String
' create a clipboard item
Dim MyDataObj As New DataObject
' clipboard text item
Dim MyDocLink As Variant
' DocLink file and path
Dim MyLinkPath As String


'Display the input box with the default 'Titles.Txt'
strFileName = InputBox("Enter a filename for DocLink", "Provide
filename...", "")

'Check if the user has pressed Cancel (Inputbox returns a zero length
string)
If strFileName = "" Then
Exit Sub
End If


' Open the file for exporting the link.
' file created in the Root Folder
MyLinkPath = "c:\" & strFileName & ".ndl"
Open MyLinkPath For Output As #1

'copy the clipboard to dataobject
MyDataObj.GetFromClipboard
MyDocLink = MyDataObj.GetText
Print #1, MyDocLink

'close the ndl file
Close #1

'add attached doclink
Set EmailMessage = Outlook.ActiveInspector.CurrentItem
With EmailMessage
.Attachments.Add (MyLinkPath)
End With

'delete the DocLink file from the hardrive
Kill (MyLinkPath)



End Sub
 
S

Sue Mosher [MVP-Outlook]

Outlook messages don't have macros associated with them. The only macros in Outlook are in the VBA environment that you can open from the main Outlook window with Alt+F11.

From your description, it sounds like you are using Word as the email editor and thus have created a macro in Word. You can add one line of code to instantiate an Outlook.Application object that your Word macro can use:

Set Outlook = CreateObject("Outlook.Application")


--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
G

Guest

unfortunately I am not using word, If I were then I could build the
file there.

What actually happens is as follows:

In Lotus Notes there is a copy function called DocLink. it allows you
to click on a database object and create a link directly to that
object. Our company use to standardize on Notes and many databases are
still there, but we use Outlook now for email. The problem is there is
no easy way to create a DocLink back to notes. The work around we have
is to go to the notes database and find the link, right click on it and
choose copy doclink. then take that link into notepad and save it as a
..ndl file, and then attach that file to the outlook email. ......way
too much work for most people so they don't do it.

My marco grabs the doclink copied to the clipboard and automatically
pastes it into a txt file and changes the extension then adds it to the
email.

The problem is that with the button on the Main outlook page, I
actually have to click the button twice - once to change focus to the
mail outlook page and a second time to perfomr the macro, now my email
is under the application and ihave to click on it in the taskbar to get
focus back. Also I get an error if a mail message is not already open
(Yes i can put in a check for this)

I would really like to find a way to put a button on the command bar
within the mail window. any other suggestions??
 
C

callmedoug

OK I solved my own problem.....

I opened a new mail message, clicked in the body, this allowed me to
add a macro. I then opened VBA and added my code into the module1. I
had to also add the "Microsoft Outlook 11.0 Object Library" reference,
which was what I was missing. I was then able to add the macro to the
Mail Message directly and it works perfectly.
 
S

Sue Mosher [MVP-Outlook]

The reference is necessary because you're in Word VBA but only if you want to be able to use intellisense and declare Outlook object variables (which is a good thing).

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top