Results 1 to 17 of 17

Thread: Subscript Out of Range?

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    42

    Exclamation Subscript Out of Range?

    Hi I am getting a runtime error: subscript out of range.
    Here is the code where the error occurs:

    Set ortsheet = xlOrt.Workbooks(1).Sheets(Productname)

    Productname is the string returned from an Input box for the sheet name. I made sure the syntax of the sheet name was correct.
    Isn't this the way to return a specific worksheet object? I only have 1 workbook opened, which has multiple worksheets.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Subscript Out of Range?

    Do a print of what the actual name may be and compare it with what your typing in.
    VB Code:
    1. Debug.Print Me.Sheets(1).Name 'Sheet1
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    42

    Re: Subscript Out of Range?

    hmm..I tried using the following code, but no debug statements come about.

    Instead of using Me.sheets(1), I used the way how I referenced it because using Me, I am assuming references the current opened workbook?

    The workbook I am referencing is an already existing workbook in another directory, which I specified and opened.
    Therefore I used:
    Debug.Print xlOrt.Workbooks(1).Worksheet(24).Name

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Subscript Out of Range?

    So your problem is solved?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Member
    Join Date
    Jul 2005
    Posts
    42

    Re: Subscript Out of Range?

    Hi Rob,

    No it's not. No debug statements are printed. Am I using the debug.print statement the wrong way? Anyways, I have included the code. Maybe you can see what is wrong.

    VB Code:
    1. answer = MsgBox("Is the ORT spread sheet in filepath: C:\Documents and Settings\340850.000\Desktop\Excel Project\ww_52?", _
    2. vbYesNo, "Get ORT Spreadsheet")
    3.  
    4. If answer = 7 Then
    5.         ORTfilepath = InputBox("Enter file path for the ORT spreadsheet:")
    6.         Productname = InputBox("Enter Product Name:")
    7. Else
    8.         ORTfilepath = "C:\Documents and Settings\340850.000\Desktop\Excel Project\ww_52"
    9.         Productname = InputBox("Enter Product Name:")
    10.     End If
    11.  
    12.     Set xlOrt = CreateObject("Excel.Application")
    13.     xlOrt.Visible = True
    14.     xlOrt.Workbooks.Open ORTfilepath
    15.     xlOrt.ReferenceStyle = xlA1
    16.    
    17.     Debug.Print xlOrt.Workbooks(1).Worksheets(1).Name
    18.     Set ortsheet = xlOrt.Workbooks(1).Worksheets(Productname)

    Again when Productname is "Alpine", an existing spreadsheet name, I get a subscript out of range error. I look at the tabs, it is sheet24. Does that mean the index is 24? I tried using xlOrt.Workbooks(1).Worksheets(24) , and I still get the error. Any help?
    Thanks in advance!

  6. #6
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Subscript Out of Range?

    Hi,

    Using Inputboxes is opening yourself up to all sorts of problems...

    You above code is neither error trapped or looped therefore if an invalid path/filename is supplied you will recieve a runtime error.

    My guess looking over your code is that ORTFilepath opens nothing as there is no filename supplied C:\Documents and Settings\340850.000\Desktop\Excel Project\ww_52, so therefore you should be hitting an error on this line

    xlOrt.Workbooks.Open ORTfilepath
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Subscript Out of Range?

    The ORTFilePath should be amended with the Productname file name that is also entered from the Inputbox. Then you will have a fully qualified filepath.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Subscript Out of Range?

    Ahh but you wont. as it seems in his code that the ProductName refers to the sheet itself...
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Subscript Out of Range?

    Ah but you will. This is what I meant.
    VB Code:
    1. xlOrt.Workbooks.Open ORTfilepath & "\" & Productname
    But filepath validation should be done before the wb is attempted to be opened.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Subscript Out of Range?

    That's if it is the Filepath.. it also has not be proceeded with the .xls

    Code Supplied shows us the the ProductName is in fact the sheet name to be activated..

    Set ortsheet = xlOrt.Workbooks(1).Worksheets(Productname)

    Mchow,

    Aside from me and RD bickering, what version of Excel are you using here.. Or what App is this VBA From.
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Subscript Out of Range?

    Who's bickering?

    Ah, I see said the blind man. Just finished my first cup of coffee. Brain starting to warm up.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  12. #12
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Subscript Out of Range?

    Any Excuse...
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Subscript Out of Range?

    I know its evening time where you are but for me over here its early morning and I only had a few hours sleep. So no not "any excuse".
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  14. #14
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Subscript Out of Range?

    Reel him in... he's biting..
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  15. #15
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Subscript Out of Range?

    But anyways..., enough Chit Chatting in this thread. Its not really the Forum for it.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  16. #16
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: Subscript Out of Range?

    True, True..

    Lets concentrate on the solution...

    If you are using Xp version of Office you can use the Application.FileDialog, setting the intial directory to your default path.. use it like so..

    VB Code:
    1. Dim strMyFile As String
    2.     Dim Fd As FileDialog
    3.     Set Fd = Application.FileDialog(msoFileDialogOpen)
    4.     With Fd
    5.         .InitialFileName = ""C:\Documents and Settings\340850.000\Desktop\Excel Project\ww_52\"
    6.         .Filters.Clear
    7.         .Filters.Add "Excel Files", "*.xls"
    8.         .Title = "Open File"
    9.         If .Show = -1 Then strMyFile = .SelectedItems.Item(1)
    10.     End With

    If you are not using this version then you would be better off using a commondialog control to allow the user to select their, file.. This way you are definately getting the correct file name..

    If not then I have a commonDialog module you can include in your project
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  17. #17
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Subscript Out of Range?

    And here is one of my modules for generating a ShowOpen and ShowSave dialog boxes. Also, it includes
    a BrowseForFolder dialog too. Very handy and light weight since its all APIs and no ocx controls.

    VB Code:
    1. 'In a standard Module (Module1.bas)
    2. Option Explicit
    3.  
    4. Public Const LF_FACESIZE = 32
    5. Public Const MAX_PATH = 260
    6.  
    7. 'ShowOpen/ShowSave flags:
    8. Public Const OFN_ALLOWMULTISELECT = &H200
    9. Public Const OFN_CREATEPROMPT = &H2000
    10. Public Const OFN_ENABLEHOOK = &H20
    11. Public Const OFN_ENABLETEMPLATE = &H40
    12. Public Const OFN_ENABLETEMPLATEHANDLE = &H80
    13. Public Const OFN_EXPLORER = &H80000
    14. Public Const OFN_EXTENSIONDIFFERENT = &H400
    15. Public Const OFN_FILEMUSTEXIST = &H1000
    16. Public Const OFN_HIDEREADONLY = &H4
    17. Public Const OFN_LONGNAMES = &H200000
    18. Public Const OFN_NOCHANGEDIR = &H8
    19. Public Const OFN_NODEREFERENCELINKS = &H100000
    20. Public Const OFN_NOLONGNAMES = &H40000
    21. Public Const OFN_NONETWORKBUTTON = &H20000
    22. Public Const OFN_NOREADONLYRETURN = &H8000
    23. Public Const OFN_NOTESTFILECREATE = &H10000
    24. Public Const OFN_NOVALIDATE = &H100
    25. Public Const OFN_OVERWRITEPROMPT = &H2
    26. Public Const OFN_PATHMUSTEXIST = &H800
    27. Public Const OFN_READONLY = &H1
    28. Public Const OFN_SHAREAWARE = &H4000
    29. Public Const OFN_SHAREFALLTHROUGH = 2
    30. Public Const OFN_SHARENOWARN = 1
    31. Public Const OFN_SHAREWARN = 0
    32. Public Const OFN_SHOWHELP = &H10
    33. Public Const OFS_MAXPATHNAME = 128
    34.  
    35. 'BrowseForFolder flags:
    36. Public Const BIF_RETURNONLYFSDIRS = &H1       ' For finding a folder to start document searching
    37. Public Const BIF_DONTGOBELOWDOMAIN = &H2      ' For starting the Find Computer
    38. Public Const BIF_STATUSTEXT = &H4
    39. Public Const BIF_RETURNFSANCESTORS = &H8
    40. Public Const BIF_BROWSEFORCOMPUTER = &H1000   ' Browsing for Computers.
    41. Public Const BIF_BROWSEFORPRINTER = &H2000    ' Browsing for Printers
    42. Public Const BIF_BROWSEINCLUDEFILES = &H4000  ' Browsing for Everything
    43.  
    44. 'Error constants
    45. Public Const CDERR_DIALOGFAILURE = &HFFFF
    46. Public Const CDERR_FINDRESFAILURE = &H6
    47. Public Const CDERR_GENERALCODES = &H0
    48. Public Const CDERR_INITIALIZATION = &H2
    49. Public Const CDERR_LOADRESFAILURE = &H7
    50. Public Const CDERR_LOADSTRFAILURE = &H5
    51. Public Const CDERR_LOCKRESFAILURE = &H8
    52. Public Const CDERR_MEMALLOCFAILURE = &H9
    53. Public Const CDERR_MEMLOCKFAILURE = &HA
    54. Public Const CDERR_NOHINSTANCE = &H4
    55. Public Const CDERR_NOHOOK = &HB
    56. Public Const CDERR_REGISTERMSGFAIL = &HC
    57. Public Const CDERR_NOTEMPLATE = &H3
    58. Public Const CDERR_STRUCTSIZE = &H1
    59.  
    60. 'Types
    61. Type POINTAPI
    62.     x As Long
    63.     y As Long
    64. End Type
    65.  
    66. Type RECT
    67.     Left As Long
    68.     Top As Long
    69.     Right As Long
    70.     Bottom As Long
    71. End Type
    72.  
    73. Type OPENFILENAME
    74.     lStructSize As Long
    75.     hwndOwner As Long
    76.     hInstance As Long
    77.     lpstrFilter As String
    78.     lpstrCustomFilter As String
    79.     nMaxCustFilter As Long
    80.     nFilterIndex As Long
    81.     lpstrFile As String
    82.     nMaxFile As Long
    83.     lpstrFileTitle As String
    84.     nMaxFileTitle As Long
    85.     lpstrInitialDir As String
    86.     lpstrTitle As String
    87.     flags As Long
    88.     nFileOffset As Integer
    89.     nFileExtension As Integer
    90.     lpstrDefExt As String
    91.     lCustData As Long
    92.     lpfnHook As Long
    93.     lpTemplateName As String
    94. End Type
    95.  
    96. Type BROWSEINFO
    97.     hwndOwner As Long
    98.     pIDLRoot As Long
    99.     pszDisplayName As Long
    100.     lpszTitle As Long
    101.     ulFlags As Long
    102.     lpfnCallback As Long
    103.     lParam As Long
    104.     iImage As Long
    105. End Type
    106.  
    107. Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal hMem As Long)
    108.  
    109. Declare Function lstrcat Lib "kernel32" Alias "lstrcatA" (ByVal _
    110. lpString1 As String, ByVal lpString2 As String) As Long
    111.  
    112. Declare Function SHBrowseForFolder Lib "shell32" (lpbi As _
    113. BROWSEINFO) As Long
    114.  
    115. Declare Function SHGetPathFromIDList Lib "shell32" (ByVal pidList _
    116. As Long, ByVal lpBuffer As String) As Long
    117.  
    118. Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" _
    119. (pOpenfilename As OPENFILENAME) As Long
    120.  
    121. Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" _
    122. (pOpenfilename As OPENFILENAME) As Long
    123.  
    124. Declare Function GetFileTitleAPI Lib "comdlg32.dll" Alias "GetFileTitleA" _
    125. (ByVal lpszFile As String, ByVal lpszTitle As String, ByVal cbBuf As Integer) As Integer
    126.  
    127. Declare Function CommDlgExtendedError Lib "comdlg32.dll" () As Long
    128.  
    129. Global OFName As OPENFILENAME
    130. Global BInfo As BROWSEINFO
    131.  
    132. Public Function ShowOpen(hwndOwner As Long, sFilter As String, sTitle As String, Optional nFlags As Long = OFN_EXPLORER) As String
    133.    
    134.     OFName.lStructSize = Len(OFName)
    135.     OFName.hwndOwner = hwndOwner
    136.     OFName.hInstance = App.hInstance
    137.     OFName.lpstrFilter = sFilter
    138.     OFName.lpstrFile = String(254, vbNullChar)
    139.     OFName.nMaxFile = 255
    140.     OFName.lpstrFileTitle = String(254, vbNullChar)
    141.     OFName.nMaxFileTitle = 255
    142.     OFName.lpstrTitle = sTitle
    143.     OFName.flags = nFlags
    144.  
    145.     If GetOpenFileName(OFName) Then
    146.         ShowOpen = StripTerminator(OFName.lpstrFile)
    147.     Else
    148.         ShowOpen = ""
    149.     End If
    150.    
    151. End Function
    152.  
    153. Public Function ShowSave(hwndOwner As Long, sFilter As String, sTitle As String, Optional nFlags As Long = OFN_EXPLORER) As String
    154.    
    155.     OFName.lStructSize = Len(OFName)
    156.     OFName.hwndOwner = hwndOwner
    157.     OFName.hInstance = App.hInstance
    158.     OFName.lpstrFilter = sFilter
    159.     OFName.lpstrFile = String(254, vbNullChar)
    160.     OFName.nMaxFile = 255
    161.     OFName.lpstrFileTitle = String(254, vbNullChar)
    162.     OFName.nMaxFileTitle = 255
    163.     OFName.lpstrTitle = sTitle
    164.     OFName.flags = nFlags
    165.  
    166.     If GetSaveFileName(OFName) Then
    167.         ShowSave = StripTerminator(OFName.lpstrFile)
    168.     Else
    169.         ShowSave = ""
    170.     End If
    171.    
    172. End Function
    173.  
    174. Public Function BrowseForFolder(hwndOwner As Long, sTitle As String) As String
    175.  
    176.     Dim iNull As Integer
    177.     Dim lpIDList As Long
    178.     Dim lResult As Long
    179.  
    180.     With BInfo
    181.         .hwndOwner = hwndOwner
    182.         .lpszTitle = lstrcat(sTitle, "")
    183.         .ulFlags = BIF_RETURNONLYFSDIRS
    184.     End With
    185.  
    186.     lpIDList = SHBrowseForFolder(BInfo)
    187.     If lpIDList Then
    188.         BrowseForFolder = String$(MAX_PATH, 0)
    189.         SHGetPathFromIDList lpIDList, BrowseForFolder
    190.         CoTaskMemFree lpIDList
    191.         BrowseForFolder = StripTerminator(BrowseForFolder)
    192.     End If
    193.    
    194. End Function
    195.  
    196. Public Function GetFileTitle(sFile As String) As String
    197.  
    198.     GetFileTitle = String(255, vbNullChar)
    199.     GetFileTitleAPI sFile, GetFileTitle, 255
    200.     GetFileTitle = StripTerminator(GetFileTitle)
    201.    
    202. End Function
    203.  
    204. Public Function GetErrorString() As String
    205.  
    206.     Select Case CommDlgExtendedError
    207.         Case CDERR_DIALOGFAILURE
    208.             GetErrorString = "The dialog box could not be created."
    209.         Case CDERR_FINDRESFAILURE
    210.             GetErrorString = "The common dialog box function failed to find a specified resource."
    211.         Case CDERR_INITIALIZATION
    212.             GetErrorString = "The common dialog box function failed during initialization."
    213.         Case CDERR_LOADRESFAILURE
    214.             GetErrorString = "The common dialog box function failed to load a specified resource."
    215.         Case CDERR_LOADSTRFAILURE
    216.             GetErrorString = "The common dialog box function failed to load a specified string."
    217.         Case CDERR_LOCKRESFAILURE
    218.             GetErrorString = "The common dialog box function failed to lock a specified resource."
    219.         Case CDERR_MEMALLOCFAILURE
    220.             GetErrorString = "The common dialog box function was unable to allocate memory for internal structures."
    221.         Case CDERR_MEMLOCKFAILURE
    222.             GetErrorString = "The common dialog box function was unable to lock the memory associated with a handle."
    223.         Case CDERR_NOHINSTANCE
    224.             GetErrorString = "The ENABLETEMPLATE flag was set in the Flags member of the initialization structure for " & _
    225.             "the corresponding common dialog box, but you failed to provide a corresponding instance handle."
    226.         Case CDERR_NOHOOK
    227.             GetErrorString = "The ENABLEHOOK flag was set in the Flags member of the initialization structure for the corresponding " & _
    228.             "common dialog box, but you failed to provide a pointer to a corresponding hook procedure."
    229.         Case CDERR_REGISTERMSGFAIL
    230.             GetErrorString = "The RegisterWindowMessage function returned an error code when it was called by the common " & _
    231.             "dialog box function."
    232.         Case CDERR_NOTEMPLATE
    233.             GetErrorString = "The ENABLETEMPLATE flag was set in the Flags member of the initialization structure for the " & _
    234.             "corresponding common dialog box, but you failed to provide a corresponding template."
    235.         Case CDERR_STRUCTSIZE
    236.             GetErrorString = "The lStructSize member of the initialization structure for the corresponding common dialog box is invalid."
    237.         Case Else
    238.             GetErrorString = "Undefined error ..."
    239.     End Select
    240.    
    241. End Function
    242.  
    243. Private Function StripTerminator(sInput As String) As String
    244.  
    245.     Dim ZeroPos As Integer
    246.    
    247.     ZeroPos = InStr(1, sInput, vbNullChar)
    248.     If ZeroPos > 0 Then
    249.         StripTerminator = Left$(sInput, ZeroPos - 1)
    250.     Else
    251.         StripTerminator = sInput
    252.     End If
    253.    
    254. End Function
    Last edited by RobDog888; Jul 31st, 2005 at 12:17 PM. Reason: Fixing wordwraps so page wont be too wide. ;)
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

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



Click Here to Expand Forum to Full Width