开发者

How to programmatically enable the "Options..." button in the MS Word Print Setup dialog

I'm trying to display the MS Word "Print Setup" d开发者_如何学运维ialog in a VSTO AddIn for Microsoft Word 2003. I can display the dialog box, but the options button at the bottom left corner of the dialog is always disabled as per the following screen capture.

How to programmatically enable the "Options..." button in the MS Word Print Setup dialog

The relevant code for what I've done so far is:

private void printSetup_Click(CommandBarButton Ctrl, ref bool CancelDefault)
{
    Dialog dialog = App.Dialogs[WdWordDialog.wdDialogFilePrintSetup];
    Object missing = Type.Missing;
    dialog.Show(ref missing); // Note that the param is TimeOut
}

Can anyone tell me what I have to do to enable the Options button? I know it can be done because we are replacing a template that used to do this in VBA and the button is enabled there...

Regards, Ben


The properties of dialog boxes are only available through late-binding and since you are using C# you'll need to use InvokeMember to get and set values related to the dialog you are working with.

From the documentation of the WdWordDialog Enumeration you know that for the WdWordDialog.wdDialogFilePrintSetup dialog an Options attribute is available. The link is for Office 2007, but for the case in hand it should suffice.

With this knowledge you can do something like this to set the dialog attribute value:

object objectDialog = (object)dialog;

object[] args = new object[1];
args[0] = (object) null; // Specify value for Options attribute just as in VBA

objectDialog.GetType().InvokeMember(
    "Options", 
    BindingFlags.SetProperty, 
    null, 
    objectDialog, 
    args);


I now have a solution that works that I got from a colleague.

While it doesn't solve the more general case of launching this dialog from any VSTO C# code, it does work for launching this dialog correctly as a result of clicking a toolbar button (which is what we are trying to do). So this fixes the problem for us.

I'm actually of the opinion now that this is a bug (feature?) of MS Word and that there isn't any general way of displaying this dialog from code and having the "Options..." button enabled. I think it can only work if the dialog is invoked automatically by MS Word due to it being hooked up to the CommandBar as a built-in control. I've seen the same behaviour in VBA as well as through VSTO which tends to support the theory that it's a Word limitation/bug.

So we previously had code like this:

public MyCommandBar()
{
  _myBar = App.CommandBars.Add("My Toolbar", 1, Type.Missing, true);

  // Add a button to call our custom event handler
  _printSetup = (CommandBarButton)
          _myBar.Controls.Add(MsoControlType.msoControlButton, 
          Type.Missing, Type.Missing, 1, true); 
  _printSetup.Click += printSetup_Click(); // Call the handler shown in my original question
  // More stuff...
}

And when modified to call the built-in control by changing the second argument (Id) to Controls.Add() from Type.Missing to 511 (the ID for the File Print Setup dialog) like this the "Options..." button is enabled like one would expect:

public MyCommandBar()
{
  _myBar = App.CommandBars.Add("My Toolbar", 1, Type.Missing, true);

  // Call the built-in File Print Setup dialog automagically
  _printSetup = (CommandBarButton)
          _myBar.Controls.Add(MsoControlType.msoControlButton, 
          511, Type.Missing, 1, true); 
  // More stuff...
}

Hopefully this helps others who run into this problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