在特定路径上使用宏保存工作簿

时间:2017-09-05 15:55:41

标签: excel-vba save excel-2007 vba excel

我为项目创建了一个excel工作簿。这个工作簿包含很多宏,应该像模板一样。

用户打开"模板" -Workbook后,宏正在调用。此宏应将此当前文件保存到特定路径,并在计算机上使用新名称(当前日期以及当前时间是否存在相同文件)。

"保存"宏完成,用户在新工作簿上工作

我使用(可能)保存此工作簿的常用方法(编辑:完成工作保存 - 子)

  ' Save the File into a new Directory

  '
  ' Set a new File name , to prevent overwriting the template
  ' Filenname: date of today
  ' if the same file exists, set the current time to the current date
  ' Filenname : date-of-today + Time-of-Now


  Dim DateOfToday, TimeOfToday As String
  Dim fileName, fileNameWithTime As String
  Dim FilePathComplete, FilePathCompletesTime As String


 'get the current Day and Time
  DateOfToday = Format(Now, "dd-mm-yyyy")
  TimeOfToday = Format(Now, "hh.mm")

  'Get the Documents Directory Path
  FilePath = FileInfo.GetSpecialFolder(CSIDL_PERSONAL) + "\Workbooks\" 

 ' name of the directory
  fileName = "Workbooks-"+DateOfToday+".xlsm"

 ' filename if the file already exists
  filenameWithTime = "Workbooks-"+DateOfToday+"-"+TimeOfToday+".xlsm"

 FilePathComplete = specialFolderPath + fileName
 FilePathCompletesTime = specialFolderPath + fileNameWithTime 

 On Error GoTo DispFehler
 Application.DisplayAlerts = False

 If Dir(FilePathComplete) <> "" Then

 ChDir specialFolderPath
ThisWorkbook.SaveAs FileName:=FilePathCompletesTime,     FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
Else
ChDir specialFolderPath
ThisWorkbook.SaveAs FileName:=FilePathComplete,     FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False


End If

DispFehler:
Application.DisplayAlerts = True

0 个答案:

没有答案