问题描述
我们为我们所做的尝试
-该字段表示为
{INCLUDEPICTURE“ M:\ NVCSync \ Testing 7.16.2020.1 \ Hotel Excel 7.16.1 \ ExcelTables \ T_TestSum.emf”}
例如,我们有类似T_TestSum.emf
的图像3。找到图像后,我们选择该图像范围,将其删除并再次添加该字段。(以便更新重新刷新的图像)
Word.Field字段= r.Fields.Add(r,Word.WdFieldType.wdFieldIncludePicture,“”“ + vtablelocation.Replace(@”“,@” \“)+”“”,preserveformatting);
上面的逻辑对我们有用,但是我们有性能瓶颈。
对于200多个图像,需要120秒才能更新。
msdn建议的其他方式
-
选择文档中的所有字段并更新每个字段。
doc.Range(ref start,ref end).Select(); try { foreach (Word.Field field in doc.Fields) { field.UpdateSource(); } } catch (Exception ex) { } the above code works for smaller documents,images update quickly. it fails to update the images if documents are bigger(we didnt get any errors)
-
我们尝试使用异步等待方式来更新字段,但其性能最差。
3.parallel.foreach似乎可以正常工作,但由于文件较大且要更新许多图像,因此它失败,从而导致出现ContextSwitchDeadlock错误。enter code here
private static async Task UpdateDocumentTableswithouttasking()
{
Word.Application appW = Globals.ThisAddIn.Application;
Word.Document doc = appW.ActiveDocument;
doc.Fields.ToggleShowCodes();
try
{
Microsoft.Office.Interop.Word.Paragraphs DocPar = doc.Paragraphs;
foreach (var par in DocPar)
{
Word.Paragraph p = (Word.Paragraph)par;
string s = p.Range.Text.ToString();
Word.Range pb = p.Range;
if (!String.IsNullOrEmpty(s) && (s.Contains("INCLUDEPICTURE")) || (s.Contains("DOCVARIABLE") && s.Contains("PICTURECUSTOM")) && s != "\r")
{
var alignment = p.Range.ParagraphFormat.Alignment;
Word.WdColorIndex color = p.Range.HighlightColorIndex;
List<DocumentTables> lstDocTables = new List<DocumentTables>();
List<Documentvars> lstDocFields = new List<Documentvars>();
string[] arr = new string[] { };
string[] arrDocVarsAndPictures = new string[] { };
if (s.Contains("LINK"))
{
arrDocVarsAndPictures = s.Split(new string[] { "LINK" },StringSplitOptions.None);
}
else
{
arrDocVarsAndPictures = s.Split(new string[] { "DOCVARIABLE" },StringSplitOptions.None);
}
for (int cnt = arrDocVarsAndPictures.Length - 1; cnt >= 0; cnt--)
{
string strArr = arrDocVarsAndPictures[cnt].ToString();
string[] ArrwithImages = strArr.Split(new string[] { "INCLUDEPICTURE" },StringSplitOptions.None);
if (ArrwithImages.Length > 0)
{
for (int cntimages = ArrwithImages.Length - 1; cntimages >= 0; cntimages--)
{
string strArrWithImages = ArrwithImages[cntimages].ToString();
//This means the field is for picture and used when mapping picture or showing error message.
if (strArrWithImages.Contains("T_") || strArrWithImages.Contains("t_"))
{
try
{
string pattern = "\"";
string SelectedField = "";
if (strArrWithImages.Contains("T_"))
{
int i = strArrWithImages.IndexOf("T_");
SelectedField = strArrWithImages.Substring(i + 0);
}
else if (strArrWithImages.Contains("t_"))
{
SelectedField = strArrWithImages.Substring(strArrWithImages.IndexOf("t_") + 0);
}
string[] arrF = SelectedField.Split(new string[] { pattern },StringSplitOptions.None);
string fieldName = arrF[0].Trim().Replace(strFileExtension,"").Replace(".jpg","");
var wrksht = "";
if (strArrWithImages.Contains("Swrksht_"))
{
int i = strArrWithImages.IndexOf("Swrksht_");
wrksht = strArrWithImages.Substring(i + 0);
wrksht = wrksht.Split(new string[] { "_" },StringSplitOptions.None)[1];
if (wrksht == "T" && fieldName.Contains("_"))
{
wrksht = "";
}
}
foreach (Word.Field myMergeField in p.Range.Fields)
{
Word.Range rngFieldCode = myMergeField.Code;
string fieldText = rngFieldCode.Text;
string pattern1 = "\"";
string SelectedField1 = "";
if (fieldText.Contains("T_"))
{
int i = fieldText.IndexOf("T_");
SelectedField1 = fieldText.Substring(i + 0);
}
else if (fieldText.Contains("t_"))
{
SelectedField1 = fieldText.Substring(fieldText.IndexOf("t_") + 0);
}
if (SelectedField1.Contains("\\t"))
{
pattern1 = "\\t";
}
string[] arrF1 = SelectedField1.Split(new string[] { pattern1 },StringSplitOptions.None);
string fieldName1 = arrF1[0].Trim().Replace(strFileExtension,"");
if (fieldName == fieldName1)
{
myMergeField.Select();
Word.Range found = Globals.ThisAddIn.Application.Selection.Range;
Word.Selection s1 = Globals.ThisAddIn.Application.Selection;
s1.Range.Delete();
Word.Range r = Globals.ThisAddIn.Application.ActiveDocument.Range(found.Start,found.Start);
try
{
r.ParagraphFormat.Alignment = alignment;
}
catch (Exception ex)
{
}
//r.Select();
Globals.ThisAddIn.Application.Selection.Font.ColorIndex = Word.WdColorIndex.wdAuto;
TableInfo t = new TableInfo();
try
{
if (String.IsNullOrEmpty(wrksht))
{
t = ThisAddIn.lstTables.OrderBy(z => z.workSheetName).Where(x => x.Tablename == fieldName).FirstOrDefault();
}
else
{
t = ThisAddIn.lstTables.Where(x => x.Tablename == fieldName && x.worksheetCodename == wrksht).FirstOrDefault();
}
if (t != null)
{
if (!String.IsNullOrEmpty(t.Tablename))
{
//myMergeField.Update();
found.Font.ColorIndex = Word.WdColorIndex.wdBlack;
var vtablelocation = t.TableLocation.Replace("Swrksht_","Swrksht_" + t.worksheetCodename + "_").Replace("Swrksht__","Swrksht_").Replace("__","_");
Word.Field field = r.Fields.Add(r,Word.WdFieldType.wdFieldIncludePicture,"\"" + vtablelocation.Replace(@"\",@"\\").Replace(strFileExtension,strFileExtension) + "\"",preserveformatting);
if (t.ApplySizing == 1)
{
try
{
field.Inlineshape.Height = (float)t.height;
}
catch
{
}
try
{
field.Inlineshape.Width = (float)t.width;
}
catch
{
}
}
}
}
}
catch (Exception ex)
{
}
break;
}
}
}
catch (Exception ex)
{
}
}
}
}
}
//}
}
}
}
catch (Exception ex)
{
log.Error("\r\n" + "Error: Error occured while updating tables" + "\r\n" + ex);
}
doc.Fields.ToggleShowCodes();
foreach (Word.Field field in doc.Fields)
{
var x = field.ShowCodes;
if (x)
{
field.ShowCodes = false;
}
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)