Different behavior when using form from Outlook or (through VB) Access
Author |
Message |
Robert Marsany #1 / 9
|
 Different behavior when using form from Outlook or (through VB) Access
Hi. I have a custom Outlook form that includes a toolbar/button that fires a macro that opens a VBA form (whew!). The form has an Activate event handler that populates a listbox, and an OK and Cancel button with associated Click handlers. All works well when I open an item with the form, and click the toolbar button (or run the associated macro). When I open the item from Access using Outlook's VB object model, I see the form, same as before, and the button. When I click the button, though, the VBA form opens up with the OK, Cancel, listbox, the works, but the listbox is not populated (the Activate event handler hasn't run) and the OK and Cancel buttons don't do anything (the Click handlers don't fire). Which leads me to suspect that something needs to be dimensioned in the Access code using the "WithEvents" keyword. For example, the variable referring to the Outlook item needs to be declared WithEvents, or something. I tried this, naturally, and as you can tell it didn't work. What am I missing? --rbt
|
Sun, 31 Aug 2003 07:23:56 GMT |
|
 |
Ken Slovak - [MVP - Outlook #2 / 9
|
 Different behavior when using form from Outlook or (through VB) Access
Post a snippet of the code you are using to open Outlook from Access and the macro you are using. Maybe someone here can figure it out. -- Ken Slovak [MVP - Outlook] Lead Author, Professional Outlook 2000 Programming, Wrox Press Co-author of "Programming Microsoft Outlook 2000", Chapters 8-13, Appendices, Sams
Quote: > Hi. I have a custom Outlook form that includes a toolbar/button that fires > a macro that opens a VBA form (whew!). The form has an Activate event > handler that populates a listbox, and an OK and Cancel button with > associated Click handlers. > All works well when I open an item with the form, and click the toolbar > button (or run the associated macro). > When I open the item from Access using Outlook's VB object model, I see the > form, same as before, and the button. When I click the button, though, the > VBA form opens up with the OK, Cancel, listbox, the works, but the listbox > is not populated (the Activate event handler hasn't run) and the OK and > Cancel buttons don't do anything (the Click handlers don't fire). > Which leads me to suspect that something needs to be dimensioned in the > Access code using the "WithEvents" keyword. For example, the variable > referring to the Outlook item needs to be declared WithEvents, or something. > I tried this, naturally, and as you can tell it didn't work. > What am I missing? > --rbt
|
Sun, 31 Aug 2003 21:53:32 GMT |
|
 |
Irelan #3 / 9
|
 Different behavior when using form from Outlook or (through VB) Access
Can you post your code? -- For Outlook and VB Experts: http://escapecoms.homestead.com ***************************************************************** Visit our new DOT NET ZONE for all that's hot! <http://www.vbug.co.uk> ****************************************************************
Quote: > Hi. I have a custom Outlook form that includes a toolbar/button that fires > a macro that opens a VBA form (whew!). The form has an Activate event > handler that populates a listbox, and an OK and Cancel button with > associated Click handlers. > All works well when I open an item with the form, and click the toolbar > button (or run the associated macro). > When I open the item from Access using Outlook's VB object model, I see the > form, same as before, and the button. When I click the button, though, the > VBA form opens up with the OK, Cancel, listbox, the works, but the listbox > is not populated (the Activate event handler hasn't run) and the OK and > Cancel buttons don't do anything (the Click handlers don't fire). > Which leads me to suspect that something needs to be dimensioned in the > Access code using the "WithEvents" keyword. For example, the variable > referring to the Outlook item needs to be declared WithEvents, or something. > I tried this, naturally, and as you can tell it didn't work. > What am I missing? > --rbt
|
Sun, 31 Aug 2003 22:15:41 GMT |
|
 |
Robert Marsany #4 / 9
|
 Different behavior when using form from Outlook or (through VB) Access
