gpt4 book ai didi

Word VBA REF field is not updated in IF field alternative branch(If字段替代分支中的Word VBA参考字段未更新)

转载 作者:bug小助手 更新时间:2023-10-25 18:30:07 24 4
gpt4 key购买 nike



Consider a simple Word document which contains three things:

考虑一个简单的Word文档,它包含三项内容:



  1. A bookmarked portion of the text to be used as a lookup value (bookmark name is a_bookmark)

  2. A legacy dropdown control to allow user to select one of two options (control bookmark name is a_control, option values are "One" and "Two", with "One" currently selected)

  3. IF field that analyzes the value of a_control dropdown and generates a fixed text if dropdown has a value of "One", and a lookup value stored in a_bookmark bookmark otherwise.


So the IF field code looks like this:

因此If字段代码如下所示:


{ IF { REF a_control } = "One" "Fixed value" { REF a_bookmark } }

Actually, the dropdown is not essential in this scenario, it is only used here to show that the execution of both branches of IF field is theoretically possible. In principle, one can further reduce this example to the following:

实际上,下拉列表在这个场景中并不是必需的,它在这里只是用来说明执行IF字段的两个分支在理论上是可能的。原则上,可以将这个例子进一步简化为以下几点:


{ IF 1 = 1 "Fixed value" { REF a_bookmark } }

So basically we use a lookup value in the branch that will not currently execute. In case of the dropdown this is achieved by the fact that the dropdown currently contains a value of "One" so that the main branch of the IF field is executed. In case of the simplified example this is achieved by virtue of 1 = 1 tautology.

因此,基本上我们在分支中使用当前不会执行的查找值。在DropDown的情况下,这是通过DropDown当前包含值“1”的事实来实现的,以便执行If字段的主分支。在简化的例子中,这是通过1=1重言式实现的。


Bottom line is that { REF a_bookmark } will not be currently executed, but it can be executed in principle in future when conditions change.

底线是当前不会执行{ref a_bookmark},但原则上可以在未来条件变化时执行。


Finally one more setup: imagine that the text inside of the a_bookmark bookmark has changed for some legitimate reason and we want it to be reflected (updated) in { REF a_bookmark } lookup.

最后还有一个设置:假设a_bookmark书签中的文本由于某些合法原因发生了更改,并且我们希望它在{ref a_bookmark}查找中得到反映(更新)。


Here is the problem: I can't force the update of the { REF a_bookmark } field either from VBA or from Word no matter what I try. So far I have tried:

问题是:无论我怎么尝试,我都无法从VBA或Word强制更新{ref a_bookmark}字段。到目前为止,我已经尝试过:



  1. Iterating through the fields in VBA, finding the field of interest and calling its Update method. No result, its Result.Text property continues to hold some old value.

  2. Updating all fields programmatically: ThisDocument.Fields.Update

  3. Invoking print preview programmatically: ThisDocument.PrintPreview (yes, Update fields before printing option is on)

  4. Invoking print preview from GUI

  5. Expanding the IF field entirely in GUI with Shift-F9, then selecting { REF a_bookmark } field in that expansion and invoking Update field context menu option specifically for that field.


So it seems to me that Word 2007 is just refusing to update a field that is nested into currently passive branch of the IF field, even if I talk to this field directly in VBA. Does anyone know a way to force-update such field?

因此,在我看来,Word 2007只是拒绝更新嵌套在If字段的当前被动分支中的字段,即使我直接在VBA中与该字段对话。有谁知道强制更新这样的场的方法吗?


Update: No, "just wait until the user selects the alternative option and the field will become an active branch and will update on its own" solution is not suitable, the reason I need the field update NOW is because VB script needs to replace that field with its actual value (unlink the field) at the time of running the script, way before the user will be using the document.

UPDATE:不,“只需等待用户选择替代选项,字段将成为活动分支并自动更新”解决方案不合适,我现在需要更新字段的原因是因为VB脚本需要在运行脚本时用其实际值替换该字段(取消链接该字段),远远早于用户将使用该文档。


更多回答
优秀答案推荐

