MSDN Library

Customizing Menus and Toolbars in InfoPath 2003

Summary: You can customize the menus and toolbars in Microsoft Office InfoPath 2003 to hide specific controls from users when they are filling out a form. To disable a control, you use the numerical identifier (ID) associated with that control. You don't need to write code to determine the IDs, because this article provides a list of IDs for every control, menu, and toolbar available in the InfoPath user interface. Using these IDs, learn how to customize the toolbars in InfoPath by using Microsoft JScript and C# code. (20 printed pages)

Mike Talley, Microsoft Corporation

November 2004

Applies to: Microsoft Office InfoPath 2003, Microsoft Office InfoPath 2003 SP1

Contents

Introduction to Menus and Toolbars in InfoPath

Microsoft Office InfoPath 2003 provides access to the standard Office toolbar objects. If you are familiar with customizing toolbars in Microsoft Office Word 2003 or Microsoft Office Excel 2003, the code to customize the menus and toolbars in InfoPath should look familiar.

You can write code to enumerate the controls on an InfoPath form to determine the numerical identifier (ID) of the particular control you want to disable. For your reference, however, this article contains tables showing the ID of every control on every built-in menu and toolbar in InfoPath.

The same control may appear in multiple places in the user interface. For example, the Save control appears on both the Standard toolbar and the File menu. This article uses Microsoft JScript and C# code examples to demonstrate how to disable controls of the same kind that appear in multiple places, and how to disable controls that appear only once in the InfoPath user interface.

Working with CommandBars and CommandBarControls

In InfoPath 2003, as in most applications in the Microsoft Office System, menus and toolbars are exposed in the object model as CommandBar objects. You can work with CommandBars either individually or by using the CommandBars collection. Each CommandBar object contains a number of CommandBarControls, which expose actions the user can perform in the application, such as saving a file. The remainder of this article refers to CommandBarControls simply as controls.

Typically, you disable particular controls in the CommandBars collection to restrict what your users can and cannot do with your form. However, you can customize any menu or toolbar in the InfoPath user interface. You can disable particular menu items, such as the Print command, and you can disable entire menus, such as the Edit menu, and entire toolbars, such as the Standard toolbar. Lastly, you can create your own toolbars in InfoPath that contain only the controls you want your users to use.

The List of InfoPath CommandBars, Controls, and Control IDs lists the IDs of all the built-in controls on each CommandBar in InfoPath. Note that this list shows every available control, including those controls available only in design mode in InfoPath.

Customizing Menus and Toolbars with JScript

The following sections describe how to use JScript to disable single controls, menus, and toolbars, and how to add a custom toolbar.

Disabling Controls

For an InfoPath form based on JScript, you must first set a reference to the CommandBars object.

var objCommandBars  = Application.ActiveWindow.CommandBars;
NoteNote:

Use the CommandBars object that is available from the ActiveWindow object of the Application object, instead of the CommandBars object that is available from the Window object of the View object. If the form has not finished loading, the View object may not be available.

For controls that appear on more than one CommandBar, such as the Save control, use the FindControls method of the CommandBars collection, along with the ID of the control you want to disable. Then iterate through the returned collection to disable all instances of the control, as follows.

var myControls = objCommandBars.FindControls(1,3);
for( var enumtor = new Enumerator(myControls) ; !enumtor.atEnd();
enumtor.moveNext() )
{
   enumtor.item().Enabled = false;
}

This example uses the Enumerator object to loop through the collection of Save controls, which have an ID of 3, returned from the FindControls method. The item property accesses each individual Save control to disable it. The first argument for the FindControls method specifies the type of control for which you are searching. For a control such as a button, you use a value of 1, which corresponds to an msoControlButton.

Using the FindControls method ensures that every control of the same type is found and disabled, even if that control appears once in the InfoPath user interface (UI). However, if you know a control appears in only one place, a simpler way to disable that control is to use the FindControl method of the CommandBars class.

You disable a single control by specifying its ID and setting its Enabled property to false. The FindControl method of the CommandBars object finds the control by its ID.

