2010-05-26 1 views
0

Delmia 용 VBA에서 매크로를 만들려고합니다. 그 매크로는 파일을 열어야하고 FileBrowserDialog를 사용하여이 파일의 경로를 찾고 싶습니다. 사이트에서 찾은이 코드를 사용하지만 Excel을 사용한 예입니다. In this code work in excel 매우 잘 작동합니다.VBA 매크로를 사용하여 파일 브라우저 대화 상자를 열 때 문제가 발생했습니다.

Delmia의 문제는 때때로 대화 상자가 나타나지 않고 때로는 매우 잘 나타나는 경우가 있습니다.

문제를 재현하려면 "StartIt()"함수를 호출하는 명령 단추를 만들 수 있습니다. 그 예에서는 파일의 경로를 "tbFileName"이라는 텍스트 상자에 썼습니다.

도움 주셔서 감사합니다.

Option Explicit 

Type thOPENFILENAME 
    lStructSize As Long 
    hwndOwner As Long 
    hInstance As Long 
    strFilter As String 
    strCustomFilter As String 
    nMaxCustFilter As String 
    nFilterIndex As Long 
    strFile As String 
    nMaxFile As Long 
    strFileTitle As String 
    nMaxFileTitle As Long 
    strInitialDir As String 
    strTitle As String 
    Flags As Long 
    nFileOffset As Integer 
    nFileExtension As Integer 
    strDefExt As String 
    lCustData As Long 
    lpfnHook As Long 
    lpTemplateName As String 
End Type 

Declare Function th_apiGetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (OFN As thOPENFILENAME) As Boolean 
Declare Function th_apiGetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (OFN As thOPENFILENAME) As Boolean 
Declare Function CommDlgExtendetError Lib "commdlg32.dll"() As Long 

Private Const thOFN_READONLY = &H1 
Private Const thOFN_OVERWRITEPROMPT = &H2 
Private Const thOFN_HIDEREADONLY = &H4 
Private Const thOFN_NOCHANGEDIR = &H8 
Private Const thOFN_SHOWHELP = &H10 
Private Const thOFN_NOVALIDATE = &H100 
Private Const thOFN_ALLOWMULTISELECT = &H200 
Private Const thOFN_EXTENSIONDIFFERENT = &H400 
Private Const thOFN_PATHMUSTEXIST = &H800 
Private Const thOFN_FILEMUSTEXIST = &H1000 
Private Const thOFN_CREATEPROMPT = &H2000 
Private Const thOFN_SHAREWARE = &H4000 
Private Const thOFN_NOREADONLYRETURN = &H8000 
Private Const thOFN_NOTESTFILECREATE = &H10000 
Private Const thOFN_NONETWORKBUTTON = &H20000 
Private Const thOFN_NOLONGGAMES = &H40000 
Private Const thOFN_EXPLORER = &H80000 
Private Const thOFN_NODEREFERENCELINKS = &H100000 
Private Const thOFN_LONGNAMES = &H200000 

Function StartIt() 
    Dim strFilter As String 
    Dim lngFlags As Long 
    strFilter = thAddFilterItem(strFilter, "Excel Files (*.xls)", "*.XLS") 
    strFilter = thAddFilterItem(strFilter, "Text Files(*.txt)", "*.TXT") 
    strFilter = thAddFilterItem(strFilter, "All Files (*.*)", "*.*") 
    frmFileManipulation.tbFileName.Value = thCommonFileOpenSave(InitialDir:="C:\Windows", Filter:=strFilter, FilterIndex:=3, Flags:=lngFlags, DialogTitle:="File Browser") 
    Debug.Print Hex(lngFlags) 
End Function 

Function GetOpenFile(Optional varDirectory As Variant, Optional varTitleForDialog As Variant) As Variant 
    Dim strFilter As String 
    Dim lngFlags As Long 
    Dim varFileName As Variant 
    lngFlags = thOFN_FILEMUSTEXIST Or thOFN_HIDEREADONLY Or thOFN_NOCHANGEDIR 

    If IsMissing(varDirectory) Then varDirectory = "" 

    If IsMissing(varTitleForDialog) Then varTitleForDialog = "" 

    strFilter = thAddFilterItem(strFilter, "Excel (*.xls)", "*.XLS") 
    varFileName = thCommonFileOpenSave(OpenFile:=True, InitialDir:=varDirectory, Filter:=strFilter, Flags:=lngFlags, DialogTitle:=varTitleForDialog) 

    If Not IsNull(varFileName) Then varFileName = TrimNull(varFileName) 

    GetOpenFile = varFileName 

