【问题标题】:Outlook AddIn Zoom.PercentageOutlook 插件缩放百分比
【发布时间】:2015-07-30 04:33:47
【问题描述】:

我正在尝试将此 VBA 代码从 Outlook 插件转换为 C#

Private Sub objInspector_Activate() Handles objInspector.Activate
        Dim wdDoc As Microsoft.Office.Interop.Word.Document = objInspector.WordEditor
        wdDoc.Windows(1).Panes(1).View.Zoom.Percentage = lngZoom
End Sub

但我无法访问 Panes.View.Zoom.Percentage 属性

主要思想是当用户打开一封电子邮件时,他会得到一个自定义的缩放级别。

我现在得到的是:

void Inspector_Activate()          
 {             
// this bool is true
// bool iswordMail = objInspector.IsWordMail();

//I get the word document

Document word = objInspector.WordEditor as Microsoft.Office.Interop.Word.Document;

word.Application.ActiveDocument.ActiveWindow.View.Zoom.Percentage = 150;
// at this point i'm getting an exception
// I've also tried with 
// word.ActiveWindow.ActivePane.View.Zoom.Percentage = 150; getting the same exception                      
}

例外是:

“System.Runtime.InteropServices.COMException”类型的异常 发生在 OutlookAddInTest.dll 中,但未在用户代码中处理

附加信息:此对象模型命令在 电子邮件。

我是 C# 和 Office 插件的新手,有什么建议吗?

【问题讨论】:

    标签: c# vba outlook zooming outlook-addin


    【解决方案1】:

    使用word.Windows.Item(1).View.Zoom.Percentage = 150(其中word来自Inspector.WordEditor

    【讨论】:

    • 不错的尝试,谢谢,但它不起作用。 “Microsoft.Office.Interop.Word.Windows”不包含“项目”的定义
    • 示例代码看起来像是在 VBA 中。在 C# 中,Item 方法被替换为 indexer 属性。尝试改用以下代码:word.Windows[1].View.Zoom.Percentage = 150;
    • 成功了!!这么简单,word.Windows[1].Panes[1].View.Zoom.Percentage = 150;,非常感谢
    【解决方案2】:

    word.Application.ActiveDocument.ActiveWindow.View.Zoom.Percentage = 150;

    究竟是什么属性触发了异常?

    反正代码中不需要调用Application和ActiveDocument属性。 Inspector 类的 WordEditor 属性返回 Document 类的实例(不是 Word 应用程序实例)。

    【讨论】:

    • ActiveWindow 触发异常。我试过 word.ActiveWindow.ActivePane.View.Zoom.Percentage = 150;结果相同。这里msdn.microsoft.com/en-us/library/…我找到了缩放界面,但是我仍然无法获得ActiveWindow。
    • 尝试使用 Word Application 类的 Windows 属性来获取 Window 类的实例。
    【解决方案3】:

    感谢 Eugene Astafiev 的帮助。 方括号起到了作用

    VBA

    Private Sub objInspector_Activate() Handles objInspector.Activate
            Dim wdDoc As Microsoft.Office.Interop.Word.Document = objInspector.WordEditor
            wdDoc.Windows(1).Panes(1).View.Zoom.Percentage = 150
    End Sub
    

    C#

    private void Inspector_Activate()
            {
                Document wdDoc = objInspector.WordEditor;
                wdDoc.Windows[1].Panes[1].View.Zoom.Percentage = 150;  
            }
    

    【讨论】:

      【解决方案4】:

      我一直想要这个,然后我在 MSDN Gallery Outlook 2010: Developing an Inspector Wrapper 偶然发现了一个不错的项目。它有一组用于所有 Outlook 对象的包装器,因此您可以获得每个感兴趣的项目的真实事件。不确定这是否是有史以来最有效的方法,但它似乎有效。

      我的视力有问题,所以想要所有东西都变黑,然后放大所有东西。我似乎可以通过重写 Activate() 方法来做到这一点。这一切都很新,所以我们会看看它是否能长期存在。

          protected virtual void Activate() {
              var activeDocument = Inspector.WordEditor as Document;
              if (activeDocument == null)
                  return;
      
              var mailZoom = GetSetting("MailZoom", 125);
              if (mailZoom != 0)
                  activeDocument.Windows[1].View.Zoom.Percentage = mailZoom;
      
              if (GetSetting("MailBlack", true)) {
                  activeDocument.Background.Fill.ForeColor.RGB = 0;
                  activeDocument.Background.Fill.Visible = msoTrue;
                  activeDocument.Saved = true;
              }
          }
      

      在本例中,GetSetting 只是一个从 INI 文件返回设置的函数。您可以使用常量或其他一些存储方法。

      可能有更好的方法来获得黑底白字,但这似乎相当不错。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-20
        • 1970-01-01
        • 2013-01-03
        相关资源
        最近更新 更多