2009-07-07 5 views

답변

38

Renaud Bompuis의 답변에 대한 나의 의견이 엉망입니다.

실제로 런타임 바인딩을 사용할 수 있으며 11.0 개체 라이브러리에 대한 참조가 필요하지 않습니다.

다음 코드는 언급하지 않고 작동합니다

Dim f As Object 
Set f = Application.FileDialog(3) 
f.AllowMultiSelect = True 
f.Show 

MsgBox "file choosen = " & f.SelectedItems.Count 

참고 그 위의 작품을 잘 런타임도. 앨버트는 말했다 이미 무슨에

+2

+1 항상 이것이 가능하기를 바랬습니다. 늦은 바인딩 작업 msoOpenFileDialog 등 숫자 옵션을 전달합니다. 그래서 간단하지만 좋은 대답 :) –

+0

수 있지만 그렇게해서는 안됩니다. 후기 바인딩을 사용하면 어둠 속에서 코딩하고 있습니다. 참조를 추가하고 적절하게 선언하고 filedialog 객체를 설정하면 IDE에서 힌트를 표시하지만 후기 바인딩에서는 그렇지 않습니다. – Chris

+1

@Chris OTOH, 초기 바인딩을 사용하면 사용자가 참조를 추가해야 함을 의미합니다. 즉, 늦은 바인딩이 일부 응용 프로그램에 필요합니다. –

17

Access 2007에서는 Application.FileDialog 만 있으면됩니다. 샘플 말한다

' Requires reference to Microsoft Office 12.0 Object Library. ' 
Private Sub cmdFileDialog_Click() 
    Dim fDialog As Office.FileDialog 
    Dim varFile As Variant 

    ' Clear listbox contents. ' 
    Me.FileList.RowSource = "" 

    ' Set up the File Dialog. ' 
    Set fDialog = Application.FileDialog(msoFileDialogFilePicker) 

    With fDialog 

     ' Allow user to make multiple selections in dialog box ' 
     .AllowMultiSelect = True 

     ' Set the title of the dialog box. ' 
     .Title = "Please select one or more files" 

     ' Clear out the current filters, and add our own.' 
     .Filters.Clear 
     .Filters.Add "Access Databases", "*.MDB" 
     .Filters.Add "Access Projects", "*.ADP" 
     .Filters.Add "All Files", "*.*" 

     ' Show the dialog box. If the .Show method returns True, the ' 
     ' user picked at least one file. If the .Show method returns ' 
     ' False, the user clicked Cancel. ' 
     If .Show = True Then 

     'Loop through each file selected and add it to our list box. ' 
     For Each varFile In .SelectedItems 
      Me.FileList.AddItem varFile 
     Next 

     Else 
     MsgBox "You clicked Cancel in the file dialog box." 
     End If 
    End With 
End Sub 

으로, 당신이> 도구> 참조합니다 VBE IDE에서 Microsoft Access에서 12.0 개체 라이브러리에 대한 참조를 (이 있는지 확인 : 여기

는 액세스 문서의 예입니다 메뉴).

+0

실제로 지연 바인딩을 사용할 수 있으며 11.0 개체 라이브러리에 대한 참조는 필요하지 않습니다. 다음 코드는 모든 참조없이 작동합니다 희미한 객체 설정 F = Application.FileDialog (3) f.AllowMultiSelect = 진정한 f.Show 있는 MsgBox f.SelectedItems 및 "파일 = 선택이 끝난"으로 f를 .Count 위의 내용은 런타임에도 잘 작동합니다. Albert D.Kallal 에드먼턴, 앨버타 캐나다 [email protected] –

+0

예제는 여기에서 복사됩니다. http://support.microsoft.com/en-us/kb/824272 – Mike

2

추가 :

이 코드 (다양한 샘플의 매시업)은 다른 이름으로 저장 대화 상자

Function getFileName() As String 
    Dim fDialog As Object 
    Set fDialog = Application.FileDialog(msoFileDialogSaveAs) 
    Dim varFile As Variant 

    With fDialog 
     .AllowMultiSelect = False 
     .Title = "Select File Location to Export XLSx :" 
     .InitialFileName = "jeffatwood.xlsx" 

    If .Show = True Then 
     For Each varFile In .SelectedItems 
     getFileName = varFile 
     Next 
    End If 
