Can't programmatically access a custom Outlook form in C# (late binding problem)

S

Scott English

I am creating an Outlook Add-in in C#. I want to programmatically manipulate an Outlook form. I am using the PIAs for Office XP.

The ModifiedFormPages property of an Inspector returns a Pages collection. The Add method and Items property return an object of type Object. All the example code I find (which is all for VB or VBScript) says that this object has a Controls property on it that can be used to access the controls on the page. If I use VB.NET, I can access the Controls property (using VB's late binding) and get a Forms.Controls collection back. The "page" object, however, is not a Forms.Page object. In fact, I can't figure out the object's type. The GetType().FullName reports "__ComObject".

C#, however, doesn't let me call the Controls property since it doesn't allow the late-binding that VB.NET does.

The following event is wired up to the NewInspector event of the ActiveInspector
private void Inspectors_NewInspector(Outlook.Inspector inspector)

{

Outlook.Pages pages = (Outlook.Pages)inspector.ModifiedFormPages;

object page = pages.Add("Test");

Type pageType = page.GetType();

Debug.WriteLine(pageType.FullName); // Prints "__ComObject"

Forms.Controls controls = (Forms.Controls)page.Controls; // Won't compile. How do I make this call?

}



The following VB.NET code works.

Private Sub Inspectors_NewInspector(ByVal Inspector As Outlook.Inspector) Handles Inspectors.NewInspector

Dim page As Object = Inspector.ModifiedFormPages.Add("Test")

If TypeOf page Is Forms.Page Then Debug.WriteLine("Page!") ' Nothing Prints

If TypeOf page.Controls Is Forms.Controls Then Debug.WriteLine("Controls!") ' Prints "Controls!" No problem calling the Controls method using VB's late binding.

Dim TextBox1 As Forms.TextBox = page.Controls.Add("Forms.TextBox.1", "TextBox1", True)

Inspector.ShowFormPage("Test")

End Sub
 
S

Sue Mosher [MVP-Outlook]

Maybe a dumb question, but what are you trying to do in the first place?
Modifying form layouts is a bad idea unless you're planning to republish the
form.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



I am creating an Outlook Add-in in C#. I want to programmatically
manipulate an Outlook form. I am using the PIAs for Office XP.

The ModifiedFormPages property of an Inspector returns a Pages collection.
The Add method and Items property return an object of type Object. All the
example code I find (which is all for VB or VBScript) says that this object
has a Controls property on it that can be used to access the controls on the
page. If I use VB.NET, I can access the Controls property (using VB's late
binding) and get a Forms.Controls collection back. The "page" object,
however, is not a Forms.Page object. In fact, I can't figure out the
object's type. The GetType().FullName reports "__ComObject".

C#, however, doesn't let me call the Controls property since it doesn't
allow the late-binding that VB.NET does.

The following event is wired up to the NewInspector event of the
ActiveInspector
private void Inspectors_NewInspector(Outlook.Inspector inspector)

{

Outlook.Pages pages = (Outlook.Pages)inspector.ModifiedFormPages;

object page = pages.Add("Test");

Type pageType = page.GetType();

Debug.WriteLine(pageType.FullName); // Prints "__ComObject"

Forms.Controls controls = (Forms.Controls)page.Controls; // Won't compile.
How do I make this call?

}



The following VB.NET code works.

Private Sub Inspectors_NewInspector(ByVal Inspector As Outlook.Inspector)
Handles Inspectors.NewInspector

Dim page As Object = Inspector.ModifiedFormPages.Add("Test")

If TypeOf page Is Forms.Page Then Debug.WriteLine("Page!") ' Nothing
Prints

If TypeOf page.Controls Is Forms.Controls Then
Debug.WriteLine("Controls!") ' Prints "Controls!" No problem calling the
Controls method using VB's late binding.

Dim TextBox1 As Forms.TextBox = page.Controls.Add("Forms.TextBox.1",
"TextBox1", True)

Inspector.ShowFormPage("Test")

End Sub
 
S

Scott English

Well, if I want to monipulate controls on the form (even if I'm not adding
or removing them), I need to have a reference to the control in order to
manipulate its properties (such as changing a dropdowns list or disabling a
control).
 
S

Sue Mosher [MVP-Outlook]

I've been struggling with this in VB.Net since I don't speak C# and running
into similar issues. Only with late binding am I able to get form control
manipulation code to work. Otherwise, page is a System.__COMObject and
various problems occur. (I also had a detour because I had a reference to
the wrong MSForms -- see
http://support.microsoft.com/default.aspx?scid=kb;en-us;824009) I'm asking
around and will let you know if I find any more information.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
B

Branimir Giurov

Hi Sue,

I'm currently working on a project that had to integrate 2 different
versions of Office (XP and 2003) -
we had no problems working separately - on a single machine using PIAs for
the version of Office installed on it and using early binding.
However when we started to test, we understood that it won't work like this.

The problem came from the fact that some of the event handlers in Office
2003 (specifically Word 2003) have new signatures - new delegate types. So
we had to use Reflection, in order to check which is the underlying version
of Office, and then based on that to generate a new instance of the delegate
and the event, and dynamically to attach them at run-time.

Hope that this will help,
Branimir
 
S

Sue Mosher [MVP-Outlook]

Paul Reynolds wrote an interesting article a while back on using Reflection
to work with different Outlook versions and load the needed PIAs based on
the Outlook version --
http://www.ftponline.com/vsm/2003_12/magazine/features/reynolds/.

The forms controls issue where this thread started is separate, though, I
think. I know I'm not the only one having problems getting them to work --
even in VB.Net -- with anything but an As Object declaration.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
K

kupanb

This is the right format for this call in C#:

MSForms.UserForm page = (MSForms.UserForm)pages[strPageName];
 

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

Similar Threads


Top