Microsoft Excel

Herbers Excel/VBA-Archiv

Informationen und Beispiele zum Thema MsgBox
BildScreenshot zu MsgBox MsgBox-Seite mit Beispielarbeitsmappe aufrufen

Application.FileDialog(msoFileDialogSaveAs) | Herbers Excel-Forum


Betrifft: Application.FileDialog(msoFileDialogSaveAs) von: Philipp
Geschrieben am: 03.12.2009 14:36:22

Hallo,

ich möchte nur bestimmte Dateiformate beim speichern zulassen. Die Filterfunktion funktioniert hier nicht (oder jedenfalls bei mir)

Ich arbeite übrigens in WORD und dort gibt es die Application.GetSaveAsFilename Anweisung nicht.

Sub test
    Set oFileDialog = Application.FileDialog(msoFileDialogSaveAs)
        
    With oFileDialog
        .Title = "Auto Report Save as"
        .ButtonName = "speichern"
        .InitialFileName = "AutoReport_"
        .Filters.Clear
          .Filters.Add "word-files", "*.doc"
        If .Show = -1 Then
           .Execute
           sName = .SelectedItems(1)
        Else
            Exit Sub
        End If
    
    End Sub
hat jemand von euch eine Idee. Wie ich nur ein bestimmtes Format zulassen kann?
Danke für eure Hilfe.

Phil

  

Betrifft: AW: Application.FileDialog(msoFileDialogSaveAs) von: Josef Ehrensberger
Geschrieben am: 03.12.2009 16:39:39

Hallo Phil,

ich verwende das hier.

' **********************************************************************
' Modul: Modul2 Typ: Allgemeines Modul
' **********************************************************************

Option Explicit

Private Declare Function GetSaveFileName Lib "comdlg32.dll" _
  Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) _
  As Long


Private Type OPENFILENAME
  lStructSize As Long
  hwndOwner As Long
  hInstance As Long
  lpstrFilter As String
  lpstrCustomFilter As String
  nMaxCustFilter As Long
  nFilterIndex As Long
  lpstrFile As String
  nMaxFile As Long
  lpstrFileTitle As String
  nMaxFileTitle As Long
  lpstrInitialDir As String
  lpstrTitle As String
  Flags As Long
  nFileOffset As Integer
  nFileExtension As Integer
  lpstrDefExt As String
  lCustData As Long
  lpfnHook As Long
  lpTemplateName As String
End Type

Public Const OFN_FILEMUSTEXIST As Long = &H1000&
Public Const OFN_HIDEREADONLY As Long = &H4&
Public Const OFN_PATHMUSTEXIST As Long = &H800&


Public Function ShowSave(Filter As String, Flags As Long, _
    hWnd As Long, FileName As String) As String

  
  Dim Buffer As String
  Dim Result As Long
  Dim ComDlgOpenFileName As OPENFILENAME
  
  Buffer = FileName & String$(128 - Len(FileName), 0)
  
  With ComDlgOpenFileName
    .lStructSize = Len(ComDlgOpenFileName)
    .hwndOwner = hWnd
    .Flags = OFN_HIDEREADONLY Or OFN_PATHMUSTEXIST
    .nFilterIndex = 1&
    .nMaxFile = Len(Buffer)
    .lpstrFile = Buffer
    .lpstrFilter = Filter
  End With
  
  Result = GetSaveFileName(ComDlgOpenFileName)
  
  If Result <> 0 Then
    ShowSave = Left$(ComDlgOpenFileName.lpstrFile, _
      InStr(ComDlgOpenFileName.lpstrFile, _
      Chr$(0)) - 1)
  End If
End Function

Sub Datei_Speichern()
  Dim Filter As String, FileName As String
  Dim Flags As Long, lngIndex As Long
  
  Flags = OFN_FILEMUSTEXIST Or OFN_HIDEREADONLY Or _
    OFN_PATHMUSTEXIST
  
  Filter = "Word-Dateien (*.doc*)" & Chr$(0)
  
  Filter = Filter & Chr$(0)
  
  FileName = ShowSave(Filter, Flags, Application.hWnd, "AutoReport_.doc")
  
  If FileName <> "" Then
    MsgBox FileName
  End If
  
End Sub



Gruß Sepp