Referencing custom form controls within VBA-- can it be done?

S

sdfhgsdhf

Hi all, I'd be most grateful if you could help on this one. I am trying to
programmatically access Control objects within a custom form from an Outlook
2000 VBA application. However, the help files for VBA list the 'Pages'
collection object but not the 'Page' object-- which leads me to believe I
have to use VBS for this. However, using VBS is of no use here as the
results of the call need to go back to the VBA application.

The routine I am working on at the moment is below. It attempts to get the
Pages collection using ModifiedFormPages, then attempts to get a page by
supplying a page name as an index to this collection. This should be simple,
but I suspect some functionality has been removed from Outlook VBA to stop
me doing this.

Thanks in advance!

----------------------------
Public Sub readFormItem(ByVal itemID As Integer)
' Reads data about an item on the folder form
Dim myFolder As Outlook.MAPIFolder
Dim myItem As Outlook.ContactItem
Dim myMod As Outlook.Pages
Dim myPage As Page
Dim a As Integer

Set myFolder = getAdvisersFolder
Set myItem = myFolder.Items.GetFirst
MsgBox myItem.FullName ' <-- this reports OK
Set myMod = myItem.GetInspector.ModifiedFormPages ' <-- this executes
OK
MsgBox "Got modified form pages"
Set myPage = myMod("Member advisers") ' <-- *** fails here ****
MsgBox "Got page"

For a = 0 To myForm.Controls.count
MsgBox myForm.Caption
Next
End Sub
 
K

Ken Slovak - [MVP - Outlook]

The Page object that is returned for a form page is an object in the MSForms
2.0 library, which is what provides the controls (textboxes, listboxes,
checkboxes, etc.) that are used on Outlook forms.

In general it's best to use Object for anything related to forms controls or
pages, it seems to work much better and more consistently than using early
bound declarations. In addition, only form pages that have been modified are
contained in the ModifiedFormPages collection, so you have to be careful
about that.

Your code uses MyForm without declaring it and uses myForm.Caption
incorrectly. Also, the Controls collection is 1 based and not 0 based.
Outlook collections are 1 based.

Dim oControls As Object
Dim oControl As Object
Dim oPage As Object

'other previous code
Set oPage = myItem.GetInspector.ModifiedFormPages(("Member advisers")
Set oControls = oPage.Controls
For Each oControl In oControls
Debug.Print oControl.Name 'there is no Caption property for a Control
object
'only some types of controls such as CheckBox have a Caption property
Next

The best thing you can do is add MSForms to your project references and then
use the Object Browser to see what is available in that library and what
each control type has in the way of usable properties.
 

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