W
wpiet
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
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