AW: Macros laufen nicht mehr nach Speichern der Datei
03.04.2012 16:50:06
Tino
Hallo,
xlExcel8 ist für .xls Datei nicht für .xlsm
xlsx = xlOpenXMLWorkbook
xlsm = xlOpenXMLWorkbookMacroEnabled
xlsb = xlExcel12
xls = xlExcel8
Versuch mal so, Code bezieht sich auf die aktive Mappe.
Option Explicit
Sub Save_As_Version()
Dim Pfad$, Dateiname$, booIsSave As Boolean
Pfad = ThisWorkbook.Path 'Pfad anpassen
Dateiname = ThisWorkbook.Name 'Dateiname anpassen + Extention (.xls oder .xlsm usw...)
booIsSave = Speichern_Unter(Dateiname, Pfad)
If booIsSave Then
MsgBox "Datei wurde gespeichert"
End If
End Sub
'Function Dialog Speichern unter **********************************************************************
Function Speichern_Unter(ByVal sFile_Name As String, ByVal sPath As String) As Boolean
Dim ArrFileFormat, varIndex, sExtension$, iFileFormat%
'Extension der Datei
sExtension$ = Right$(sFile_Name, Len(sFile_Name) - InStrRev(sFile_Name, "."))
'zulässige Erweiterung, evlt. weitere hinzufügen
ArrFileFormat = Array("xlsx", "xlsm", "xlsb", "xls") 'Datei Versionen
varIndex = Application.Match(sExtension, ArrFileFormat, 0) 'Index für Dialog und Angabe
'Formatabfrage
iFileFormat = File_Format(sExtension$)
If iFileFormat = 0 Then
MsgBox "Auswahl ist kein Excel File!", vbCritical
Exit Function
End If
sPath = IIf(Right$(sPath, 1) = "\", sPath, sPath & "\")
'welche Excelversion
On Error GoTo Error_Handler:
If Val(Application.Version) > 11 Then
'Datei im richtigen Format speichern
ActiveWorkbook.SaveAs sPath & sFile_Name, iFileFormat
ElseIf iFileFormat = 56 Then
'bis Version 11, speichern als *.xls
ActiveWorkbook.SaveAs sPath & sFile_Name
Else
MsgBox "Extension kann mit dieser Excel- Version nicht gespeichert werden!", vbCritical
Exit Function
End If
Error_Handler:
If Err.Number = 0 Then
Speichern_Unter = True
Else
MsgBox Err.Description, vbCritical + vbMsgBoxSetForeground + vbMsgBoxHelpButton, _
"Error: " & Err.Number, _
Err.HelpFile, _
Err.HelpContext
End If
End Function
'Funktion zum ermitteln des Dateiformats ab xl2007
'zulässige Erweiterung, evlt. weitere hinzufügen
Private Function File_Format(sExtension$)
Select Case LCase(sExtension$)
Case "xlsx": File_Format = 51
Case "xlsm": File_Format = 52
Case "xlsb": File_Format = 50
Case "xls": File_Format = 56
End Select
End Function
Gruß Tino