我不是程序员,但我对此有所了解。我目前有一个单词宏,用于保存具有默认名称和当前日期的新文档。我试图在excel 2010中做同样的事情,但不知道如何。
非常感谢任何帮助。 这是我使用的词的宏。
Sub FileSave()
If ActiveDocument.Path = "" Then
With Dialogs(wdDialogFileSaveAs)
.Name = MakeDocName
.Show
End With
Else
ActiveDocument.Save
End If
End Sub
Function MakeDocName() As String
Dim theName As String
Dim uscore As String
uscore = "_"
theName = "DocType_DocDescription_"
theName = theName & Format(Now, "yyyy-mm-dd")
MakeDocName = theName
End Function
Sub FileSaveAs()
With Dialogs(wdDialogFileSaveAs)
.Name = MakeDocName
.Show
End With
End Sub
答案 0 :(得分:2)
更新以回应评论。将此代码添加到ThisWorkbook
模块将在保存任何打开的文档时生成自定义文件名。
Option Explicit
Private WithEvents App As Excel.application
Private Sub Workbook_Open()
Set App = application
End Sub
Private Sub App_WorkbookBeforeSave(ByVal Wb As Workbook, ByVal SaveAsUI As Boolean, Cancel As Boolean)
App.EnableEvents = False
If Wb.Path = "" Then
With App.Dialogs(xlDialogSaveAs)
Call .Show(MakeDocName, xlOpenXMLWorkbookMacroEnabled)
End With
Else
Wb.Save
End If
App.EnableEvents = True
Cancel = True
End Sub
Function MakeDocName() As String
Dim theName As String
Dim uscore As String
uscore = "_"
theName = "DocType_DocDescription_"
theName = theName & Format(Now, "yyyy-mm-dd")
MakeDocName = theName
End Function
答案 1 :(得分:2)
使用内置的.GetSaveAsFilename
语法是
expression.GetSaveAsFilename(InitialFilename,FileFilter,FilterIndex,Title,ButtonText)
实施例
Option Explicit
Sub Sample()
Dim Ret
Dim InitFile As String
InitFile = "MyFile" '<~~ You can set Default name here
Ret = Application.GetSaveAsFilename(InitialFileName:=InitFile, _
fileFilter:="Excel Files (*.xlsx), *.xlsx", _
FilterIndex:=1, _
Title:="Save As")
If Ret <> False Then
'
'~~> Code to save the file
'
End If
End Sub