Excel Word 文档中的范围在更新后不保留格式 link
Excel Range in Word Document Does not keep Formatting after updating link
我有一个 Word 文件,里面有几个来自 excel 文件的链接 table。但是,当我更新 link 时,文件中的 table 不会保持 table 格式。
如果我通过 Word 手动完成,格式会保留。
我尝试使用以下代码以编程方式执行此操作:
using Word = Microsoft.Office.Interop.Word;
public void LaunchWord()
{
WordApp = new Word.Application();
Document = WordApp.Documents.Open(PathToTemporaryTemplate, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Word.WdOpenFormat.wdOpenFormatAuto, Missing.Value, true, Missing.Value, Missing.Value);
Fields = Document.Fields;
Document.UpdateStylesOnOpen = false;
Document.Activate();
}
然后我尝试像这样更新 link:
public void ChangeLinks(string pathToNewExcel)
{
var links = Fields.Cast<Word.Field>().AsEnumerable().Where(c => c.LinkFormat != null).ToList();
foreach (Word.Field field in links)
{
//field.LinkFormat.AutoUpdate = false;
//field.DoClick();
field.LinkFormat.SourceFullName = pathToNewExcel;
//field.OLEFormat.Activate();
field.OLEFormat.PreserveFormattingOnUpdate = true;
field.LinkFormat.Update();
//field.LinkFormat.SavePictureWithDocument = true;
//field.UpdateSource();
field.Update();
}
Document.Save();
}
注释是我尝试过但没有用的所有其他内容。
感谢任何帮助。
感谢您的宝贵时间!
经过大量的尝试和提问,我得出了以下解决方案:
foreach (Word.Field field in links)
{
field.Code.Text = field.Code.Text.Replace(oldPath, newPath);
field.Update();
}
如何在Code.Path属性中精确定位excel文件的路径有点棘手,但它会在更新字段后保留所有格式。
我有一个 Word 文件,里面有几个来自 excel 文件的链接 table。但是,当我更新 link 时,文件中的 table 不会保持 table 格式。
如果我通过 Word 手动完成,格式会保留。
我尝试使用以下代码以编程方式执行此操作:
using Word = Microsoft.Office.Interop.Word;
public void LaunchWord()
{
WordApp = new Word.Application();
Document = WordApp.Documents.Open(PathToTemporaryTemplate, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Word.WdOpenFormat.wdOpenFormatAuto, Missing.Value, true, Missing.Value, Missing.Value);
Fields = Document.Fields;
Document.UpdateStylesOnOpen = false;
Document.Activate();
}
然后我尝试像这样更新 link:
public void ChangeLinks(string pathToNewExcel)
{
var links = Fields.Cast<Word.Field>().AsEnumerable().Where(c => c.LinkFormat != null).ToList();
foreach (Word.Field field in links)
{
//field.LinkFormat.AutoUpdate = false;
//field.DoClick();
field.LinkFormat.SourceFullName = pathToNewExcel;
//field.OLEFormat.Activate();
field.OLEFormat.PreserveFormattingOnUpdate = true;
field.LinkFormat.Update();
//field.LinkFormat.SavePictureWithDocument = true;
//field.UpdateSource();
field.Update();
}
Document.Save();
}
注释是我尝试过但没有用的所有其他内容。
感谢任何帮助。
感谢您的宝贵时间!
经过大量的尝试和提问,我得出了以下解决方案:
foreach (Word.Field field in links)
{
field.Code.Text = field.Code.Text.Replace(oldPath, newPath);
field.Update();
}
如何在Code.Path属性中精确定位excel文件的路径有点棘手,但它会在更新字段后保留所有格式。