Word Interop中的字段对象出现意外行为。 [英] Unexpected behavior from Fields Object in Word Interop.
问题描述
大家好,
我说的是字段界面(https://msdn.microsoft.com/en-us/library/microsoft.office.interop .word.fields.aspx)
I'm talking about the fields interface (https://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.fields.aspx)
在我使用Word = Microsoft.Office.Interop启动一个word文档之后:
After I launch a word document like:
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();
}
我从Fields对象中得到意外行为。
I get unexpected behavior from the Fields object.
- 我无法访问它通过数组索引(Fields [i])
- 属性"项目"不会出现在任何地方
- 我可以预测集合,但如果我更改当前字段项的属性,则下一个foreach循环将迭代同一项,从而产生无限循环。
我的最终目标是找到包含链接的字段集合,因此我可以访问以下集合:
My ultimate goal is to find the collection of fields with links inside, so I can access the collection like:
var links = Fields.Cast<Word.Field>().AsEnumerable().Where(c => c.LinkFormat != null).ToList();
最后,这将不允许我在更改LinkFormat.SourceFullName时保留格式(请参阅https://social.msdn.microsoft.com/Forums/en-US/c1193f6d-4dcd-4bde-a112-c03a8eaed2ae/ excel-range-in-word-document-not-keep-formatting-after-updating-link?forum = worddev)
In the end, this will not allow me to keep the formatting when changing the LinkFormat.SourceFullName (see https://social.msdn.microsoft.com/Forums/en-US/c1193f6d-4dcd-4bde-a112-c03a8eaed2ae/excel-range-in-word-document-does-not-keep-formatting-after-updating-link?forum=worddev)
如果我尝试手动设置field.Code .Text属性,我得到一个 System.Runtime.InteropServices.COMException:'范围不能删除'
And if I try to manually set a field.Code.Text property, I get a System.Runtime.InteropServices.COMException: 'The range cannot be deleted'
我参考 Microsoft.Office.Interop.Word版本15
/运行时版v2.0.50727 / Visual Studio 2017版本15.1
I reference Microsoft.Office.Interop.Word version 15 / runtime v2.0.50727 / Visual Studio 2017 ver 15.1
感谢任何帮助。
Any help is appreciated.
感谢您的时间!
Thank you for your time!
推荐答案
链接Excel对象的大小调整是设计但是有一个注册表项可以防止它:
1.打开注册表编辑器,然后导航到:
$
HKEY_CURRENT_USER \Software \ Microsoft /\\\\\\\\\\\\\ Excel \选项
其中#是Office版本。
2.添加新的DWORD值:QFE_Boston
3.设置新的DWORD价值为1
The resizing of Linked Excel Objects is by design but there is registry key to prevent it:
1. Open the Registry Editor, then navigate to:
HKEY_CURRENT_USER\Software\Microsoft\Office\#\Excel\Options
where # is the Office version.
2. Add a new DWORD value: QFE_Boston
3. Set the new DWORD value to 1
办公室版本:
2010 - 14.0
2013 - 15.0
2016 - 16.0
Office versions:
2010 - 14.0
2013 - 15.0
2016 - 16.0
至于循环问题,您可以向后处理集合。
As for the looping issues, you could process the collection backwards.
这篇关于Word Interop中的字段对象出现意外行为。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!