VBA将文件另存为预定名称

时间:2018-07-05 08:44:51

标签: vba ms-word

我正在尝试使用VBA在Word的保存对话框中预填充文件名。 我现在使用的代码是 这份文件:

Private Sub Document_New()
  Register_Event_Handler
End Sub

mdiEventConnect模块:

Dim X As New EventClassModule
Sub Register_Event_Handler()
  Set X.App = Word.Application
End Sub

EventClassModule类:

Public WithEvents App As Word.Application

Private Sub App_DocumentBeforeSave _
  (ByVal Doc As Document, _
  SaveAsUI As Boolean, _
  Cancel As Boolean)

  Dim intResponse As Integer

    intResponse = MsgBox("Do you really want to " _
    & "save the document?", _
    vbYesNo)

 If intResponse = vbNo Then
   Cancel = True
 Else:
   Dim fPth As Object
   Set fPth = Application.FileDialog(msoFileDialogSaveAs)

   With fPth
     .InitialFileName = "Testfile" & ".docx"
     .Title = "Bestand Opslaan"
     .InitialView = msoFileDialogViewList
     .Show
   End With
 End If

End Sub

我现在遇到的问题是保存对话框会按原样弹出,但是一旦我保存文件,就会弹出另一个保存对话框,其中没有预先确定的文件名。

如果有人对我有任何建议,将不胜感激。

0 个答案:

没有答案