如何使用word interop C#从现有word文档中获取smartart索引
问题描述:
亲爱的所有人,
我想评估smartart形状,它已插入现有的word文档中。文档中包含n个smartart形状。
我在谷歌上搜索过这个,但我找不到合适的。
请查看我的代码....
我尝试了什么:
Dear All,
I want to evaluate smartart shape, which is already inserted in the existing word document. Where the document contain n number of smartart shapes.
I alreay searched on google regarding this, but I did not find suitable one.
Please review my code....
What I have tried:
public static bool EvaluateSmartArtShape(IQuestion question, string filename, SmartArtShapes _smartArtShape)
{
WordInterop.Application wordApplication = GetOrCreateWordApplication(question.ObjectStore);
try
{
//Avoid screen flickering or unwanted alerts while initializing
wordApplication.ScreenUpdating = false;
WordInterop.WdAlertLevel displayAlertLevel = wordApplication.DisplayAlerts;
wordApplication.DisplayAlerts = WordInterop.WdAlertLevel.wdAlertsNone;
WordInterop.Document wordDocument = wordApplication.Documents.Open(filename);
//WORD
WordInterop.Shapes ws = wordDocument.Shapes;
foreach(WordInterop.Shape s in ws)
{
if (s.HasSmartArt == MsoTriState.msoTrue)
{
if (s == wordApplication.SmartArtLayouts[(int)_smartArtShape])//line 697
{
return true;
}
}
}
wordDocument.Close();
}
catch (Exception ee)
{
string strError = ee.ToString();
Cleanup(question.ObjectStore, true);
}
return false;
}
错误:
Error:
System.Runtime.InteropServices.COMException (0x80010105): The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))
at Microsoft.Office.Core.SmartArtLayouts.get_Item(Object Index)
at TeTec.Action.Office2013.Word.WordHelper.EvaluateSmartArtShape(IQuestion question, String filename, SmartArtShapes _smartArtShape) in C:\TETEC\Development\TeTec.Action\Office\Office2013\Word\WordHelper.cs:line 697
任何人都可以帮助我。
谢谢
Can any one please help me.
Thanks
答
public static bool EvaluateSmartArtShape(IQuestion question, string filename, SmartArtShapes _smartArtShape)
{
WordInterop.Application wordApplication = GetOrCreateWordApplication(question.ObjectStore);
try
{
//Avoid screen flickering or unwanted alerts while initializing
wordApplication.ScreenUpdating = false;
WordInterop.WdAlertLevel displayAlertLevel = wordApplication.DisplayAlerts;
wordApplication.DisplayAlerts = WordInterop.WdAlertLevel.wdAlertsNone;
WordInterop.Document wordDocument = wordApplication.Documents.Open(filename);
bool result = false;
//WORD
WordInterop.Shape tempSmartShape = null;
WordInterop.Shapes ws = wordDocument.Shapes;
foreach (WordInterop.Shape s in ws)
{
if (s.HasSmartArt == MsoTriState.msoTrue)
{
tempSmartShape = s;
SmartArt _smartArt = tempSmartShape.SmartArt;
string _smartArtName = _smartArt.Layout.Name;
if (_smartArtName.Replace(" ", "").Trim().ToLower() == _smartArtShape.ToString().Trim().ToLower())
result = true;
break;
}
}
wordDocument.Close();
return result;
}
catch (Exception)
{
Cleanup(question.ObjectStore, true);
}
return false;
}