Visual Studio 2010 中的 VBA 到 C# 转换

发布于 2024-11-17 12:34:19 字数 918 浏览 4 评论 0原文

我正在使用带有 VSTO 的 Visual Studio 2010。目前,我正在努力在输入电子邮件后通过功能区按钮修改正文的内容。

我知道Word编辑器是Outook 2007中的默认编辑器。那么,在检查器窗口(撰写邮件窗口)中使用Word编辑器时,如何获取正文文本以执行搜索和替换操作?

我有一个 VBA 宏代码,它工作得很好。我想将此代码转换为 C Sharp,当在撰写邮件窗口中单击功能区按钮时,它将起作用。

Sub ASAtoHyperlinkCompose()

 Dim uiInspector As Outlook.Inspector

 Dim uiObject  As Object

 Dim uiItem   As Outlook.MailItem

 Dim uiDoc   As Word.Document

 Set uiInspector = Application.ActiveInspector

 Set uiObject = uiInspector.CurrentItem

 If uiObject.MessageClass = "IPM.Note" And _

  uiInspector.IsWordMail = True Then

  Set uiItem = uiInspector.CurrentItem

  Set uiDoc = uiInspector.WordEditor

  With uiDoc.Range.Find

   .Text = "ASA^$^$^#^#^#^#^#"

   While .Execute

   .Parent.Hyperlinks.Add .Parent, _

  "http://stack.com=" & .Parent.Text & "outlook2007"

    .Parent.Collapse wdCollapseEnd

   Wend

  End With

  End If

End Sub

I am using Visual Studio 2010 with VSTO. Presently, I am working on modifying the contents of the body through a ribbon button, after the email has been typed.

I am aware the Word Editor is the default editor in Outook 2007. So how can I get the text of the body to perform search and replace operations when using word editor for the inspector window(compose mail window)?

I have a macro code in VBA, which works perfectly fine. I want to convert this code into C sharp which will work when the ribbon button is clicked in compose mail window.

Sub ASAtoHyperlinkCompose()

 Dim uiInspector As Outlook.Inspector

 Dim uiObject  As Object

 Dim uiItem   As Outlook.MailItem

 Dim uiDoc   As Word.Document

 Set uiInspector = Application.ActiveInspector

 Set uiObject = uiInspector.CurrentItem

 If uiObject.MessageClass = "IPM.Note" And _

  uiInspector.IsWordMail = True Then

  Set uiItem = uiInspector.CurrentItem

  Set uiDoc = uiInspector.WordEditor

  With uiDoc.Range.Find

   .Text = "ASA^$^$^#^#^#^#^#"

   While .Execute

   .Parent.Hyperlinks.Add .Parent, _

  "http://stack.com=" & .Parent.Text & "outlook2007"

    .Parent.Collapse wdCollapseEnd

   Wend

  End With

  End If

End Sub

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

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

发布评论

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

评论(1

顾冷 2024-11-24 12:34:19

我不熟悉 Outlook 的自动化和工具包。这或多或少与转换为 C# 的代码相同。如果不出意外的话,这就是一个开始。没有与 C# 等效的 With 语句(仅供参考)。

private void ASAtoHyperlinkCompose()
   {                                           
       var uiInspector = Application.ActiveInspector;

       var uiObject = uiInspector.CurrentItem;

       if (uiObject.MessageClass.ToString().Equals("IPM.Note") && uiInspector.IsWordMail)
       {
           var uiItem = uiInspector.CurrentItem;

           var uiDoc = uiInspector.WordEditor;

           uiDoc.Range.Find.Text = "ASA^$^$^#^#^#^#^#";

           while (uiDoc.Range.Find.Execute())
           {
               uiDoc.Range.Find.Parent.Hyperlinks.Add(uiDoc.Range.Find.Parent, string.Format(@"http://stack.com={0}outlook2007", uiDoc.Range.Find.Parent.Text));
               uiDoc.Range.Find.Parent.Collapse(wdCollapseEnd);

           }
       }
   }

I'm not familiar with Outlook's automation and toolkit. This is more or less the same code converted to C#. If nothing else it is a start. There is no C# equivalent With statement FYI.

private void ASAtoHyperlinkCompose()
   {                                           
       var uiInspector = Application.ActiveInspector;

       var uiObject = uiInspector.CurrentItem;

       if (uiObject.MessageClass.ToString().Equals("IPM.Note") && uiInspector.IsWordMail)
       {
           var uiItem = uiInspector.CurrentItem;

           var uiDoc = uiInspector.WordEditor;

           uiDoc.Range.Find.Text = "ASA^$^$^#^#^#^#^#";

           while (uiDoc.Range.Find.Execute())
           {
               uiDoc.Range.Find.Parent.Hyperlinks.Add(uiDoc.Range.Find.Parent, string.Format(@"http://stack.com={0}outlook2007", uiDoc.Range.Find.Parent.Text));
               uiDoc.Range.Find.Parent.Collapse(wdCollapseEnd);

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