End With 
End Function 
+1

이것은 다른 사람들이 제시 한 답변과 동일한 답변입니다. 좀 더 장황하고 더 많은 옵션을 보여 주지만 기존 답변의 반복 일뿐입니다. –

+0

@ David-W-Fenton 아니에요. 파일 이름을 반환하고 필터를 설정할 수있게 해줍니다. Office 의존성이 없으며 모든 양식이나 모듈에 직접 붙여 넣을 수 있으므로 플러그 앤 플레이 방식입니다. 이것은 더 나은 대답이고, 다른 답변들에 관해서는 당신의 비평은 문서에서 복사/붙여 넣기하는 사람들에게 더욱 적절합니다. –

0

나는 존 M은 영업 이익의에 대한 가장 좋은 대답을 가지고 동의를 할 수있는 기능을 제공합니다 문제. 생각은 명확히 밝혀지지 않았지만, 명백한 목적은 선택된 파일 이름을 얻는 것이었고 다른 대답은 카운트 또는 목록을 반환하는 것입니다. 그러나이 경우 msofiledialogfilepicker가 더 나은 옵션 일 수 있다고 덧붙입니다. 예 :

Dim f As object 
Set f = Application.FileDialog(msoFileDialogFilePicker) 
dim varfile as variant 
f.show 
with f 
    .allowmultiselect = false 
    for each varfile in .selecteditems 
     msgbox varfile 
    next varfile 
end with 

참고 : 다중 선택이 거짓이기 때문에 varfile의 값이 동일하게 유지됩니다 (단 하나의 항목이 어느 선택). 루프 밖에서 그 값을 동등한 성공으로 사용했습니다. 그러나 John M이 그랬던 것처럼 그것을하는 것이 더 나은 방법 일 것입니다. 또한 폴더 선택 도구를 사용하여 선택한 폴더를 가져올 수 있습니다. 나는 늦은 바인딩을 항상 선호하지만 개체가 기본 액세스 라이브러리에 기본이라고 생각하므로 여기서는 필요하지 않을 수도 있습니다.

1

나는 위와 비슷한 해결책을 가지고 있으며 파일 열기, 저장, 파일 선택을 위해 작동합니다. 나는 그것을 자체 모듈에 붙여 넣고 모든 Access DB에서 사용합니다. 코드에 따르면 Microsoft Office 14.0 Object Library가 필요합니다. 내가 생각하는 또 다른 옵션은 다음과 같습니다.

Public Function Select_File(InitPath, ActionType, FileType) 
    ' Requires reference to Microsoft Office 14.0 Object Library. 

    Dim fDialog As Office.FileDialog 
    Dim varFile As Variant 


    If ActionType = "FilePicker" Then 
     Set fDialog = Application.FileDialog(msoFileDialogFilePicker) 
     ' Set up the File Dialog. 
    End If 
    If ActionType = "SaveAs" Then 
     Set fDialog = Application.FileDialog(msoFileDialogSaveAs) 
    End If 
    If ActionType = "Open" Then 
     Set fDialog = Application.FileDialog(msoFileDialogOpen) 
    End If 
    With fDialog 
     .AllowMultiSelect = False 
     ' Disallow user to make multiple selections in dialog box 
     .Title = "Please specify the file to save/open..." 
     ' Set the title of the dialog box. 
     If ActionType <> "SaveAs" Then 
      .Filters.Clear 
      ' Clear out the current filters, and add our own. 
      .Filters.Add FileType, "*." & FileType 
     End If 
     .InitialFileName = InitPath 
     ' Show the dialog box. If the .Show method returns True, the 
     ' user picked a file. If the .Show method returns 
     ' False, the user clicked Cancel. 
     If .Show = True Then 
     'Loop through each file selected and add it to our list box. 
      For Each varFile In .SelectedItems 
       'return the subroutine value as the file path & name selected 
       Select_File = varFile 
      Next 
     End If 
    End With 
End Function 
관련 문제