[英]Automatically execute Macro in word VBA
我有一個在打開 word 文檔時執行的宏。 問題是空字 object(沒有文件/文檔)打開宏在打開文件/文檔時工作正常。 如果新的 word 文檔打開了文件/文檔,則宏不起作用並顯示錯誤消息。
我的宏: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
這個文件。
Private Sub Document_Open()
Call AutoExec
End Sub
Private Sub Document_New()
Call AutoExec
End Sub
錯誤消息:
Runtime Error '91'
Selection.HomeKey Unit:=wdStory
有人可以幫忙解決這個問題。
請環繞文檔是否可用,因為應用程序打開沒有未打開文檔,這就是您收到此錯誤的原因:
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.