Consulting

Results 1 to 9 of 9

Thread: Error: 'Cannot find project or library'. 2002/200 compatibility error?

  1. #1
    VBAX Regular
    Joined
    Jul 2008
    Posts
    47
    Location

    Error: 'Cannot find project or library'. 2002/200 compatibility error?

    Hi,

    I wrote a program for someone that worked perfectly fine on my copy of office XP (2002). When i handed the program over, it would not run on their office 2000.

    It throws out the error 'Cannot find project or library' on line:

    month1val = Application.GetOpenFilename("Excel File (*.xls), *.xls")
    Is this a 2002/2000 compatibility issue?

    Thanks
    Gary

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Yes, GetOpenFilename came in with 2002.


    You cannot use Application.GetOpenFilename, you have to use an API.
    This is much more flexible than Application.GetOpenFilename,
    including what you want to do, it also returns properties of the files.

    I use a version encapsulated in a class module, attached below. To use,
    add this code to a class module, call it clsGetOpenFileName, and invoke
    in the following way

    [vba]

    Dim cFileOpen As clsGetOpenFilename

    Set cFileOpen = New clsGetOpenFilename

    With cFileOpen
    .FileName = "Ex*.xls"
    .FileType = "Excel Files"
    .DialogTitle = "Class GetOpenFileName Demo"
    .MultiFile = "N"
    .SelectFile

    If .SelectedFiles.Count > 0 Then
    MsgBox (.SelectedFiles(1))
    End If
    End With

    Set cFileOpen = Nothing
    [/vba]

    Class module

    [vba]

    Option Explicit

    '-----------------------------*------------------------------*----------------
    ' Win32 API Declarations
    '-----------------------------*------------------------------*----------------
    Private Declare Function GetOpenFileName Lib "comdlg32.dll" _
    Alias "GetOpenFileNameA" _
    (pOpenfilename As OPENFILENAME) As Long


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


    Private Declare Function GetShortPathName Lib "kernel32" _
    Alias "GetShortPathNameA" _
    (ByVal lpszLongPath As String, _
    ByVal lpszShortPath As String, _
    ByVal cchBuffer As Long) As Long


    Private Type OPENFILENAME
    nStructSize As Long
    hWndOwner As Long
    hInstance As Long
    sFilter As String
    sCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    sFile As String
    nMaxFile As Long
    sFileTitle As String
    nMaxTitle As Long
    sInitialDir As String
    sDialogTitle As String
    flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    sDefFileExt As String
    nCustData As Long
    fnHook As Long
    sTemplateName As String
    End Type


    '-----------------------------*------------------------------*----------------
    ' Private Variables
    '-----------------------------*------------------------------*----------------
    Private OFN As OPENFILENAME


    Private sFileType As String 'Type of file narrative
    Private sFileName As String 'Filename string to restrict list
    Private sReadOnly As String 'Y/N flag
    Private sMultiFile As String 'Allow selection of multiple files
    Private sTitle As String 'Title in file dialog box


    '-----------------------------*------------------------------*----------------
    ' Private Constants
    '-----------------------------*------------------------------*----------------
    Private Const OFN_ALLOWMULTISELECT As Long = &H200
    Private Const OFN_CREATEPROMPT As Long = &H2000
    Private Const OFN_ENABLEHOOK As Long = &H20
    Private Const OFN_ENABLETEMPLATE As Long = &H40
    Private Const OFN_ENABLETEMPLATEHANDLE As Long = &H80
    Private Const OFN_EXPLORER As Long = &H80000
    Private Const OFN_EXTENSIONDIFFERENT As Long = &H400
    Private Const OFN_FILEMUSTEXIST As Long = &H1000
    Private Const OFN_HIDEREADONLY As Long = &H4
    Private Const OFN_LONGNAMES As Long = &H200000
    Private Const OFN_NOCHANGEDIR As Long = &H8
    Private Const OFN_NODEREFERENCELINKS As Long = &H100000
    Private Const OFN_NOLONGNAMES As Long = &H40000
    Private Const OFN_NONETWORKBUTTON As Long = &H20000
    Private Const OFN_NOREADONLYRETURN As Long = &H8000& '*see comments
    Private Const OFN_NOTESTFILECREATE As Long = &H10000
    Private Const OFN_NOVALIDATE As Long = &H100
    Private Const OFN_OVERWRITEPROMPT As Long = &H2
    Private Const OFN_PATHMUSTEXIST As Long = &H800
    Private Const OFN_READONLY As Long = &H1
    Private Const OFN_SHAREAWARE As Long = &H4000
    Private Const OFN_SHAREFALLTHROUGH As Long = 2
    Private Const OFN_SHAREWARN As Long = 0
    Private Const OFN_SHARENOWARN As Long = 1
    Private Const OFN_SHOWHELP As Long = &H10
    Private Const OFS_MAXPATHNAME As Long = 260


    'OFS_FILE_OPEN_FLAGS and OFS_FILE_SAVE_FLAGS below are mine to save long
    'statements; they're not a standard Win32 type.
    Private Const OFS_FILE_OPEN_FLAGS = OFN_EXPLORER Or _
    OFN_LONGNAMES Or _
    OFN_CREATEPROMPT Or _
    OFN_NODEREFERENCELINKS


    Private Const OFS_FILE_SAVE_FLAGS = OFN_EXPLORER Or _
    OFN_LONGNAMES Or _
    OFN_OVERWRITEPROMPT Or _
    OFN_HIDEREADONLY


    '-----------------------------*------------------------------*--
    ' Class Properties
    '-----------------------------*------------------------------*--
    Public SelectedFiles As New Collection


    Public Property Let FileType(FileType As String)
    sFileType = FileType
    End Property


    Public Property Let FileName(FileName As String)
    sFileName = FileName
    End Property


    Public Property Let MultiFile(MultiFile As String)
    sMultiFile = UCase(MultiFile)
    End Property


    Public Property Let DialogTitle(Title As String)
    sTitle = Title
    End Property


    Public Property Get ReadOnly()
    ReadOnly = sReadOnly
    End Property


    '-----------------------------*------------------------------*--
    ' Class Methods
    '-----------------------------*------------------------------*--
    Public Function SelectFile() As Long
    '-----------------------------*------------------------------*--
    Dim i
    Dim sFilters As String
    Dim sBuffer As String
    Dim sLongname As String
    Dim sShortname As String


    If ValidInput Then
    'create a string of filters for the dialog
    sFilters = sFileType & vbNullChar & vbNullChar

    With OFN

    .nStructSize = Len(OFN) 'Size of the OFN structure
    .sFilter = sFilters 'Filters for the dropdown combo
    .nFilterIndex = 1 'Index to the initial filter

    'Default filename, plus additional padding for user's
    ' final selection(s). Must be double-null terminated
    .sFile = sFileName & Space$(1024) & vbNullChar & vbNullChar

    .nMaxFile = Len(.sFile) 'the size of the buffer
    'Default if file has no extension
    .sDefFileExt = sFileName & vbNullChar & vbNullChar
    'Make space for file title if single selection made,
    ' double-null terminated, and its size
    .sFileTitle = vbNullChar & Space$(512) & _
    vbNullChar & vbNullChar
    .nMaxTitle = Len(OFN.sFileTitle)
    'Starting folder, double-null terminated
    .sInitialDir = ThisWorkbook.Path & vbNullChar

    .sDialogTitle = sTitle 'the dialog title string

    'Default open flags and multiselect
    .flags = OFS_FILE_OPEN_FLAGS Or _
    OFN_NOCHANGEDIR

    If sMultiFile = "Y" Then .flags = .flags Or _
    OFN_ALLOWMULTISELECT

    End With

    SelectFile = GetOpenFileName(OFN)
    If SelectFile Then
    'Remove trailing pair of terminating nulls and
    ' trim returned file string
    sBuffer = Trim$(Left$(OFN.sFile, Len(OFN.sFile) - 2))
    'If multiple select, first member is the path, remaining
    ' members are the files under that path
    Do While Len(sBuffer) > 3
    SelectedFiles.Add StripDelimitedItem( _
    sBuffer, vbNullChar)
    Loop


    sReadOnly = Abs((OFN.flags And OFN_READONLY))


    End If
    End If


    End Function


    Private Sub Class_Initialize()
    sTitle = "GetOpenFileName"
    End Sub


    Private Sub Class_Terminate()
    Set SelectedFiles = Nothing
    End Sub


    '-----------------------------*------------------------------*------
    Private Function ValidInput() As Boolean
    '-----------------------------*------------------------------*------
    Dim i As Long

    ValidInput = True

    i = 1
    If IsEmpty(sFileName) Then
    sFileName = " - a file description must be supplied"
    i = i + 1
    ValidInput = False
    End If

    If IsEmpty(sFileType) Then
    sFileType = " - a file extension must be supplied"
    i = i + 1
    ValidInput = False
    End If

    If sMultiFile <> "Y" And sMultiFile <> "N" Then
    sMultiFile = "Multiple files must be Y or N"
    i = i + 1
    ValidInput = False
    End If

    End Function


    '-----------------------------*------------------------------*------
    Private Function StripDelimitedItem(startStrg As String, _
    delimiter As String) As String
    '-----------------------------*------------------------------*------
    'take a string separated by nulls, split off 1 item, and shorten
    ' the string
    ' so the next item is ready for removal.
    '-----------------------------*------------------------------*------
    Dim iPos As Long

    iPos = InStr(1, startStrg, delimiter)

    If iPos Then
    StripDelimitedItem = Mid$(startStrg, 1, iPos)
    startStrg = Mid$(startStrg, iPos + 1, Len(startStrg))
    End If

    End Function


    '-----------------------------*------------------------------*------
    Private Function TrimNull(item As String) As String
    '-----------------------------*------------------------------*------
    Dim iPos As Long

    iPos = InStr(item, Chr$(0))
    If iPos Then
    TrimNull = Left$(item, iPos - 1)
    Else
    TrimNull = item
    End If

    End Function
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Tutor Mavyak's Avatar
    Joined
    Jul 2008
    Posts
    204
    Location
    I don't belive it is a compatibility issue, since it's in the Office 2000 VBA Language reference:

    http://msdn.microsoft.com/en-us/library/aa298292(office.10).aspx

    Sadly, I don't know what is causing the issue, though.


    (Edit: Anachronistic post. I defer to xld.)

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Mavyak
    I don't belive it is a compatibility issue, since it's in the Office 2000 VBA Language reference:

    http://msdn.microsoft.com/en-us/library/aa298292(office.10).aspx

    Sadly, I don't know what is causing the issue, though.


    (Edit: Anachronistic post. I defer to xld.)
    Don't, I am wrong. I thought it came in with 2002, but I just started 2000 after seeing your post and it is there in 2000 as you say.

    Therefore the problem is confusing.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5

  6. #6
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Quote Originally Posted by gscarter
    Hi,

    I wrote a program for someone that worked perfectly fine on my copy of office XP (2002). When i handed the program over, it would not run on their office 2000.

    It throws out the error 'Cannot find project or library' on line:

    month1val = Application.GetOpenFilename("Excel File (*.xls), *.xls")
    Is this a 2002/2000 compatibility issue?

    Thanks
    Gary
    Have you checked for "missing" references?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    VBAX Regular
    Joined
    Jul 2008
    Posts
    47
    Location
    Quote Originally Posted by mdmackillop
    Have you checked for "missing" references?
    Ive just checked with the user, and the only difference in references is his reference 'Microsoft Word 10.0 Object Library' is missing. I cant see as this would cause the problem im having, but i could be wrong.

    When i remove that reference from my copy, the macro still runs fine.

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    A missing reference can cause unexpected errors with no apparent link. Typically I find the Chr function causes an error. Remove the tick from any missing references.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    VBAX Regular
    Joined
    Jul 2008
    Posts
    47
    Location
    Ive tried this on another copy of office 2000 and get the same error, does this mean i cannot use:
    GetOpenFilename

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •