There are probably several ways to do this, but this seemed pretty straightforward to me. The code loops through the CommandBars collection that is part of Microsoft.Office.Core and sets the Enabled property to false. If you want, you can selectively inspect each toolbar in the collection and enable/disable specific ones.
You can use the Context Changed Event to set a spiffy title for your form. Be aware that, if the Context Changed Event is grayed out, you need to change the form’s browser compatibility options so it is not able to be opened in InfoPath Forms Services.
Uncheck the box “Design a form template that can be opened in a browser or InfoPath.”
Since we are using code, you’ll have to fully trust the form and sign it.
We’re using C# in this example, so set the form template code language to C#.
Add a reference to Microsoft.Office.Core by browsing to MSO.DLL in your Microsoft Shared folder. You can see it here in my list of recent references.
Now you should be able to add the USING statement for Microsoft.Office.Core.
using Microsoft.Office.InfoPath;
using Microsoft.Office.Core; // Required for Commandbars
using System;
using System.Xml;
using System.Xml.XPath;
using System.Windows.Forms;
using mshtml;
namespace Test_Form
{
public partial class FormCode
{
#region EventHandlers
public void InternalStartup()
{
EventManager.FormEvents.Loading += new LoadingEventHandler(FormEvents_Loading);
EventManager.FormEvents.ContextChanged += new ContextChangedEventHandler(FormEvents_ContextChanged);
}
#endregion
#region Toolbars
public void FormEvents_ContextChanged(object sender, ContextChangedEventArgs e)
{
if (this.New)
{
this.Application.ActiveWindow.Caption = “Fill out my form!”;
}
}
public void FormEvents_Loading(object sender, LoadingEventArgs e)
{
HideAllToolbars();
}
void HideAllToolbars()
{
// The main toolbar with the file menu will be hidden (msoBarTypeMenuBar)
// along with all the other toolbars (msoBarTypeNormal)
// if HideToolbarsFlag is true in the datasource
/* List of enumerated menu bars
Menu Bar msoBarTypeMenuBar 1
Standard msoBarTypeNormal 2
Print Preview
Formatting
Tables
Ink
Task Pane
Property Editor
Office Clipboard
XML Source
Research
XML Document
Signatures
Document Actions
Clip Art
Selection and Visibility
Document Management
Document Updates
Mail Merge Panes
Fax Service
Meeting Workspace
Attachment Options
Clipboard
Envelope
System
Online Meeting
* */
XPathNavigator root = MainDataSource.CreateNavigator();
string hideToolbarsFlag = root.SelectSingleNode(“//my:HideToolbarsFlag”, NamespaceManager).ToString();
bool flag;
if (hideToolbarsFlag.ToLower() == “true”)
{
flag = true;
}
else
{
flag = false;
}
CommandBars myCommandBars = this.Application.ActiveWindow.CommandBars as CommandBars;
CommandBar myCommandBar = (this.Application.ActiveWindow.CommandBars as CommandBars).ActiveMenuBar;
foreach (CommandBar item in myCommandBars)
{
//MessageBox.Show(item.Name.ToString() + ” ” + item.Type.ToString() + ” ” + item.Index.ToString());
if (flag == true && (item.Type == MsoBarType.msoBarTypeMenuBar || item.Type == MsoBarType.msoBarTypeNormal))
{
item.Enabled = false;
}
}
}
#endregion
}
}
In order to control whether the toolbars are displayed or not, a field is added to the data source. Add the element, HideToolbarsFlag, that will control whether or not to hide the toolbars.
This bit of code retrieves it’s value. If you set the default value to true, it hides the toolbars; anything else, it doesn’t.
string hideToolbarsFlag = root.SelectSingleNode(“//my:HideToolbarsFlag”, NamespaceManager).ToString();
And now preview your form.
This works nicely in 2007, but not so much in 2010. The Menu Bar and Quick Launch are still visible. Would you have code to resolve this in InfoPath 2010?
Thank you,
Diana
Yes, 2010 update please!!!!