2010-07-20 78 views
1

Excel 2003 API具有彈出「另存爲」對話框的GetSaveAsFilename方法。這需要在各種參數,包括文件類型(XLS,TXT,CSV,...),像下面這樣:在對話框的文件Excel API另存爲對話框問題

 
GetSaveAsFilename(Missing.Value, 
        "Microsoft Office Excel Workbook (*.xls), *.xls", 
        1, 
        Missing.Value, 
        Missing.Value); 

我的問題是,我只看到「的* .xls」鍵入下拉菜單。如果我跳過文件類型部分(第二個參數),我會在其中看到「所有文件(*。*)」。我怎樣才能看到包含所有可能的文件類型的正常列表,而無需創建所有文件類型的長字符串並將其傳遞給方法?

回答

3

這是link,可能會有所幫助。相關帖子是第三項。這不是一個理想的解決方案(這將是一些常量),但它可能是一種方式,讓您以編程方式遍歷可用過濾器並構建一個字符串以在GetSaveAsFilename中使用。

下面的代碼的相關部分:

Sub Main() 

'Declare a variable as a FileDialogFilters collection. 
Dim fdfs As FileDialogFilters 

'Declare a variable as a FileDialogFilter object. 
Dim fdf As FileDialogFilter 

'Set the FileDialogFilters collection variable to 
'the FileDialogFilters collection of the SaveAs dialog box. 
Set fdfs = Application.FileDialog(msoFileDialogSaveAs).Filters 

'Iterate through the description and extensions of each 
'default filter in the SaveAs dialog box. 
For Each fdf In fdfs 

    'Display the description of filters that include 
    Debug.Print fdf.Description & " (" & fdf.Extensions & ")," & fdf.Extensions 

Next fdf 

End Sub 

希望這有助於!

+0

是的,謝謝。巧合的是,我在看那個鏈接。這應該做到這一點。我仍然使用這個解決方案作爲最後的手段,因爲它基本上是一種解決方法。謝謝。 – aliensurfer 2010-07-20 18:14:46

+0

沒問題。這太糟糕了,沒有一個恆定的。看起來像很多浪費的打字。祝你好運! – 2010-07-20 18:27:31

1

你想要Application.Dialogs(xlDialogSaveAs).Show

Sub showdialog() 
    Dim name as string 
    name = "test" 
    Application.Dialogs(xlDialogSaveAs).Show name 'drop the name parameter 
                'if you don't want to provide 
                'a default name 
End Sub 

這隻允許用戶保存文件。儘管如此,這將不允許您直接獲取他們選擇的名稱。它會返回一個布爾值,這意味着他們點擊OK。因爲它是保存對話框,所以你可以看到他們是否點擊OK,然後檢查當前的文件名。這基本上會告訴你他們將文件保存爲。