Here's the relevant code: In Outlook: ---------- Items in this particular folder have a custom Outlook Form associated with them. On the toolbar, there's a button that runs the macro RunSelectGrantCategories(), which looks like this: Public Sub RunSelectGrantCategories() On Error GoTo criticalError Dim dlg As SelectGrantCategoriesDialog Set theItem = Application.ActiveInspector.CurrentItem Set dlg = New SelectGrantCategoriesDialog Load dlg With dlg .GrantCategoryArray = grantCategories() .NumGrantCategories = UBound(grantCategories, 1) .CatString = theItem.Categories .Show End With On Error GoTo noncriticalError If dlg.ChangeFlag Then theItem.Categories = dlg.CatString Unload dlg Exit Sub criticalError: MsgBox "There was an error. Try rerunning the Initialize_Handler macro.", vbExclamation, "Error in Categories" Exit Sub noncriticalError: Exit Sub End Sub This routine loads a VBA Form of the class SelectGrantCategoriesDialog, sets some of the properties associated with the form, and then displays it. The form has event code associated with both itself and its contained controls; for example, the UserForm_Activate event fills a listbox on the form from the GrantCategoriesArray property, and the OK and Cancel buttons set properties appropriately and close the form using their _Click event code. As mentioned, everything runs fine when the Item Inspector is invoked from Outlook and the button that triggers the macro clicked there. However, when the Item is displayed with an inspector from VBA code in Access, the event code associated with the SelectGrantCategoriesDialog isn't triggered: the Userform_Activate code doesn't happen, and the OK and Cancel buttons don't do anything. From Access: ------------- There's a button on an Access form, that, when clicked, does the following: ' get the contact details for a given StubID, if we have one If Not IsNull(Me![StubID]) Then refreshContactDetails ' If there's a current Contact Item, display it If Not (cItem Is Nothing) Then cItem.Display True Else ' there's no current one, so create a new one If contactFolder Is Nothing Then ... The line "cItem.Display True" brings up the contact in it's Outlook Form, complete with custom button for displaying the dialog that's not working right. "cItem" is declared thusly: Dim cItem As ContactItem in the General/Declarations section of the Access Form code. refreshContactDetails() is also in the Form code; it looks like this: Public Sub refreshContactDetails() ' Find the contact in the contacts folder corresponding to the IndividualLink If contactFolder Is Nothing Then Set contactFolder = openFolder(contactFolderName) End If Set cItem = findContact(Me![StubID], contactFolder) ' Update the relevant fields If cItem Is Nothing Then Me![Address] = "" Me![Phone] = "" Else Me![Address] = cItem.BusinessAddress Me![Phone] = cItem.BusinessTelephoneNumber End If End Sub openFolder() is a utility routine in a separate Access module. It looks like this: Public Function openFolder(folderName) On Error GoTo handleErr ' Open Outlook Set outlookApplication = New Outlook.Application ' Open MAPI Folders Set outlookNamespace = outlookApplication.GetNamespace("MAPI") ' Open folder Set parentFolder = FollowPath(outlookNamespace, networkPath) If parentFolder Is Nothing Then Set parentFolder = FollowPath(outlookNamespace, localPath) Set openFolder = parentFolder.Folders(contactFolderName) Exit Function handleErr: LogAnError ("OutlookFunctions:openFolder") End Function where "outlookApplication" is declared as an Object. Finally, findContact(), used to find a specific contact using the field "User1" in the Outlook ContactItem, is another utility routine as follows: Public Function findContact(ID, f) On Error GoTo handleErr ' Set a restriction on the Folder Set c = f.Items.Find("[User1] = '" & ID & "'") Set findContact = c Exit Function handleErr: LogAnError ("OutlookFunctions:findContact") End Function Lots of bits and pieces. Thanks for offering to take a quick gander at it. --rbt
|
Mon, 01 Sep 2003 06:46:38 GMT |
|
 |
Robert Marsany #5 / 9
|
 Different behavior when using form from Outlook or (through VB) Access
More on my mystery: yesterday, I accidentally saved the Outlook macro file (VBAProject.OTM) without digitally signing it, so today when I open Outlook it asks whether or not to enable macros. Interestingly, the following happens when I invoke the Access code to edit an Outlook item that I'm having trouble with: - Start out with Outlook closed, Access open. - I hit the button in Access. It finds the contact in Outlook and opens an Inspector on it. - I hit the toolbar button in the Inspector that runs my custom VBA form - I get the dialog about enabling/disabling macros, since this is the only instance of Outlook running. - The custom dialog form opens, and everything is fine! - I close the dialog, and close the Item Inspector. - I hit the button again in Access. - Inspector appears. I hit the toolbar button. No dialog about enabling/disabling macros this time. - The custom dialog form opens, but events are disabled: the Userform_Activate event didn't run, and the OK and Cancel buttons do nothing. I can close the dialog using the standard close gadget on the window itself. Is it me, or does the fact that it works once seem odd to y'all? --rbt
|
Mon, 01 Sep 2003 06:54:16 GMT |
|
 |
