Event change of table in Word

北慕城南 提交于 2019-12-24 01:52:49

问题


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.

  1. Intersect does not exist in Word VBA.
  2. 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:

  1. Record the wholeDocument in a static string.
  2. When there is a change - record again in a new string.
  3. Compare the strings and color the differences.

However, this is a bit tough, as far as word does not have Changeevent 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!