views:

313

answers:

2

I'm trying to display the MS Word "Print Setup" dialog 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.

alt text

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

A: 

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);
João Angelo
Thanks Joao, however I have no idea what the type of the "Options" property is and cannot find any documentation about it. Unfortunately the MSDN page you linked to only lists the attributes available but doesn't say what type they are (string, bool etc) and I can't find any other pages with the info. It appears that the "DoNotSetAsSysDefault" parameter is a bool and "Printer" is a string but I don't know about "Options". I tried setting it to true but it didn't work (no exception thrown, it just didn't change the behaviour). Any other ideas or do you have a longer code snippet for me? Thanks.
Ben Robbins
How does the dialog is shown currently? If it's with VBA can you share the code used to show the dialog?
João Angelo
Thanks Joao, it turns out the dialog is currently shown as a built-in control from the tool bar button in VBA in much the same way as my answer. If you try and display the dialog using VBA (say in an event handler method) you get the same problem of the "Options..." button being disabled. I'm pretty sure that the options button being disabled is just a bug in MS Word, but the work-around is to trigger it as a built-in control via the CommandBarControl.Add() method, which displays the dialog with the "Options..." button enabled. Thanks for your help, Ben.
Ben Robbins
A: 

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.

Ben Robbins