objCommandBars.FindControl(1,748).Enabled = false;

This example disables the Save As control, which has an ID of 748.

Disabling Entire Menus and Toolbars

To disable an entire menu, such as the Edit menu, you must specify a different control type than that used for a control. Instead of 1, you use a control type of 10, which corresponds to the msoControlPopup enumerated value. This type is represented by the first argument in the FindControl method of the CommandBars class. To disable the Edit menu, which has an ID of 30003, the code looks like this.

objCommandBars.FindControl(10,30003).Enabled = false;

As you can see, the code for disabling an entire menu is very similar to that for disabling a control on a menu or toolbar.

To disable an entire toolbar, such as the Menu Bar, Standard, or Formatting toolbar, you specify the name of the toolbar in the code and set its Enabled property to false, as follows.

objCommandBars("Menu Bar").Enabled = false;
objCommandBars("Standard").Enabled = false;
objCommandBars("Formatting").Enabled = false;

When working with toolbars, specify them by name. Even if your form is deployed in other languages, the toolbars you disable by name will be disabled in the other languages.

Note that when you disable an entire toolbar, it will not appear in the InfoPath UI.

Adding a Custom Toolbar

You have learned how to disable particular controls on a toolbar or an entire toolbar, but what if you want to create a new toolbar? In InfoPath, you can create custom toolbars and add controls to those toolbars, but, unlike other Office applications, you cannot hook custom functions to the click event of those controls. InfoPath provides another mechanism for creating custom toolbars with custom events; namely, by modifying the form definition (.xsf) file. For more information, see Display a Custom Toolbar in the InfoPath Software Development Kit (SDK).

Although you cannot hook custom functions to the click event of a control, you may find it useful to group built-in controls on your custom toolbar. You do that by using the Copy method of the CommandBarControl object, as follows.

var objCommandBars = Application.ActiveWindow.CommandBars;
objCustomCommandBar = objCommandBars.Add("My Custom Toolbar");
objCustomCommandBarControl = 
objCommandBars.FindControl(1,3).Copy(objCustomCommandBar);
objCustomCommandBarControl2 = 
objCommandBars.FindControl(1,748).Copy(objCustomCommandBar);
objCustomCommandBarControl.Caption = "Save this form as something else";
objCustomCommandBarControl2.BeginGroup = true;
objCustomCommandBar.Visible = true;

When you use the Copy method, the Save and Save As controls appear on a new custom toolbar called "My Custom Toolbar." This example also renames the Save As control, because by default the Save As control displays as text and not as an icon. This code inserts a vertical line between the two controls by using the BeginGroup property of the second control. Finally, the code shows the custom toolbar in the InfoPath UI by setting its Visible property to true.

Customizing Menus and Toolbars in Managed Code Forms

For managed code forms based on C#, the syntax by which you invoke the CommandBars class is different from that used in JScript.

Because the CommandBars COM class implements only the IDispatch interface, it is necessary to use the ObjectWrapper class, and specifically the InvokeByName method of the ObjectWrapper class, to get access to the FindControl method of the CommandBars class.

Using the ObjectWrapper class also requires you to use the namespace System.Reflection in the Microsoft .NET Framework, because the InvokeByName method is similar to (and based upon) the InvokeMember method of System.Reflection. Both of these methods require you to use the BindingFlags enumeration. You must include System.Reflection in your form code file as a using statement. You also need the Microsoft.Office.Core namespace to properly cast the interfaces defined in the Microsoft Office 11.0 Object Library.

After you add these using statement clauses, the top of your form code file looks like this.

using System;
using Microsoft.Office.Interop.InfoPath.SemiTrust;
using System.Reflection;
using Microsoft.Office.Core;

Furthermore, you must create a reference to the Microsoft Office 11.0 Object Library as part of your InfoPath solution built in Microsoft Visual Studio .NET.

Disabling Controls in Managed Code Forms

Using the InvokeByName method of the ObjectWrapper class to gain access to the CommandBars class requires a few more lines of code than the JScript equivalent. The code to disable a control in a managed code form is as follows.

