天道酬勤,学无止境

mergefield

替换 Word 2003 文档中的 MergeFields 并保持样式(Replace MergeFields in a Word 2003 document and keep style)

问题 我一直在尝试创建一个库来替换 Word 2003 文档上的 MergeFields,一切正常,只是我在替换它时丢失了应用于该字段的样式,有没有办法保留它? 这是我用来替换字段的代码: private void FillFields2003(string template, Dictionary<string, string> values) { object missing = Missing.Value; var application = new ApplicationClass(); var document = new Microsoft.Office.Interop.Word.Document(); try { // Open the file foreach (Field mergeField in document.Fields) { if (mergeField.Type == WdFieldType.wdFieldMergeField) { string fieldText = mergeField.Code.Text; string fieldName = Extensions.GetFieldName(fieldText); if (values.ContainsKey(fieldName)) { mergeField.Select()

2021-07-11 09:45:35    分类:技术分享    c#   interop   ms-word   vsto   mergefield

Replace MergeFields in a Word 2003 document and keep style

I've been trying to create a library to replace the MergeFields on a Word 2003 document, everything works fine, except that I lose the style applied to the field when I replace it, is there a way to keep it? This is the code I'm using to replace the fields: private void FillFields2003(string template, Dictionary<string, string> values) { object missing = Missing.Value; var application = new ApplicationClass(); var document = new Microsoft.Office.Interop.Word.Document(); try { // Open the file foreach (Field mergeField in document.Fields) { if (mergeField.Type == WdFieldType.wdFieldMergeField)

2021-06-23 21:05:20    分类:问答    c#   interop   ms-word   vsto   mergefield

将光标定位在Word文档的开始/结尾(Position cursor at start/end of Word document)

问题 我们正在使用Word Interop从.Net处理Word 2007文档。 通常在字段中做一些事情,如: For Each f In d.Fields f.Select() //do stuff with fields here Next 这将保留所选文档中的最后一个字段。 因此,为了简洁起见,我们希望将光标定位在文档的末尾(或者甚至可以开始)。 谷歌搜索答案并没有太多...我能得到的最近的数字似乎表明我们需要让自己参与范围或书签。 有一个用于Document对象的GoTo方法,但是它提供的WdGoToItem选项都没有用。 没有简单的方法可以将光标发送到文档的末尾(或开始)吗? 编辑 我的部分问题是我不喜欢保留最后一个字段。 现在已经意识到我可以做到 f.Unlink 删除mergefield并将字段文本保留为纯文本。 哪个更整洁,无论我们是否也重新定位光标 回答1 @亚历山大·科耶夫尼科夫(Alexander Kojevnikov):感谢您的帮助,因为您使我处在正确的轨道上。 但是我发现我必须将.GoTo应用于单词选择对象,而不是文档。 如: Dim what As Object = Word.WdGoToItem.wdGoToLine Dim which As Object = Word.WdGoToDirection.wdGoToLast //below line

2021-04-26 15:54:11    分类:技术分享    .net   interop   ms-word   mergefield

How do I insert a mergefield into an If..else statement in VBA using Word MailMerge?

The below macro will input the field heading followed by the field value. The If statement should check the value of the field and if its greater than 30 the actual value is given, otherwise a zero. Instead of the value appearing if it's true, I just get the actual text I've typed, not it's value. Sub Macro1() Dim doc As Word.Document Dim dtField As Word.MailMergeDataField Dim sFieldName As String Dim sFieldActualName As String Dim j As Integer Set doc = ActiveDocument j = 1 For Each dtField In doc.MailMerge.DataSource.DataFields sFieldActualName = doc.MailMerge.DataSource.FieldNames(j).Name

2021-04-21 12:25:36    分类:问答    vba   ms-word   mailmerge   mergefield

Position cursor at start/end of Word document

We are manipulating our Word 2007 documents from .Net using Word Interop. Mostly doing stuff with fields as in: For Each f In d.Fields f.Select() //do stuff with fields here Next This leaves the last field in the document selected. So, for the sake of neatness we would like to position the cursor at the end of the document (or even the start would be OK). Googling for the answer doesn't throw up much ... the nearest I can get seems to be suggesting we need to involve ourselves with ranges or bookmarks. There's a GoTo method for the Document object but none of the WdGoToItem options it offers

2021-04-11 05:06:31    分类:问答    .net   interop   ms-word   mergefield