A Microsoft Outlook email forum. Outlook Banter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » Outlook Banter forum » Microsoft Outlook Email Newsgroups » Outlook and VBA
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

Troubles adding a macro to Mail Message - help please



 
 
Thread Tools Search this Thread Display Modes
  #1  
Old January 26th 06, 06:13 PM posted to microsoft.public.outlook.program_vba
[email protected]
external usenet poster
 
Posts: 3
Default Troubles adding a macro to Mail Message - help please

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

Ads
  #2  
Old January 26th 06, 08:45 PM posted to microsoft.public.outlook.program_vba
Sue Mosher [MVP-Outlook]
external usenet poster
 
Posts: 11,651
Default Troubles adding a macro to Mail Message - help please

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
http://www.turtleflock.com/olconfig/index.htm
and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
http://www.outlookcode.com/jumpstart.aspx


wrote in message ups.com...
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

  #3  
Old January 26th 06, 09:01 PM posted to microsoft.public.outlook.program_vba
[email protected]
external usenet poster
 
Posts: 3
Default Troubles adding a macro to Mail Message - help please

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??

  #4  
Old January 26th 06, 10:36 PM posted to microsoft.public.outlook.program_vba
callmedoug
external usenet poster
 
Posts: 13
Default Troubles adding a macro to Mail Message - help please

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.

  #5  
Old January 27th 06, 12:01 AM posted to microsoft.public.outlook.program_vba
Sue Mosher [MVP-Outlook]
external usenet poster
 
Posts: 11,651
Default Troubles adding a macro to Mail Message - help please

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
http://www.turtleflock.com/olconfig/index.htm
and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
http://www.outlookcode.com/jumpstart.aspx


"callmedoug" wrote in message ups.com...
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.

 




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Outlook 2003 Free/Busy Troubles vairvixen Outlook - Calandaring 0 February 15th 06 01:57 AM
E-mail message stoney123 Outlook - Using Contacts 1 February 10th 06 08:45 PM
In Outlook how do I get the Inbox to be above the mail message? Ewheeler Outlook - General Queries 1 February 1st 06 03:37 PM
how to change the mail message body Ram Add-ins for Outlook 10 January 27th 06 06:21 PM
Macro to selectively delete message from POP3 server alainr Outlook and VBA 3 January 20th 06 01:42 PM


All times are GMT +1. The time now is 08:51 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.Search Engine Friendly URLs by vBSEO 2.4.0
Copyright ©2004-2025 Outlook Banter.
The comments are property of their posters.