prosource

파일 경로 가져오기(폴더로 종료)

probook 2023. 9. 20. 20:24
반응형

파일 경로 가져오기(폴더로 종료)

나는 사용자가 버튼을 클릭하여 열 특정 파일로 이동하도록 하는 방법을 알고 있습니다.

코드:

Private Sub CommandButton2_Click()
    Dim vaFiles As Variant

    vaFiles = Application.GetOpenFilename()

    ActiveSheet.Range("B9") = vaFiles
End Sub

사용자가 폴더로 이동하여 저장할 수 있는 두 번째 버튼을 원합니다..pdf내 프로그램이 만든 파일.

문제: 더GetOpenFilename파일을 클릭해야 합니다.폴더에 파일이 없으면 사용자가 할 수 있는 일이 없습니다.

사용.Application.FileDialog물건

Sub SelectFolder()
    Dim diaFolder As FileDialog
    Dim selected As Boolean

    ' Open the file dialog
    Set diaFolder = Application.FileDialog(msoFileDialogFolderPicker)
    diaFolder.AllowMultiSelect = False
    selected = diaFolder.Show

    If selected Then
        MsgBox diaFolder.SelectedItems(1)
    End If

    Set diaFolder = Nothing
End Sub

사용자가 폴더를 선택하는 대신 취소 버튼을 누를 경우 ErrorHandler를 추가했습니다.따라서 끔찍한 오류 메시지를 받는 대신 폴더를 선택해야 한다는 메시지가 표시되고 루틴이 종료됩니다.아래 코드는 폴더 경로를 범위 이름으로 저장합니다(시트의 셀 A1과 연결됩니다).

Sub SelectFolder()

Dim diaFolder As FileDialog

'Open the file dialog
On Error GoTo ErrorHandler
Set diaFolder = Application.FileDialog(msoFileDialogFolderPicker)
diaFolder.AllowMultiSelect = False
diaFolder.Title = "Select a folder then hit OK"
diaFolder.Show
Range("IC_Files_Path").Value = diaFolder.SelectedItems(1)
Set diaFolder = Nothing
Exit Sub

ErrorHandler:
Msg = "No folder selected, you must select a folder for program to run"
Style = vbError
Title = "Need to Select Folder"
Response = MsgBox(Msg, Style, Title)

End Sub

VBA 편집기 도구 메뉴에서 참조...을 클릭합니다."Microsoft Shell Controls And Automation"(마이크로소프트 셸 제어 및 자동화)으로 스크롤하여 선택합니다.

Sub FolderSelection()
    Dim MyPath As String
    MyPath = SelectFolder("Select Folder", "")
    If Len(MyPath) Then
        MsgBox MyPath
    Else
        MsgBox "Cancel was pressed"
    End If
End Sub

'Both arguements are optional. The first is the dialog caption and
'the second is is to specify the top-most visible folder in the
'hierarchy. The default is "My Computer."

Function SelectFolder(Optional Title As String, Optional TopFolder _
                         As String) As String
    Dim objShell As New Shell32.Shell
    Dim objFolder As Shell32.Folder

'If you use 16384 instead of 1 on the next line,
'files are also displayed
    Set objFolder = objShell.BrowseForFolder _
                            (0, Title, 1, TopFolder)
    If Not objFolder Is Nothing Then
        SelectFolder = objFolder.Items.Item.Path
    End If
End Function

소스 링크.

이를 통해 도움이 될 수 있습니다.

Sub SelectFolder()
    Dim diaFolder As FileDialog
    Dim Fname As String

    Set diaFolder = Application.FileDialog(msoFileDialogFolderPicker)
    diaFolder.AllowMultiSelect = False
    diaFolder.Show

    Fname = diaFolder.SelectedItems(1)

    ActiveSheet.Range("B9") = Fname

End Sub

기본적으로 폴더를 찾아보려는 경우:예를 들어 "D:\Default_Folder"는 "InitialFileName" 속성만 초기화합니다.

Dim diaFolder As FileDialog

' Open the file dialog
Set diaFolder = Application.FileDialog(msoFileDialogFolderPicker)
diaFolder.AllowMultiSelect = False
diaFolder.InitialFileName = "D:\Default_Folder"
diaFolder.Show

사용하다Application.GetSaveAsFilename()당신이 사용했던 것과 같은 방식으로Application.GetOpenFilename()

언급URL : https://stackoverflow.com/questions/5971292/get-file-path-ends-with-folder

반응형