开发者

An Explicit conversion exists when working with word in C#

This code gets the active inspector window i.e the compose mail window and performs the search and replace function for the body of the email.

But I am getting an error:

Cannot implicitly convert type 'object' to '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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