I'm a newbie in this forum and a newbie with VBA. I need some help and I don't know where else to turn. I inherited this piece of code from Word97.
The code for the most part functions, but there's an interesting quirk I can't figure out. If the user runs this macro and clicks on a file to open, the code executes perfectly. If the user presses the cancel button, then runs the macro again they receive the Run time Error 13 Data type mismatch error. I can't figure this out. Any ideas?
Thanks.
[VBA] Sub OpenFiles()
Dim cdlFileOpen As CommonDialog
Dim iPos As Integer
Dim iLastPos As Integer
Dim strFileName As String
Dim strPath As String
Dim WorkDirectoryName$
Dim IniFileName$
Dim CurDir As String
'Create common dialog box object
Set cdlFileOpen = FileOpen.CommonDialog1
cdlFileOpen.DialogTitle = "Flash Word 97: Open Work Files"
' Set CancelError is True
cdlFileOpen.CancelError = True
On Error GoTo ErrHandler
' Set flags
cdlFileOpen.flags = cdlOFNAllowMultiselect ' & cdlOFNExplorer ' & cdlOFNLongNames
' Set filters
cdlFileOpen.Filter = "All Files (*.*)|*.*|Work files (*.wrk)|*.wrk|Advance Files (*.adv)|*.adv|Rejected Files (*.rjt)|*.rjt|Done Files (*.don)|*.don|Trash Files (*.tsh)|*.tsh"
' Specify default filter (*.wrk from above list)
cdlFileOpen.FilterIndex = 2
'Make sure the file name is blank
cdlFileOpen.FileName = ""
'Set the open directory
IniFileName$ = WordBasic.[GetPrivateProfileString$]("DLS", "Shared_INI", "WIN.INI")
WorkDirectoryName$ = WordBasic.[GetPrivateProfileString$]("Global Settings", "WorkDirectory", IniFileName$)
CurDir = Options.DefaultFilePath(wdCurrentFolderPath)
If CurDir = "c:\windows\desktop" Then
cdlFileOpen.InitDir = WorkDirectoryName$
Else
cdlFileOpen.InitDir = CurDir
End If
' Display the Open dialog box
cdlFileOpen.ShowOpen
'Save path info
iLastPos = InStr(1, cdlFileOpen.FileName, " ")
If iLastPos = 0 Then
'Only one file was selected, look for slash as path terminator
Do
iLastPos = iPos
iPos = InStr(iLastPos + 1, cdlFileOpen.FileName, "\")
Loop Until iPos = 0
End If
strPath = Left(cdlFileOpen.FileName, iLastPos - 1)
'Check path for trailing slash. Results from dialog vary for some OSs.
If Right(strPath, 1) <> "\" Then
strPath = strPath & "\"
End If
Debug.Print "Path=" & strPath & "|"
iPos = InStr(iLastPos + 1, cdlFileOpen.FileName, " ")
While iPos <> 0
'Parse the name of the next file to open
strFileName = LTrim(Mid(cdlFileOpen.FileName, iLastPos + 1, iPos - iLastPos - 1))
Debug.Print " File='" & strFileName & "'"
'Open the file
Documents.Open FileName:=strPath & strFileName, ConfirmConversions:=False, _
ReadOnly:=False, AddToRecentFiles:=True, PasswordDocument:="", _
PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
WritePasswordTemplate:="", Format:=wdOpenFormatAuto
'Set current position in name string and look for next file
iLastPos = iPos
iPos = InStr(iLastPos + 1, cdlFileOpen.FileName, " ")
Wend
'Process last file
strFileName = Mid(cdlFileOpen.FileName, iLastPos + 1)
Debug.Print " File='" & strFileName & "'"
Documents.Open FileName:=strPath & strFileName, ConfirmConversions:=False, _
ReadOnly:=False, AddToRecentFiles:=True, PasswordDocument:="", _
PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
WritePasswordTemplate:="", Format:=wdOpenFormatAuto
Exit Sub
ErrHandler:
Select Case Err.Number
Case 32755
'User pressed the Cancel button, do nothing and just exit
Case Else
'Display error message
MsgBox Err.Number & ":" & Err.Description
End Select
Exit Sub
End Sub
[/VBA]