(It's a while since you posted, so I expect you found a way to deal with this. But...)

(你发帖已经有一段时间了,所以我希望你能找到一种方法来处理这件事。但是...)


I was able to replicate this problem. I was surprised.

我能够复制这个问题。我很惊讶。


What to do depends partly on what the bookmark might contain.

做什么部分取决于书签可能包含的内容。


If it's just a piece of text, then you could start with

如果这只是一段文字,那么你可以从


{ IF { REF a_control } = "One" "Fixed value" "{ REF a_bookmark }" }

then, if you know that field index of that { REF a_bookmark } field is 10 (say),

然后,如果您知道{ref a_bookmark}字段的字段索引是10(比方说),



  • unprotect the document for forms


use, e.g.

用法,例如


With ActiveDocument.Fields(10)
.Result.Text = ActiveDocument.Bookmarks("a_bookmark").Range.Text
.Unlink
End With


  • reprotect for forms if that is what you need.


or if it could contain non-text material such as images, then

或者如果它可以包含非文本材料,如图像,则


With ActiveDocument.Fields(10)
.Result.FormattedText = ActiveDocument.Bookmarks("a_bookmark").Range.FormattedText
.Unlink
End With

But, be aware that if a_bookmark contains anything that Word would treat as a double quotation mark, the IF field containing the { REF a_bookmark } field will resolve as you might hope, whereas the IF field containing the content from the a_bookmark bookmark will be truncated after that mark. i.e. if you had to avoid that you'd have to process the bookmark text to deal with that problem.

但是,请注意,如果a_bookmark包含Word会视为双引号的任何内容,则包含{ref a_bookmark}字段的if字段将如您所希望的那样进行解析,而包含a_bookmark书签内容的if字段将在该标记之后被截断。例如,如果您必须避免这种情况,您将不得不处理书签文本来处理该问题。


Also, if you have to use an approach like that, you might be able to avoid having a { REF a_bookmark } field in the IF field in the first place, and just use another bookmark to indicate where you should put the content of a_bookmark. e.g.

此外,如果您必须使用这样的方法,则可以避免在if字段中首先使用{ref a_bookmark}字段,而只需使用另一个书签来指示应该将a_bookmark的内容放在哪里。例如:


{ IF { REF a_control } = "One" "Fixed value" "" }

but insert a point bookmark called b_bookmark between the final "".

但在最后的“”之间插入一个名为b_bookmark的点书签。


Then, e.g.

然后,例如


ActiveDocument.Bookmarks("b_bookmark").Range.Text = ActiveDocument.Bookmarks("a_bookmark").Range.Text

or whatever.

或者什么都行。


更多回答

This method implies that I know the bookmark name referred to in the REF field. In my use case there are literally hundreds of such fields that need to be programmatically unlinked, so I can't hardcode the name of the bookmark, as it is different for every IF/REF pair. And fetching the name of the bookmark from the REF field programmatically has proven to be very ambiguous and unreliable (see this post of mine: stackoverflow.com/questions/76923717/…

此方法意味着我知道ref字段中引用的书签名称。在我的用例中,确实有数百个这样的字段需要以编程方式取消链接,所以我不能硬编码书签的名称,因为每个if/ref对都是不同的。从REF字段以编程方式获取书签的名称已被证明是非常模糊和不可靠的(参见我的这篇帖子:Stackoverflow.com/Questions/76923717/…

So what I ended up doing to solve the problem is this: iterate through all fields, detect a nested REF field through completely magic method (basically Copy method fails with exception on such fields due to unexplained reasons, so I trap that exception), then create a new blank REF field in the end of the document and copy the Code.Text of my field into its Code.Text property, truncating "REF" in the beginning, force-update the new field, then copy its Result.Text into Result.Text of the nested field, finally erase temporary field at the end of the document. Ugly, but it works.

因此,我最终要做的是:迭代所有字段,通过完全神奇的方法检测嵌套的ref字段(基本上复制方法失败,因为无法解释的原因导致此类字段出现异常,所以我捕获了该异常),然后在文档末尾创建一个新的空白ref字段,并将我的字段的Code.Text复制到其Code.Text属性中,在开头截断“ref”,强制更新新字段,然后将其Result.Text复制到嵌套字段的Result.Text中,最后擦除文档末尾的临时字段。很难看,但很管用。

@RegusPregus since the document cannot actually work properly as it is, I suppose I'd try to modify it so that it could. What I'd probably start with is something like mark the places where I needed to insert the bookmarked text with bookmarks (as I suggested) with a name related to the original name, e.g. use x_a_bookmark to "link" to a_bookmark, then iterate through all bookmarks starting with names starting with "x_" and replace their content with the "linked" bookmark content, I.e. a_bookmark. But maybe you aren't in a position to do that.

@RegusPregus由于文档实际上不能正常工作,我想我会尝试修改它,以便它可以。我可能会从这样的东西开始:用与原始名称相关的名称标记需要插入书签文本的书签(如我所建议的),例如使用x_a_bookmark“链接”到a_bookmark,然后遍历所有以“x_”开头的名称开始的书签,并将其内容替换为“链接”的书签内容,即a_bookmark。但也许你没资格这么做。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com