To disable controls that appear in multiple places, such as the Save control, you use the FindControls method, then iterate over each CommandBarControl in the CommandBarControls collection that is returned, as follows.

object[] args = new object[] {1,3};
CommandBarControls commandBarsFindControls = (CommandBarControls)commandBars.InvokeByName(
"FindControls",
MethodFlags,
args,
null);

foreach(CommandBarControl Item in commandBarsFindControls)
{
Item.Enabled = false;
}

This example disables both instances of the Save control. The Item property is a property of the CommandBarControls class and is of type CommandBarControl.You must specify this propertyin the foreach statement.

If you know a control appears only once in the InfoPath UI, you can use the FindControl method, as follows.

BindingFlags MethodFlags = BindingFlags.InvokeMethod | BindingFlags.DeclaredOnly | BindingFlags.Public | 
BindingFlags.Instance;

ObjectWrapper commandBars = (ObjectWrapper)thisApplication.ActiveWindow.CommandBars;

object[] args2 =  new object[] {1,748};

CommandBarControl commandBarsFindControl = (CommandBarControl)commandBars.InvokeByName(
"FindControl",    // property
MethodFlags,
args2,   // arguments
null);     // Culture

commandBarsFindControl.Enabled = false;

This example disables the Save As control.

Disabling Entire Menus and Toolbars in Managed Code Forms

To disable an entire menu, such as the Edit menu, you must specify a different control type, as shown previously in the JScript example. The C# syntax is as follows.

BindingFlags MethodFlags = BindingFlags.InvokeMethod | 
BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance;

object[] args4 = new object[] {10,30003};
         
CommandBarControl commandBarMenu = (CommandBarControl)commandBars.InvokeByName(
"FindControl",
MethodFlags,
args4,
null);

commandBarMenu.Enabled = false;

This example uses the same BindingFlags as in the previous C# examples, because it invokes the FindControl method.

To disable an entire toolbar, you use the Item property of the CommandBar class, but instead of passing the index of the Item property, you pass the name of the toolbar, as follows.

BindingFlags PropFlags = BindingFlags.GetProperty | 
BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance;

object[] args3 = new object[] {"Menu Bar"};
CommandBar commandBarItem = (CommandBar)commandBars.InvokeByName(
"Item",
PropFlags,
args3,
null);
         
commandBarItem.Enabled = false;

In this example, the BindingFlags statement tells the InvokeByName method that you are going to access a property. This is different from the other C# code examples, in which a method was invoked.

Adding Custom Toolbars in Managed Code Forms

As described in the JScript section, you can create custom toolbars in InfoPath, but you cannot hook custom functions to the click event of a control on your custom toolbar. For more information about how to modify the form definition (.xsf) file to create custom functions, see Display a Custom Toolbar in the InfoPath SDK.

Although you cannot hook custom functions to your toolbar, you may still find it useful to group built-in controls on your custom toolbar. In managed code forms, you can do this by using the Copy method of the CommandBarControl object, as follows.

BindingFlags MethodFlags = BindingFlags.InvokeMethod | BindingFlags.DeclaredOnly | BindingFlags.Public | 
BindingFlags.Instance;

ObjectWrapper commandBars = (ObjectWrapper)thisApplication.ActiveWindow.CommandBars;

object[] args =  new object[] {1,3};
object[] args2 = new object[] {1,748};
object[] args3 = new object[] {"My Custom Toolbar"};

CommandBar AddNewToolbar = (CommandBar)commandBars.InvokeByName(
"Add",
MethodFlags,
args3,
null);

CommandBarControl commandBarsFindControl = (CommandBarControl)commandBars.InvokeByName(
"FindControl",
MethodFlags,
args,
null);

CommandBarControl commandBarsFindControl2 = (CommandBarControl)commandBars.InvokeByName(
"FindControl",
MethodFlags,
args2,
null);

commandBarsFindControl.Copy(AddNewToolbar, 1);
commandBarsFindControl2.Copy(AddNewToolbar, 2);
commandBarsFindControl2.Caption = "Save this form as something else";
commandBarsFindControl2.BeginGroup = true;
AddNewToolbar.Visible = true;

