在 C# 中使用单词时存在显式转换
此代码获取活动检查器窗口,即撰写邮件窗口,并对电子邮件正文执行搜索和替换功能。
但我收到错误:
无法将类型“object”隐式转换为“Microsoft.Office.Interop.Word.Range”。存在显式转换(您是否缺少强制转换?)
此处的代码供您参考。
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);
}
}
}
}
我该如何纠正此错误?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你需要投射这一行
:
You need to cast this line:
to
尝试
Try