vba 保存事件后触发 MS Word 宏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3636625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trigger MS Word macro after save event
提问by blokeley
My MS Word 2007 template has a footer with the filename in it. The user is going to open the template and do a "Save As..." to make their document.
我的 MS Word 2007 模板有一个包含文件名的页脚。用户将打开模板并执行“另存为...”以制作他们的文档。
I want the filename shown in the footer to update immediately to the new filename.
我希望页脚中显示的文件名立即更新为新文件名。
Is there an AfterSaveEvent
or something that I can use as a hook to start my VBA script that does the update?
有没有AfterSaveEvent
什么东西可以用作钩子来启动我的 VBA 脚本来进行更新?
Or is there a much easier way?
或者有更简单的方法吗?
采纳答案by Dr. belisarius
Just create a macro like this (I believe it works better if included in the Normal.dot)
只需创建一个这样的宏(我相信如果包含在 Normal.dot 中它会更好)
Sub FileSaveAs()
'
' FileSaveAs Macro
' Saves a copy of the document in a separate file
'
Dialogs(wdDialogFileSaveAs).Show
'returns the name including the .doc extension
ChosenFileNameAndExtension = ActiveDocument.Name 'Or use .FullName
' Your code here
End Sub
It will be triggered whenever the user selects "File Save As"
只要用户选择“文件另存为”就会触发
HTH!
哼!
回答by blokeley
This worked based on @belisarius' answer:
这是基于@belisarius 的回答:
Sub UpdateAll()
Dim oStory As Object
Dim oToc As Object
'Exit if no document is open
If Documents.Count = 0 Then Exit Sub
Application.ScreenUpdating = False
For Each oStory In ActiveDocument.StoryRanges
oStory.Fields.Update 'Update fields in all stories
Next oStory
For Each oToc In ActiveDocument.TablesOfContents
oToc.Update 'Update table of contents
Next oToc
Application.ScreenUpdating = True
End Sub
Sub FileSaveAs()
'
' FileSaveAs Macro
' Saves a copy of the document in a separate file
'
Dialogs(wdDialogFileSaveAs).Show
UpdateAll
End Sub
回答by Nishara MJ
We know that the Aftersave
event isn't available for Microsoft Word. But Word allows us to use BeforeSave
event. I have implemented this solution and it works fine.
我们知道该Aftersave
活动不适用于 Microsoft Word。但是 Word 允许我们使用BeforeSave
事件。我已经实施了这个解决方案,它工作正常。
First we have to implement Application.onTime
method in Word BeforeSave
event as follows
首先我们要Application.onTime
在WordBeforeSave
事件中实现方法如下
Private Sub mobjWord_DocumentBeforeSave(ByVal Doc As Word.Document, SaveAsUI As Boolean, Cancel As Boolean)
Word.Application.OnTime Now + TimeValue("00:00:02"), "AfterSave"
End Sub
This method will call the method called AfterSave
after 2 seconds.
此方法将调用AfterSave
2 秒后调用的方法。
Public Sub AfterSave()
While Word.ActiveDocument.Application.BackgroundSavingStatus <> 0
DoEvents
Wend
'Implement your code here
End Sub
In this method the while loop will be circulated until the document save process is completed. So you can implement you code after the while loop.
在此方法中,while 循环将一直循环,直到文档保存过程完成。所以你可以在 while 循环之后实现你的代码。