This example uses the Copy method to display the Save and Save As… controls on a new custom toolbar called "My Custom Toolbar." This example also renames the Save As… control, because by default the Save As… control displays as text and not as an icon. The code inserts a vertical line between the two controls by using the BeginGroup property of the second control. Finally, the code shows the custom toolbar in the InfoPath UI by setting its Visible property to true.

Re-enabling Controls

When InfoPath is closed, the state of the toolbars is saved. If you do not re-enable the controls, they will remain disabled in the InfoPath UI, including in design mode. You can re-enable these controls when any built-in events occur, such as the OnSubmitRequest event, or through any custom business logic process that represents an appropriate condition for the disabled controls to be re-enabled. Note, however, that you cannot re-enable controls on the _Shutdown event in managed code forms.

The process of re-enabling controls is straightforward. You simply repeat the code for disabling the control, but set the Enabled property to true for each menu, toolbar, or control you previously disabled.

Additional Considerations

There are a few more issues to consider when working with menus and toolbars in InfoPath.

First, the form you deploy must be fully trusted, because the security level on the CommandBars class is Level 3.

Secondly, you need to ensure that you leave the toolbars in InfoPath in the state in which your user found them before opening your form. You do not want to disable the controls in other forms inadvertently by not re-enabling the controls you disabled in your form.

Lastly, when you use the control ID instead of the name to disable a control, you can be sure it remains disabled in your form if you deploy the form in more than one language. However, if you prefer to use the actual name of a control, you can do so by enclosing the name in quotation marks. If you do this, remember that you do not need to include the ampersand (&) accelerator character, but it is necessary to include the ellipsis (...) on those controls that use it as part of the name.

List of InfoPath CommandBars, Controls, and Control IDs

The following list shows each built-in CommandBar in the InfoPath UI:

  • Menu Bar

  • Standard

  • Formatting

  • Ink

  • Envelope

  • Online Meeting

  • Print Preview

  • System

  • Tables

  • Task Pane

The following tables show all controls for each built-in CommandBar, along with the control ID. Whether you find a control on the Menu Bar or on one of the various toolbars, such as Standard or Formatting, its ID is the same.

NoteNote:

Some controls are available only in the InfoPath design-mode UI. Controls marked with an asterisk (*) are available only in design mode.

Table 1. Menu Bar controls and IDs
Menu Bar ID

&File

Fi&ll Out a Form…

Desi&gn a Form…

P&review Form*

&Default...*

With Data &File...*

With User &Role...*

&Open in Design Mode...

&Open...

Open &from SharePoint Site...

I&mport Form...

&Close

&Save

Save &As...

P&ublish...*

Extrac&t Form Files...*

&Merge Forms...

&Export To

&Web...

Microsoft Office &Excel...

Page Set&up...

Pag&e Setup...*

Print Pre&view

&Print...

Sen&d Form as Attachment

Propert&ies...*

Sen&d to Mail Recipient

Su&bmit

&1 (This MRU does not exist)

&2 (This MRU does not exist)

&3 (This MRU does not exist)

&4 (This MRU does not exist)

&5 (This MRU does not exist)

&6 (This MRU does not exist)

&7 (This MRU does not exist)

&8 (This MRU does not exist)

&9 (This MRU does not exist)

E&xit

&Edit

&Undo

&Redo

Cu&t

&Copy

Copy &Ink as Text

&Paste

Select A&ll

&Find...

R&eplace...

Cha&nge Binding...*

C&hange To*

&Convert 1*

&Convert 2*

&Convert 3*

&Convert 4*

&Convert 5*

&Convert 6*

&Convert 7*

&Convert 8*

&Convert 9*

&Convert 10*

M&ore...*

&View

&Stop Ink Entry

&Start Ink Entry

Tas&k Pane

&Toolbars

Desig&n Tasks...*

Data &Source...*

Manage &Views...*

Word &Print Views...*

Sample &Data*

&Header and Footer*