End Function 

Function thCommonFileOpenSave(Optional ByRef Flags As Variant, Optional ByVal InitialDir As Variant, Optional ByVal Filter As Variant, _ 
           Optional ByVal FilterIndex As Variant, Optional ByVal DefaultEx As Variant, Optional ByVal fileName As Variant, _ 
           Optional ByVal DialogTitle As Variant, Optional ByVal hwnd As Variant, Optional ByVal OpenFile As Variant) As Variant 

    Dim OFN As thOPENFILENAME 
    Dim strFileName As String 
    Dim FileTitle As String 
    Dim fResult As Boolean 

    If IsMissing(InitialDir) Then InitialDir = CurDir 
    If IsMissing(Filter) Then Filter = "" 
    If IsMissing(FilterIndex) Then FilterIndex = 1 
    If IsMissing(Flags) Then Flags = 0& 
    If IsMissing(DefaultEx) Then DefaultEx = "" 
    If IsMissing(fileName) Then fileName = "" 
    If IsMissing(DialogTitle) Then DialogTitle = "" 
    If IsMissing(hwnd) Then hwnd = 0 
    If IsMissing(OpenFile) Then OpenFile = True 

    strFileName = Left(fileName & String(256, 0), 256) 
    FileTitle = String(256, 0) 

    With OFN 
     .lStructSize = Len(OFN) 
     .hwndOwner = hwnd 
     .strFilter = Filter 
     .nFilterIndex = FilterIndex 
     .strFile = strFileName 
     .nMaxFile = Len(strFileName) 
     .strFileTitle = FileTitle 
     .nMaxFileTitle = Len(FileTitle) 
     .strTitle = DialogTitle 
     .Flags = Flags 
     .strDefExt = DefaultEx 
     .strInitialDir = InitialDir 
     .hInstance = 0 
     .lpfnHook = 0 
     .strCustomFilter = String(255, 0) 
     .nMaxCustFilter = 255 
    End With 

    If OpenFile Then fResult = th_apiGetOpenFileName(OFN) Else fResult = th_apiGetSaveFileName(OFN) 


    If fResult Then 
     If Not IsMissing(Flags) Then Flags = OFN.Flags 
     thCommonFileOpenSave = TrimNull(OFN.strFile) 
     Else 
     thCommonFileOpenSave = vbNullString 
    End If 

End Function 

Function thAddFilterItem(strFilter As String, strDescription As String, Optional varItem As Variant) As String 

    If IsMissing(varItem) Then varItem = "*.*" 
    thAddFilterItem = strFilter & strDescription & vbNullChar & varItem & vbNullChar 

End Function 

Private Function TrimNull(ByVal strItem As String) As String 
    Dim intPos As Integer 
    intPos = InStr(strItem, vbNullChar) 
    If intPos > 0 Then 
     TrimNull = Left(strItem, intPos - 1) 
     Else 
     TrimNull = strItem 
    End If 

End Function 

답변

0

좋은 공용 대화 상자 COM 클래스를 사용할 수 없습니까?


Set cdlg = CreateObject("MSComDlg.CommonDialog") 
... 
cdlg.ShowOpen 
+0

고마워요,이 클래스 작업을 잘 !!!! 하지만 인터넷에서 수업을 찾기 위해 많은 연구를하고 그걸 결코 찾지 못합니다. 또한 VB 개발 환경에서 도움이되지 않습니다. 고맙습니다. 다른 클래스를 어디에서 찾을 수 있는지 말해 주시면 아주 좋습니다. – user90714

0

Excel에서이 작업을 수행하는 경우 기본 제공 GetOpenFilename 또는 GetSaveFilename 기능을 사용할 수 있습니다. Office 향상된 대화 상자를 표시합니다.

sFile = Application.GetOpenFilename("Excel Files,*.xls;*.xlsx", 1, "Please Select your File", "Select", False) 
관련 문제