Hello:
I wrote a procedure to import 5 files into an Excel workbook. If the file is not found on the directory, then the an OpenFileDialog pops up to allow the user to browse for the file. If the user selects a file other than the one that is supposed to be imported, a warning pops up. Since there is a possibility that users select the wrong file more than once, I wrote a While Loop to keep checking for the right file name.
Up to there everything worked well. Here is where the issue comes in, If the file is not on the expected directory, say is on a CD, the user browses and finds the right file, and tries to open it, I still get the error message that the file is not the correct one. I figure out why, the While Loop is correct but the way I set my variables up is where the issue is.
My files are supposed to be located in the same path as my workbook so I set up my variables as follows:
Dim xlWBPath As String = Globals.ThisWorkbook.Application.ActiveWorkbook.Path
Dim strImportFile As String
strImportFile = xlWBPath & "\" & GetImportFiles(n)
But because my missing file may be on a different directory my variable
strImportFile
is no longer valid, I have to now get the new path of the selected file and change, or declare a new variable. I have tried to do that on my code, but it is not working. Below is my code, I highlighted the area with the trouble:
Dim xlDestSheet As Excel.Worksheet
Dim xlWBPath As String = Globals.ThisWorkbook.Application.ActiveWorkbook.Path
Dim strImportFile As String
Dim xlWBSource As Object = Nothing
Dim xlWBImport As Object = Nothing
For n As Long = 1 To 5 Step 1
strImportFile = xlWBPath & "\" & GetImportFiles(n)
xlDestSheet = DataSheets(n)
Dim strDestSheetName As String = xlDestSheet.Name
If Len(Dir(strImportFile)) > 0 Then
xlWBSource = Globals.ThisWorkbook.Application.ActiveWorkbook
xlWBImport = Globals.ThisWorkbook.Application.Workbooks.Open(strImportFile)
xlWBImport.Worksheets(1).Cells.Copy(xlWB.Worksheets(strDestSheetName).Range("A1"))
xlWBImport.Close()
Else
Dim msbProceed As MsgBoxResult
Dim strVmbProceedResults As String = ("Procedure Canceled. Your project will now close")
Dim strPrompt As String = " source file does not exist." & vbNewLine & _
"Press OK to browse for the file or CANCEL to quit"
msbProceed = MsgBox("The " & GetImportFiles(n) & strPrompt, MsgBoxStyle.OkCancel + MsgBoxStyle.Question, "Verify Source File")
If msbProceed = MsgBoxResult.Cancel Then
msbProceed = MsgBox(strVmbProceedResults, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical)
xlWB.Close(SaveChanges:=False)
Exit Sub
Else
Dim ofdGetOpenFileName As New OpenFileDialog()
ofdGetOpenFileName.Title = "Open File " & GetImportFiles(n)
ofdGetOpenFileName.InitialDirectory = xlWBPath
ofdGetOpenFileName.Filter = "Excel Files (*.xls;*.xlsx; *.xlsm; *.csv)| *.xls; *.csv; *.xlsx; *.xlsm"
ofdGetOpenFileName.FilterIndex = 2
ofdGetOpenFileName.RestoreDirectory = True
If ofdGetOpenFileName.ShowDialog() = System.Windows.Forms.DialogResult.Cancel Then
Dim msbContinue As MsgBoxResult
Dim strAlert As String = ("You have not selected a workbook. The project will now close without saving changes")
msbContinue = MsgBox(strAlert, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "No Workbook Selected")
xlWB.Close(SaveChanges:=False)
Exit Sub
Else
While strImportFile <> GetImportFiles(n)
Dim msbContinue As MsgBoxResult
Dim strAlert As String = ("The selected file is invalid. Please select file: " & GetImportFiles(n) & vbNewLine & _
" to continue.")
msbContinue = MsgBox(strAlert, MsgBoxStyle.RetryCancel + MsgBoxStyle.Critical, "Wrong File Selection")
If msbContinue = MsgBoxResult.Cancel Then
xlWB.Close(SaveChanges:=False)
Exit Sub
Else
ofdGetOpenFileName.ShowDialog()
Dim strGetPath As String = Nothing
strGetPath = System.IO.Path.GetDirectoryName(ofdGetOpenFileName.FileName)
strImportFile = strGetPath & "\" & GetImportFiles(n)
End If
Continue While
If strImportFile = GetImportFiles(n) Then
Exit While
strImportFile = ofdGetOpenFileName.FileName
xlWBImport = Globals.ThisWorkbook.Application.Workbooks.Open(strImportFile)
xlWBImport.Worksheets(1).Cells.Copy(xlWB.Worksheets(strDestSheetName).Range("A1"))
xlWBImport.Close()
End If
Try
xlWBSource = Globals.ThisWorkbook.Application.ActiveWorkbook
xlWBImport = Globals.ThisWorkbook.Application.Workbooks.Open(strImportFile)
xlWBImport.Worksheets(1).Cells.Copy(xlWB.Worksheets(strDestSheetName).Range("A1"))
xlWBImport.Close()
Catch
MsgBox(Err.Description, MsgBoxStyle.Critical, "Unexpected Error")
End Try
End While
End If
End If
End If
Next
End Sub