Page &Width Guide*

&Insert

&Table...

Layout &Table...

&Horizontal Line

Page &Break*

Sy&mbol...

&Picture

&Clip Art...

&From File...

Co&ntrol...

Ink &Drawing and Writing*

Hyperl&ink...*

Text &Box*

Drop-Dow&n List Box*

Repeatin&g Table...*

More &Controls...*

F&ormat

&Font...

Paragrap&h...

Appl&y font to all XXXX*

Apply font to all Control&s*

Bullets and &Numbering...

&Borders and Shading...

&Layout...*

Bac&kground Color...*

&Color Schemes...*

&Asian Typography...

&Grid...*

&Data Formatting...*

C&onditional Formatting...*

Data &Validation...*

View &Properties...*

Control P&roperties...*

&Object...

&Tools

&Spelling...

Set &Language...

Con&vert Ink to Text

&Go to Next Error

Show &Error Message...

&Digital Signatures...

Defaul&t Values...*

S&ubmitting Forms...*

&Data Connections...*

&Resource Manager...*

Scri&pt*

On &Load Event...*

On &Switch Views Event...*

On &Context Change Event...*

&On Sign Event...*

Microsoft Script &Editor*

Us&er Roles...*

Con&vert Main Data Source...*

Design This For&m...

&Workflow...*

&Add Service Pack Features to Form...*

&Customize...

&Options...

T&able

Dra&w Table

&Insert*

&Table...*

Layout &Table...*

Repeatin&g Table...*

Columns to the &Left*

Columns to the &Right*

Rows &Above*

Rows &Below*

In&sert

&Table...

Layout &Table...

Repeatin&g Table...

Columns to the &Left

Columns to the &Right

Rows &Above

Rows &Below

&Delete

&Table

&Columns

&Rows

Sele&ct

&Table

&Column

&Row

C&ell

&Merge Cells

S&plit Cells...

&Borders and Shading...

Grid&lines*

Repeating &Table Properties...*

Table P&roperties...

&Help

Microsoft Office InfoPath &Help

Microsoft Office InfoPath &Tour

&Microsoft Office Online

Microsoft Office &InfoPath Developer Resources

&Contact Us

Chec&k for Updates

Detect and &Repair...

Acti&vate Product...

Customer &Feedback Options...

&About Microsoft Office InfoPath

30002

9111

9112

31264

7275

7279

5070

7717

23

7274

33774

106

3

748

9676

7273

9685

31277

7721

7720

247

5072

109

4

5042

5045

9686

7276

7643

7644

7645

7646

7647

7648

7649

7650

7651

752

30003

128

129

21

19

5088

22

756

141

313

7723

31260

7314

7315

7316

7317

7318

7319

7320

7321

7322

7323

7722

30004

33772

33773

5746

30045

9677

7656

7394

5047

7724

762

5073

30005

3680

7737

3654

509

308

30180

682

2619

7726

9405

1576

7580

7585

7587

7728

30006

9698

779

7730

7731

783

7642

7729

9687

6674

7284

9688

7281

9689

7285

7725

7283

962

30007

2

790

33771

9117

9118

9690

5077

7576

7736

7287

31258

7732

7733

5078

5079

3631

33770

5129

7290

5130

5055

797

522

30008

2059

30444

3680

7737

7587

3685

3687

3681

3683

30462

3680

7737

7587

3685

3687

3681

3683

30445

3695

9691

9692

30446

3698

3700

3699

3701

798

800

7642

300

7395

7654

30010

984

7293

3775

9365

7903

9340

3774

5933

7714

927

Table 2. Standard controls and IDs
Standard ID

Su&bmit

Fi&ll Out a Form...

Desi&gn a Form...

&Open...

&Open in Design Mode...

&Save

Sen&d to Mail Recipient

&Preview Form

&Close Preview

&Print

Print Pre&view

&Spelling...

Cu&t

&Copy

&Paste

&Format Painter

&Undo

&Redo

Hyperl&ink...

&Insert Table...

&Insert Table

&Insert Table

&Browse by Table

