LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 23
Default Reference Excel UDF from Outlook

Is there a way, in Office 2003, to reference an Excel UDF in Outlook VBA (or
other applications)?

I created a function in an Excel workbook & I can use it in VBA in any other
Excel workbook by adding it via Tools/References/Browse . . . in VBE.

I want to use the same function in an Outlook VBA program but, if I try to
add a reference to the Excel workbook in Outlook VBE, it returns message,
"Can't add reference to the specified file."

Alternatively, is there somewhere else to create this function so that it
could be used in any Office application? From research, I believe an Add-in
would be ideal but I don't have Visual Studio & don't know what other options
are available.

I want to have it in a centralized location so that it is accessible to all
my users and can be easily maintained.

I tried someone's suggestion (thanks, JP) of using the Run method for the
function, VldLogin(), in Outlook using the following code:

On Error Resume Next
Set XL = GetObject(, "Excel.Application")
If XL Is Nothing Then
Set XL = CreateObject("Excel.Application")
End If
Set CmnWB = XL.Workbooks("CmnPrc.xls")
If CmnWB Is Nothing Then
XL.Workbooks.Open FileName:= _
"\\server\Path\CmnPrc.xls"
End If

LoginVld = XL.Run("VldLogin()")

If Not LoginVld = True Then
Set XL = Nothing
Exit Sub
End If

Stepping thru the code, when it hits 'LoginVld = XL.Run("VldLogin()")', it
accesses the function & shows the Userform.
The form seems to work correctly. If I omit an entry or enter invalid data,
I get the correct messages.
However, if I enter valid data, it never returns to the Outlook module.
Instead, the UserForm shows again & I seem to be caught in a loop there.

Using the function in Excel VBA, I don't have this problem. It works
perfectly.

Here is the function, along with 3 Public variables, 2 of which I use after
returning from the function:

Public Abt
Public UsrID
Public Psw

Function VldLogin() As Boolean
Abt = vbNo
Load QryLogin
QryLogin.Show
If Abt = vbYes Then
VldLogin = False
Else
VldLogin = True
End If
End Function

The userform has the following code:

Private Sub Cancel_Click()
Login.Abt = MsgBox("Are You Sure You Want To Cancel?", vbYesNo +
vbDefaultButton2)

If Login.Abt = vbYes Then
Unload Me
Else
Me.UsrID.SetFocus
End If
End Sub

Private Sub OK_Click()

Dim LoginAut As Boolean

If Len(Trim(Me.UsrID)) = 0 Then
MsgBox ("You Must Enter A User Name")
Me.UsrID.SetFocus
ElseIf Len(Trim(Me.Psw)) = 0 Then
MsgBox ("You Must Enter A Password")
Me.Psw.SetFocus
Else
LoginAut = AutLogin(Me.UsrID, Me.Psw)

If LoginAut = True Then
Login.UsrID = Me.UsrID.Value
Login.Psw = Me.Psw.Value
Unload Me
Else
MsgBox ("Invalid Username or Password; Please Re-Enter")
Me.UsrID.SetFocus
End If
End If
End Sub

Function AutLogin(ByVal UsrID As String, _
ByVal Psw As String) _
As Boolean

Const ADS_SECURE_AUTHENTICATION = 1
Dim Aut As Object ' Authentication
Dim Dmn As String ' Domain
Dim G_C As Object ' Global Catalog
Dim Root As Object ' RootDSE

On Error Resume Next

Set Root = GetObject("GC://rootDSE")
Dmn = Root.Get("defaultNamingContext")
Set G_C = GetObject("GC:")
Set Aut = G_C.OpenDSObject("GC://" & Dmn, UsrID, Psw,
ADS_SECURE_AUTHENTICATION)

If Aut Is Nothing Then
AutLogin = False
Else
AutLogin = True
End If

Set Aut = Nothing
Set G_C = Nothing
Set Root = Nothing

End Function


--
Will
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reference to Microsoft Outlook VBA Troubled User Excel Programming 0 August 4th 08 09:40 PM
Reference Outlook in VBA Noemi Excel Programming 4 November 25th 07 09:43 PM
How can I reference an Outlook calendar holiday from Excel? Garhart Excel Discussion (Misc queries) 0 January 5th 06 08:38 PM
Outlook Email Reference sbruner Excel Programming 2 June 15th 05 09:59 PM
VBA reference to Outlook Torbjörn Steijer[_2_] Excel Programming 4 January 8th 04 12:45 AM


All times are GMT +1. The time now is 04:07 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"