Word VBA:在 ThisDocument class 模块和常规模块之间共享变量?
Word VBA: share variable between ThisDocument class module and a regular module?
在 Word 2013 中,我需要在 ThisDocument 和常规模块之间共享一个变量 (ccID)。这是代码:
“项目 (Toto)”→“Microsoft Word 对象”→“ThisDocument”:
Option Explicit
Public Sub Document_ContentControlAfterAdd(ByVal NewContentControl As ContentControl, ByVal InUndoRedo As Boolean)
Static ccID As String
Dim alertTime As Variant
' You can't affect document content while within this event, so save CC ID and create a timer that calls code to execute after the event has finished.
ccID = NewContentControl.ID
alertTime = Now + TimeValue("00:00:01")
Application.OnTime alertTime, "UpdateContentControl"
End Sub
“项目 (Toto)”→“模块”→“模块 1”:
' Give any newly-added content control a yellow background for user-friendliness
' (Called by ThisDocument -> Document_ContentControlAfterAdd)
Public Sub UpdateContentControl()
MsgBox ("UpdateContentControl started")
ActiveDocument.ContentControls(ccID).Range.Shading.BackgroundPatternColor = wdColorYellow
End Sub
当 UpdateContentControl 运行时,ccID 不在上下文中。如何在 ThisDocument 中的代码和常规模块之间共享变量?
这对我来说效果很好:
Public Sub Document_ContentControlAfterAdd(ByVal NewContentControl As ContentControl, _
ByVal InUndoRedo As Boolean)
NewContentControl.Range.Shading.BackgroundPatternColor = wdColorYellow
End Sub
不过它看起来像是清除了占位符文本,因此您可以使用 OnTime 执行以下操作:
这个文档:
Option Explicit
Dim LastCC
Public Sub Document_ContentControlAfterAdd(ByVal NewContentControl As ContentControl, ByVal InUndoRedo As Boolean)
LastCC = NewContentControl.id
Application.OnTime when:=Now + TimeSerial(0, 0, 1), _
Name:="UpdateContentControl"
End Sub
Public Property Get LastAddedCC()
LastAddedCC = LastCC
End Property
模块:
Public Sub UpdateContentControl()
Dim id
id = ThisDocument.LastAddedCC
Debug.Print "UpdateContentControl started with id: " & id
ThisDocument.ContentControls(id).Range.Shading.BackgroundPatternColor = wdColorYellow
End Sub
在 Word 2013 中,我需要在 ThisDocument 和常规模块之间共享一个变量 (ccID)。这是代码:
“项目 (Toto)”→“Microsoft Word 对象”→“ThisDocument”:
Option Explicit
Public Sub Document_ContentControlAfterAdd(ByVal NewContentControl As ContentControl, ByVal InUndoRedo As Boolean)
Static ccID As String
Dim alertTime As Variant
' You can't affect document content while within this event, so save CC ID and create a timer that calls code to execute after the event has finished.
ccID = NewContentControl.ID
alertTime = Now + TimeValue("00:00:01")
Application.OnTime alertTime, "UpdateContentControl"
End Sub
“项目 (Toto)”→“模块”→“模块 1”:
' Give any newly-added content control a yellow background for user-friendliness
' (Called by ThisDocument -> Document_ContentControlAfterAdd)
Public Sub UpdateContentControl()
MsgBox ("UpdateContentControl started")
ActiveDocument.ContentControls(ccID).Range.Shading.BackgroundPatternColor = wdColorYellow
End Sub
当 UpdateContentControl 运行时,ccID 不在上下文中。如何在 ThisDocument 中的代码和常规模块之间共享变量?
这对我来说效果很好:
Public Sub Document_ContentControlAfterAdd(ByVal NewContentControl As ContentControl, _
ByVal InUndoRedo As Boolean)
NewContentControl.Range.Shading.BackgroundPatternColor = wdColorYellow
End Sub
不过它看起来像是清除了占位符文本,因此您可以使用 OnTime 执行以下操作:
这个文档:
Option Explicit
Dim LastCC
Public Sub Document_ContentControlAfterAdd(ByVal NewContentControl As ContentControl, ByVal InUndoRedo As Boolean)
LastCC = NewContentControl.id
Application.OnTime when:=Now + TimeSerial(0, 0, 1), _
Name:="UpdateContentControl"
End Sub
Public Property Get LastAddedCC()
LastAddedCC = LastCC
End Property
模块:
Public Sub UpdateContentControl()
Dim id
id = ThisDocument.LastAddedCC
Debug.Print "UpdateContentControl started with id: " & id
ThisDocument.ContentControls(id).Range.Shading.BackgroundPatternColor = wdColorYellow
End Sub