简体   繁体   English

使用 C# 中的字时存在显式转换

[英]An Explicit conversion exists when working with word in C#

This code gets the active inspector window ie the compose mail window and performs the search and replace function for the body of the email.此代码获取活动检查器 window 即撰写邮件 window 并执行搜索并替换 ZC1C425268E68385D1AB5074C17A94EBF14Z 为 Z07483F5ABC2736 的正文。

But I am getting an error:但我收到一个错误:

Cannot implicitly convert type 'object' to 'Microsoft.Office.Interop.Word.Range'.无法将类型“对象”隐式转换为“Microsoft.Office.Interop.Word.Range”。 An explicit conversion exists (are you missing a cast?)存在显式转换(您是否缺少演员表?)

Code here for your reference..代码在这里供您参考..

 private void button1_Click(object sender, RibbonControlEventArgs e)
    {
          Outlook.Inspector uiInspector = Globals.ThisAddIn.Application.ActiveInspector();
          object uiObject = uiInspector.CurrentItem;
          if (uiObject is Outlook.MailItem && uiInspector.IsWordMail())
          {
              Outlook.MailItem uiItem = (Outlook.MailItem)uiObject;
              Word.Document uiDoc = uiInspector.WordEditor as Word.Document;
              if (uiDoc != null)
              {
                  ***Word.Find uiFind = uiDoc.Range().Find;***
                  uiFind.Text = "ASA^$^$^#^#^#^#^#";
                  while (uiFind.Execute())
                  {
                      Microsoft.Office.Interop.Word.Range rng = uiFind.Parent;
                      rng.Hyperlinks.Add(rng, "http://stack.com=" + rng.Text + "outlook2007");
                      rng.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
                  }
              }
          }
  }

How can I rectify this error?我该如何纠正这个错误?

You need to cast this line:您需要转换此行:

Microsoft.Office.Interop.Word.Range rng = uiFind.Parent;

to

var rng = uiFind.Parent as Microsoft.Office.Interop.Word.Range;

Try尝试

Microsoft.Office.Interop.Word.Range rng = (Microsoft.Office.Interop.Word.Range)uiFind.Parent;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM