如何以编程方式启用“选项...” MS Word 打印设置对话框中的按钮

发布于 2024-08-14 03:07:48 字数 635 浏览 1 评论 0原文

我正在尝试在 Microsoft Word 2003 的 VSTO 插件中显示 MS Word“打印设置”对话框。我可以显示该对话框,但对话框左下角的选项按钮始终处于禁用状态,如下所示捕获。

替代文本

到目前为止我所做的相关代码是:

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
}

谁能告诉我我必须做什么才能启用“选项”按钮?我知道这是可以完成的,因为我们正在替换用于在 VBA 中执行此操作的模板,并且该按钮已在那里启用...

问候, 本

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愿得七秒忆 2024-08-21 03:07:48

对话框的属性只能通过后期绑定获得,并且由于您使用的是 C#,因此您需要使用 InvokeMember 来获取和设置与您正在使用的对话框相关的值。

WdWordDialog Enumeration 的文档中,您知道对于 WdWordDialog.wdDialogFilePrintSetup 对话框的 Options 属性可用。该链接适用于 Office 2007,但对于当前的情况来说,它应该足够了。

有了这些知识,您可以执行以下操作来设置对话框属性值:

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);

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);
多孤肩上扛 2024-08-21 03:07:48

我现在从同事那里得到了一个有效的解决方案。

虽然它不能解决从任何 VSTO C# 代码启动此对话框的更一般情况,但它确实可以通过单击工具栏按钮正确启动此对话框(这就是我们正在尝试做的事情)。所以这为我们解决了问题。

我现在实际上认为这是 MS Word 的一个错误(功能?),并且没有任何通用方法可以从代码中显示此对话框并启用“选项...”按钮。我认为只有当 MS Word 自动调用对话框时它才能工作,因为它作为内置控件连接到 CommandBar。我在 VBA 以及 VSTO 中看到了相同的行为,这往往支持这是一个字限制/错误的理论。

因此,我们以前有这样的代码:

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...
}

当修改为通过将 Controls.Add() 的第二个参数 (Id) 从 Type.Missing 更改为 511(文件打印设置对话框的 ID)来调用内置控件时,如下所示“选项...”按钮如人们所期望的那样启用:

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...
}

希望这可以帮助遇到此问题的其他人。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文