I&nsert Picture

Desig&n Tasks...*

Design This For&m...

&Digital Signatures...

Microsoft Office InfoPath &Help

33769

9114

9115

23

7717

3

9686

7739

9116

2521

109

2

21

19

22

108

128

129

1576

3392

4868

333

1550

9699

9677

7290

9690

984

Table 3. Formatting controls and IDs
Formatting ID

&Font...

&Font:

&Font Size

&Font Size:

&Bold

&Italic

&Underline

Align &Left

&Center

Align &Right

&Justify

Line Spacing

1.0

1.5

2.0

&Left-to-Right

&Right-to-Left

&Left-to-Right Document

&Right-to-Left Document

&Numbering

1, 2, 3

I, II, III

i, ii, iii

A, B, C

a, b, c

No List

&Bullets

Solid Circle

Empty Circle

Solid Square

No List

&Decrease Indent

&Increase Indent

&Highlight (Yellow)

&Color Scheme

&Standard Colors

&More Colors...

F&ont Color (Red)

&Color Scheme

&Color

&More Colors...

9698

1728

255

1731

113

114

115

120

122

121

123

5734

54

55

56

1846

1847

1924

1925

11

7294

7295

7296

7297

7298

7299

12

7300

7301

7302

7578

14

15

340

1927

3240

2857

3076

1929

1928

1005

Table 4. Ink controls and IDs
Ink ID

Start Ink Entry

Stop Ink Entry

Pen

Ballpoint Pens

Black

Blue

Red

Felt Tip Pens

Black

Blue

Red

Highlighters

Yellow

Turquoise

Pink

&Pen

Eraser

Ink Color

&More Line Colors

Ink Style

&Line Width

Ink &Drawing and Writing

34773

34772

7880

6992

9062

9063

9064

6898

9065

9066

9067

6890

9068

9069

9070

9407

9408

1692

5094

692

1563

9405

Table 5. Envelope controls and IDs
Envelope ID

&Envelope

3746

Table 6. Online Meeting controls and IDs
Online Meeting ID

&Participant List

&Call Participant

&Remove Participants

&Allow Others to Edit

&Display Chat Window

Display &Whiteboard

&End Meeting

3729

3732

3734

3733

3735

3736

3737

Table 7. Print Preview controls and IDs
Print Preview ID

&Print

&First Page

P&revious Page

Page:

Page Num&ber

&Next Page

&Last Page

&Zoom Out

&Zoom In

&Zoom

&Close

2521

9700

9703

673

9705

9702

9701

445

444

6624

9704

Table 8. System controls and IDs
System ID

&Restore

&Move

&Size

Mi&nimize

Ma&ximize

&Close

839

841

842

838

843

840

Table 9. Tables controls and IDs
Tables ID

Dra&w Table

&Eraser

&Border Style

&Border Width

Border Color (Black)

&Color Scheme

&Standard Colors

&More Line Colors...

Shading Color (None)

No Fill

&Color Scheme

&Standard Colors

&More Fill Colors...

&Insert

&Table...

Layout &Table...

Repeatin&g Table...

Columns to the &Left

Columns to the &Right

Rows &Above

Rows &Below

In&sert

&Table...

Layout &Table...

Repeatin&g Table...

Columns to the &Left

Columns to the &Right

Rows &Above

Rows &Below

&Merge Cells

S&plit Cells...

&Borders and Shading...

Grid&lines

Table P&roperties...

2059

2060

1724

2622

6051

1460

1461

1052

1691

1453

1455

1456

1051

30444

3680

7737

7587

3685

3687

3681

3683

30462

3680

7737

7587

3685

3687

3681

3683

798

800

7642

300

9675

Table 10. Task Pane controls and IDs
Task Pane ID

Tas&k Pane

5746

Conclusion

This article explained how to customize controls, menus, and toolbars in InfoPath in order to hide specific functionality from users. By using the numerical IDs of the controls in your JScript and C# code, you can create a custom user experience and restrict the commands that your form users can access.

Additional Resources

Show:
© 2016 Microsoft