是否可以構建類似 AutoOpen 但不太通用的東西?所以我的意思是一個宏,它在你打開檔案 xyz.docx 時執行。
uj5u.com熱心網友回復:
請復制“Normal.dotm”“ThisDocument”代碼模塊中的下一個代碼:
Option Explicit
Const docName = "xyz.docx" 'use here the document name you need
Private Sub Document_Open()
If ActiveDocument.Name = docName Then
MsgBox ActiveDocument.Name & " has been opened..."
End If
End Sub
任何打開的檔案Open都會觸發該事件。
uj5u.com熱心網友回復:
這是在檔案打開時自動運行的示例宏。此示例檢查用戶是否嘗試打開模板進行編輯,然后它會根據模板創建一個新檔案。(在打開檔案時按住 Shift 鍵繞過宏)。
此示例僅在放置在啟用宏的模板中時才有意義,但您也可以將類似的內容添加到啟用宏的檔案中。還必須將檔案位置設為 Windows 中的受信任位置。
Sub AutoOpen()
Dim PathTemp$, NameTemp$
If ActiveDocument.Type = wdTypeTemplate Then
NameTemp$ = ActiveDocument.Name
PathTemp$ = ActiveDocument.Path
Documents.Add Template:=PathTemp$ & Application.PathSeparator & NameTemp$
For Each fWindow In Application.Windows
If fWindow.Caption = NameTemp$ Then
fWindow.Close SaveChanges:=wdDoNotSaveChanges
End If
Next fWindow
End If
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/364554.html
上一篇:ExcelVBA-添加行
下一篇:excel范圍過濾和計數行