Robert Marsany #6 / 9
|
 Different behavior when using form from Outlook or (through VB) Access
I found a workaround, and I thought you'd be interested. The problem disappears if I change the line in Access that says cItem.Display True to cItem.Display The difference is that the "True" parameter tells Outlook to display the item inspector as a modal dialog. Apparently this hoses the event handling of dialogs invoked from that inspector. For now, I'm happy to open the Outlook inspector as a non-modal window, although I'm not sure of the ramifications for my code. Correct me if I'm wrong, but either this is way too subtle for me to follow, or it's a bug in the way the Outlook object model works. Before I send off a bug report, anyone else have an opinion? --rbt
|
Mon, 01 Sep 2003 08:55:16 GMT |
|
 |
Ken Slovak - [MVP - Outlook #7 / 9
|
 Different behavior when using form from Outlook or (through VB) Access
Unless your code disposes of the Outlook Application object then you would not expect to see another prompt. BTW, there can only be one instance of Outlook running at a time. Even if you call CreateObject 6 times the returned Outlook Application object will be the same instance. My guess is your oddity is due to using modal Inspector windows. Get rid of those and your events should fire. -- Ken Slovak [MVP - Outlook] Lead Author, Professional Outlook 2000 Programming, Wrox Press Co-author of "Programming Microsoft Outlook 2000", Chapters 8-13, Appendices, Sams
Quote: > More on my mystery: yesterday, I accidentally saved the Outlook macro file > (VBAProject.OTM) without digitally signing it, so today when I open Outlook > it asks whether or not to enable macros. Interestingly, the following > happens when I invoke the Access code to edit an Outlook item that I'm > having trouble with: > - Start out with Outlook closed, Access open. > - I hit the button in Access. It finds the contact in Outlook and opens > an Inspector on it. > - I hit the toolbar button in the Inspector that runs my custom VBA form > - I get the dialog about enabling/disabling macros, since this is the > only instance of Outlook running. > - The custom dialog form opens, and everything is fine! > - I close the dialog, and close the Item Inspector. > - I hit the button again in Access. > - Inspector appears. I hit the toolbar button. No dialog about > enabling/disabling macros this time. > - The custom dialog form opens, but events are disabled: the > Userform_Activate event didn't run, and the OK and Cancel buttons do > nothing. I can close the dialog using the standard close gadget on the > window itself. > Is it me, or does the fact that it works once seem odd to y'all? > --rbt
|
Mon, 01 Sep 2003 22:38:55 GMT |
|
 |
Ken Slovak - [MVP - Outlook #8 / 9
|
 Different behavior when using form from Outlook or (through VB) Access
Don't display your Inspector modally, that doesn't work right. -- Ken Slovak [MVP - Outlook] Lead Author, Professional Outlook 2000 Programming, Wrox Press Co-author of "Programming Microsoft Outlook 2000", Chapters 8-13, Appendices, Sams
Quote: > Here's the relevant code: > In Outlook: > ---------- > Items in this particular folder have a custom Outlook Form associated with > them. On the toolbar, there's a button that runs the macro > RunSelectGrantCategories(), which looks like this: > Public Sub RunSelectGrantCategories() > On Error GoTo criticalError > Dim dlg As SelectGrantCategoriesDialog > Set theItem = Application.ActiveInspector.CurrentItem > Set dlg = New SelectGrantCategoriesDialog > Load dlg > With dlg > .GrantCategoryArray = grantCategories() > .NumGrantCategories = UBound(grantCategories, 1) > .CatString = theItem.Categories > .Show > End With > On Error GoTo noncriticalError > If dlg.ChangeFlag Then theItem.Categories = dlg.CatString > Unload dlg > Exit Sub > criticalError: > MsgBox "There was an error. Try rerunning the Initialize_Handler > macro.", vbExclamation, "Error in Categories" > Exit Sub > noncriticalError: > Exit Sub > End Sub > This routine loads a VBA Form of the class
SelectGrantCategoriesDialog, sets Quote: > some of the properties associated with the form, and then displays it. The > form has event code associated with both itself and its contained controls; > for example, the UserForm_Activate event fills a listbox on the form from > the GrantCategoriesArray property, and the OK and Cancel buttons set > properties appropriately and close the form using their _Click event code. > As mentioned, everything runs fine when the Item Inspector is invoked from > Outlook and the button that triggers the macro clicked there. However, when > the Item is displayed with an inspector from VBA code in Access, the event > code associated with the SelectGrantCategoriesDialog isn't triggered: the > Userform_Activate code doesn't happen, and the OK and Cancel buttons don't > do anything. > From Access: > ------------- > There's a button on an Access form, that, when clicked, does the following: > ' get the contact details for a given StubID, if we have one > If Not IsNull(Me![StubID]) Then refreshContactDetails > ' If there's a current Contact Item, display it > If Not (cItem Is Nothing) Then > cItem.Display True > Else ' there's no current one, so create a new one > If contactFolder Is Nothing Then > ... > The line "cItem.Display True" brings up the contact in it's Outlook Form, > complete with custom button for displaying the dialog that's not working > right. > "cItem" is declared thusly: > Dim cItem As ContactItem > in the General/Declarations section of the Access Form code. > refreshContactDetails() is also in the Form code; it looks like this: > Public Sub refreshContactDetails() > ' Find the contact in the contacts folder corresponding to the > IndividualLink > If contactFolder Is Nothing Then > Set contactFolder = openFolder(contactFolderName) > End If > Set cItem = findContact(Me![StubID], contactFolder) > ' Update the relevant fields > If cItem Is Nothing Then > Me![Address] = "" > Me![Phone] = "" > Else > Me![Address] = cItem.BusinessAddress > Me![Phone] = cItem.BusinessTelephoneNumber > End If > End Sub > openFolder() is a utility routine in a separate Access module. It looks > like this: > Public Function openFolder(folderName) > On Error GoTo handleErr > ' Open Outlook > Set outlookApplication = New Outlook.Application > ' Open MAPI Folders > Set outlookNamespace = outlookApplication.GetNamespace("MAPI") > ' Open folder > Set parentFolder = FollowPath(outlookNamespace, networkPath) > If parentFolder Is Nothing Then Set parentFolder = > FollowPath(outlookNamespace, localPath) > Set openFolder = parentFolder.Folders(contactFolderName) > Exit Function > handleErr: > LogAnError ("OutlookFunctions:openFolder") > End Function > where "outlookApplication" is declared as an Object. Finally, > findContact(), used to find a specific contact using the field "User1" in > the Outlook ContactItem, is another utility routine as follows: > Public Function findContact(ID, f) > On Error GoTo handleErr > ' Set a restriction on the Folder > Set c = f.Items.Find("[User1] = '" & ID & "'") > Set findContact = c > Exit Function > handleErr: > LogAnError ("OutlookFunctions:findContact") > End Function > Lots of bits and pieces. Thanks for offering to take a quick gander at it. > --rbt
|
Mon, 01 Sep 2003 22:40:53 GMT |
|
 |
Ken Slovak - [MVP - Outlook #9 / 9
|
 Different behavior when using form from Outlook or (through VB) Access
It's not a bug, that's the way modal forms work. Until you close them nothing else in your program will run. Also, in many versions of Outlook there is a bug in displaying modal forms, they leave a ghost Inspector around that has to be closed manually. That was fixed in Outlook 2000 SR1A. -- Ken Slovak [MVP - Outlook] Lead Author, Professional Outlook 2000 Programming, Wrox Press Co-author of "Programming Microsoft Outlook 2000", Chapters 8-13, Appendices, Sams
Quote: > I found a workaround, and I thought you'd be interested. The problem > disappears if I change the line in Access that says > cItem.Display True > to > cItem.Display > The difference is that the "True" parameter tells Outlook to display the > item inspector as a modal dialog. Apparently this hoses the event handling > of dialogs invoked from that inspector. > For now, I'm happy to open the Outlook inspector as a non-modal window, > although I'm not sure of the ramifications for my code. Correct me if I'm > wrong, but either this is way too subtle for me to follow, or it's a bug in > the way the Outlook object model works. Before I send off a bug report, > anyone else have an opinion? > --rbt
|
Mon, 01 Sep 2003 22:36:06 GMT |
|
|
|