Microsoft VBA for Outlook becoming Disabled

D

David Kaplan

I've created my first VBA app which runs great, but then after I shut
down Outlook and restart I receive the following error messages, and
would appreciate any assistance! DHK

====
Outlook experienced a serious error last time the add-in Microsoft VBA
for Outlook was opened. Would you like to disable this add-in?

To reactivate this add-in, click about Microsoft Office on the help
menu and then click disabled items.
=====

I do this and find the message: "Add-in: Microsoft VBA for Outlook
Add-in (outlvba.dll)" is disabled. I re-enable it and it works till
the next time I shut down!

Code is meant to copy a contact name/address to the clipboard, when it
is open in the business card format:

************************

Attribute VB_Name = "Module1"
Sub CopyToClipboard()
'Set up objects
Dim myOlApp As Object
Dim objItem As Object
Dim Response
Dim strToCopy As String
Dim strCompany As String

Set Clipboard = New DataObject

'Sets up an Outlook App for an Inspector
Set myOlApp = CreateObject("Outlook.Application")

'Check to ensure Outlook item is selected

If TypeName(myOlApp.ActiveInspector) = "Nothing" Then
MsgBox "Contact not open. Exiting", vbOKOnly + vbInformation
Exit Sub
End If

Set objItem = myOlApp.ActiveInspector.CurrentItem

If objItem.CompanyName = "" Then
strCompany = ""
Else
strCompany = objItem.CompanyName & vbCrLf
End If

If objItem.BusinessAddress <> "" Then
strToCopy = objItem.FullName & vbCrLf & strCompany &
objItem.BusinessAddress
Else
strToCopy = objItem.FullName & vbCrLf & strCompany &
objItem.HomeAddress
End If

Clipboard.SetText strToCopy
Clipboard.PutInClipboard

Response = MsgBox("Address Copied to Clipboard!", vbOKOnly)

Set myOlApp = Nothing
Set objItem = Nothing
Set Clipboard = Nothing


End Sub
 
K

Ken Slovak - [MVP - Outlook]

See if it works any better using the intrinsic Application object
exposed in Outlook VBA. Generally use of the Clipboard object isn't
supported in VBA code, only in VB code unless you use the Win32 API
functions to work with the Clipboard.
 
D

David Kaplan

See if it works any better using the intrinsic Application object
exposed in Outlook VBA. Generally use of the Clipboard object isn't
supported in VBA code, only in VB code unless you use the Win32 API
functions to work with the Clipboard.

Could I impose on you to be more specific about the line of code to
set up the intrinsic App object? Only intrinsic I found in searching
related to MAPI...

Thanks... DHK
 
K

Ken Slovak - [MVP - Outlook]

Instead of setting an Outlook.Application object use the verb
Application instead. In Outlook VBA that's an intrinsic object.
 
D

David Kaplan

Instead of setting an Outlook.Application object use the verb
Application instead. In Outlook VBA that's an intrinsic object.

Thanks Ken, But I'm still beating my head against a wall. The code
works great, puts exactly what I want into the clipboard, and gives me
no error messages at run time. I can step thru it and get no errors.
However, when I quit Outlook, the next time I start it, the VBA add in
is disabled, and I have to go thru the help panel to re-enable it.

Here's the latest iteration of my code.

I don't really understand your reference to an intrinsic verb. What
*exact* code change would I make to the following? I'm used to VB5
programming, but have not programmed in vba and the newer versions.

Many thanks...


Sub CopyToClipboard()
'Set up objects
Dim olApp As Outlook.Application
Set olApp = CreateObject("Outlook.Application")

Dim olItem As Outlook.ContactItem
Set olItem = olApp.ActiveInspector.CurrentItem

Dim Response
Dim strToCopy As String
Dim strCompany As String

Set Clipboard = New DataObject

'Check to ensure Outlook item is selected
If TypeName(olApp.ActiveInspector) = "Nothing" Then
MsgBox "Contact not open. Exiting", vbOKOnly + vbInformation
Exit Sub
End If

If olItem.CompanyName = "" Then
strCompany = ""
Else
strCompany = olItem.CompanyName & vbCrLf
End If

If olItem.BusinessAddress <> "" Then
strToCopy = olItem.FullName & vbCrLf & strCompany &
olItem.BusinessAddress
Else
strToCopy = olItem.FullName & vbCrLf & strCompany &
olItem.HomeAddress
End If

Clipboard.SetText strToCopy
Clipboard.PutInClipboard

Response = MsgBox("Address Copied to Clipboard!", vbOKOnly)

Set olApp = Nothing
Set olItem = Nothing
Set Clipboard = Nothing
Set Response = Nothing

End Sub
 
S

Sue Mosher [MVP]

PMJI, but my experience has been that this is problem can arise if some
other program is keeping Outlook from shutting down completely. This could
include fax software, PDA sync utilities, etc. You can check Task Manager to
make sure Outlook.exe is no longer running before restarting Outlook.
 
D

David Kaplan

Thanks for the thought, but this only occurs when I run the code. If I
start/stop Outlook without having run the code there is no problem.

I did check, however, and Outlook.exe is not running when I restart
after the problem...

Thanks...

DHK
 
K

Ken Slovak - [MVP - Outlook]

Sub CopyToClipboard()
Dim olItem As Outlook.ContactItem
Set olItem = Application.ActiveInspector.CurrentItem

I've never used the Clipboard from VBA code, just from VB code. So I'm
not sure if the problem is in your call to the DataObject. See if you
still have the problem if you don't pop the text into the clipboard
but just into a string variable. See if your code still hangs Outlook
then. If it doesn't you will have to use the Win32 API calls to work
with the clipboard.
 

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