更新 Word 文件打开时的页眉、页脚和正文字段。

huangapple go评论102阅读模式
英文:

Updating fields in headers, footers, and main story in Word on file open

问题

我使用了该网站上的代码,它们都可以正常运行,只需手动运行。以下是代码示例。

为什么我打开文档时这个不能自动运行?

  1. Private Sub Document_AutoOpen()
  2. UpdateAllFields
  3. End Sub
  4. Sub UpdateAllFields()
  5. Dim rng As Range
  6. Dim sec As Section
  7. Dim hdrFtr As HeaderFooter
  8. ' 更新正文中的域
  9. For Each rng In ActiveDocument.StoryRanges
  10. UpdateFieldsInRange rng
  11. Do While Not (rng.NextStoryRange Is Nothing)
  12. Set rng = rng.NextStoryRange
  13. UpdateFieldsInRange rng
  14. Loop
  15. Next rng
  16. ' 更新页眉和页脚中的域
  17. For Each sec In ActiveDocument.Sections
  18. For Each hdrFtr In sec.Headers
  19. UpdateFieldsInRange hdrFtr.Range
  20. Next hdrFtr
  21. For Each hdrFtr In sec.Footers
  22. UpdateFieldsInRange hdrFtr.Range
  23. Next hdrFtr
  24. Next sec
  25. End Sub
  26. Sub UpdateFieldsInRange(rng As Range)
  27. Dim fld As Field
  28. For Each fld In rng.Fields
  29. fld.Update
  30. Next fld
  31. End Sub
英文:

I used code from questions on this site that all worked, just had to manually run them. Below is a code example.

Why isn't this working automatically when I open the document?

  1. Private Sub Document_AutoOpen()
  2. UpdateAllFields
  3. End Sub
  4. Sub UpdateAllFields()
  5. Dim rng As Range
  6. Dim sec As Section
  7. Dim hdrFtr As HeaderFooter
  8. ' Update fields in the body
  9. For Each rng In ActiveDocument.StoryRanges
  10. UpdateFieldsInRange rng
  11. Do While Not (rng.NextStoryRange Is Nothing)
  12. Set rng = rng.NextStoryRange
  13. UpdateFieldsInRange rng
  14. Loop
  15. Next rng
  16. ' Update fields in headers and footers
  17. For Each sec In ActiveDocument.Sections
  18. For Each hdrFtr In sec.Headers
  19. UpdateFieldsInRange hdrFtr.Range
  20. Next hdrFtr
  21. For Each hdrFtr In sec.Footers
  22. UpdateFieldsInRange hdrFtr.Range
  23. Next hdrFtr
  24. Next sec
  25. End Sub
  26. Sub UpdateFieldsInRange(rng As Range)
  27. Dim fld As Field
  28. For Each fld In rng.Fields
  29. fld.Update
  30. Next fld
  31. End Sub

答案1

得分: 2

Wherever you got Document_AutoOpen from, it's wrong. You either need a routine named AutoOpen in a standard module, or, in the ThisDocument module, you have an event handler named Document_Open.

英文:

Wherever you got Document_AutoOpen from, it's wrong. You either need a routine named AutoOpen in a standard module, or, in the ThisDocument module, you have an event handler named Document_Open.

更新 Word 文件打开时的页眉、页脚和正文字段。

huangapple
  • 本文由 发表于 2023年5月25日 23:03:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76333779.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定