【发布时间】:2021-07-15 15:05:04
【问题描述】:
如何让我的 VBA 宏在 Word 文档打开时自动运行,而无需从开发人员功能区或按钮启动宏?
到目前为止,我尝试了这个,但它不适合我:
“FindChar”子在我运行时运行良好。我没有收到任何错误消息。脚本似乎没有运行。
Private Sub Document_open()
Call FindChar
End Sub
Sub FindChar()
Dim oTbl As Table
Dim stT As Long, enT As Long
Dim stS As Long, enS As Long
With Selection.Find ' Replacement
.Text = "["
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
End With
For Each oTbl In ActiveDocument.Tables
If oTbl.Shading.BackgroundPatternColor = RGB(176, 255, 137) Then
oTbl.Columns(1).Select
Do While Selection.Find.Execute
stT = oTbl.Range.Start
enT = oTbl.Range.End
stS = Selection.Range.Start
enS = Selection.Range.End
If stS < stT Or enS > enT Then Exit Do
Selection.Collapse wdCollapseStart
Selection.Find.Execute Replace:=wdReplaceOne
Loop
Selection.Collapse wdCollapseEnd
End If
Next
End Sub
【问题讨论】:
-
不工作 不是很有帮助。你的代码被触发了吗?有什么事吗?您收到错误消息吗?直接调用而不是通过 Open-Event 调用时,
FindChar-routine 是否正常工作? -
FindChar 在我运行时运行良好。我没有收到任何错误消息。脚本似乎没有运行。
-
在事件触发器中添加类似
Debug.Print "Document-Open triggered"的语句,并检查是否有内容写入即时窗口。您的代码在哪里?您需要将Document_open()-routine 放入 Document-module -
您到底在哪里有 Document_Open 子?