问题
Private Sub Document_Change(ByVal Target As Range)
Set table = ActiveDocument.Tables(1)
If Not Intersect(table, Target) Is Nothing Then
Target.AutoFormat ApplyColor: Red
End If
End Sub
I have the following code, but it does not seem to work in VBA Word. Can anyone help me out?
回答1:
I suppose that you copied the code from Excel VBA and tried to rebuild it a bit. There are a few differences between Excel and Word VBA.
Intersect
does not exist in Word VBA.DocumentChange
event in Word works differently from what you would expect - It occurs when a new document is created, when an existing document is opened, or when another document is made the active document. (https://msdn.microsoft.com/en-us/library/office/ff822189.aspx)
If you want to make changes in red, you may do the following:
- Record the wholeDocument in a static string.
- When there is a change - record again in a new string.
- Compare the strings and color the differences.
However, this is a bit tough, as far as word does not have Change
event as we are expecting it (e.g. as in Excel). Thus, you should run the VBA code a few times automatically.
来源:https://stackoverflow.com/questions/43469988/event-change-of-table-in-word