[英]Automatically execute Macro in word VBA
I have a macro which is executed while opening the word document.我有一个在打开 word 文档时执行的宏。 The problem is empty word object (Without file/doc) is opened macro works fine while opening a file/doc.
问题是空字 object(没有文件/文档)打开宏在打开文件/文档时工作正常。 If the fresh word document opened a file/doc then the macro doesn't work and shows error message.
如果新的 word 文档打开了文件/文档,则宏不起作用并显示错误消息。
My macro: Module1 -> normal.dot
我的宏:Module1 -> normal.dot
Dim varData As Variant
Dim font_type() As String
Dim text As String
Dim font_name As Variant
varData = Array("?", "[[", "\tag", "$", "()", "|", "\label", "\ce", "insert_eq")
For Each font_name In varData
Selection.HomeKey Unit:=wdStory
Call ClearFindSettings
Selection.Find.text = font_name
If Selection.Find.Execute = True Then
Selection.HomeKey Unit:=wdStory
'Selection.TypeParagraph
Selection.MoveUp
alert_msg = "The '" & font_name & "' Text Present in doc"
alert_msg_grp = alert_msg_grp & vbNewLine & alert_msg
End If
Next
Call Error_alert(alert_msg_grp)
End Sub
ThisDocument.
这个文件。
Private Sub Document_Open()
Call AutoExec
End Sub
Private Sub Document_New()
Call AutoExec
End Sub
Error Msg:
错误消息:
Runtime Error '91'
Selection.HomeKey Unit:=wdStory
Could someone help out on this one.有人可以帮忙解决这个问题。
Please wrap around whether document is available, since application open no document is not opened that is why you got this error:请环绕文档是否可用,因为应用程序打开没有未打开文档,这就是您收到此错误的原因:
If (Application.Documents.Count > 0) Then
Dim varData As Variant
Dim font_type() As String
Dim text As String
Dim font_name As Variant
varData = Array("?", "[[", "\tag", "$", "()", "|", "\label", "\ce", "insert_eq")
For Each font_name In varData
Selection.HomeKey Unit:=wdStory
Call ClearFindSettings
Selection.Find.text = font_name
If Selection.Find.Execute = True Then
Selection.HomeKey Unit:=wdStory
'Selection.TypeParagraph
Selection.MoveUp
alert_msg = "The '" & font_name & "' Text Present in doc"
alert_msg_grp = alert_msg_grp & vbNewLine & alert_msg
End If
Next
Call Error_alert(alert_msg_grp)
END IF
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.