newprogamer
Programmer
Hello Everyone, I have written code to open an excel file or either a word file depending on the option button the user selects. This code will open the word file. But, the excel file will not open. The objExcelfile always equals nothing. Any help would be appreciated.
'**************
Private Sub OpenQuote()
On Error Resume Next
Dim strFile As String 'The formal word document or the excel spreadsheets
'open using with VBA
If optWordQuote.Value = True Then 'Open Word Quote
Dim objWordapp As Object 'this is Word
Dim objWordfile As Object 'this is the WordQuote
strFile = DataGrid1.Columns("WordQuote") 'full path to WordQuote
Set objWordapp = CreateObject("Word.Application")
If objWordapp Is Nothing Then '*******If Word or word file are not there close
MsgBox "Microsoft Word is not found!", vbInformation, "ERROR"
GoTo quickEnd
Else
Set objWordfile = objWordapp.documents.Open(strFile) 'open quote
objWordapp.Visible = True
Set objWordfile = Nothing 'this will close the file
Set objWordapp = Nothing
End If
Else
'open excel spreadsheet
Dim objExcelapp As Object 'this is Excel
Dim objExcelfile As Object 'this is the Excel file
strFile = DataGrid1.Columns("Name") 'full path to Excel file
Set objExcelapp = CreateObject("Excel.Application")
If objExcelapp Is Nothing Then '*******If excel or excel file are not there close
MsgBox "Microsoft Excel is not found!", vbInformation, "ERROR"
GoTo quickEnd
Else
Set objExcelfile = objExcelapp.Workbook.Open(strFile) 'open spreadsheet
objExcelapp.Visible = True
Set objExcelfile = Nothing 'this will close the file
Set objExcelapp = Nothing
End If
End If
quickEnd:
Set objWordfile = Nothing 'this will close the file
Set objWordapp = Nothing
Set objExcelfile = Nothing
Set objExcelapp = Nothing
End Sub
'**************
Private Sub OpenQuote()
On Error Resume Next
Dim strFile As String 'The formal word document or the excel spreadsheets
'open using with VBA
If optWordQuote.Value = True Then 'Open Word Quote
Dim objWordapp As Object 'this is Word
Dim objWordfile As Object 'this is the WordQuote
strFile = DataGrid1.Columns("WordQuote") 'full path to WordQuote
Set objWordapp = CreateObject("Word.Application")
If objWordapp Is Nothing Then '*******If Word or word file are not there close
MsgBox "Microsoft Word is not found!", vbInformation, "ERROR"
GoTo quickEnd
Else
Set objWordfile = objWordapp.documents.Open(strFile) 'open quote
objWordapp.Visible = True
Set objWordfile = Nothing 'this will close the file
Set objWordapp = Nothing
End If
Else
'open excel spreadsheet
Dim objExcelapp As Object 'this is Excel
Dim objExcelfile As Object 'this is the Excel file
strFile = DataGrid1.Columns("Name") 'full path to Excel file
Set objExcelapp = CreateObject("Excel.Application")
If objExcelapp Is Nothing Then '*******If excel or excel file are not there close
MsgBox "Microsoft Excel is not found!", vbInformation, "ERROR"
GoTo quickEnd
Else
Set objExcelfile = objExcelapp.Workbook.Open(strFile) 'open spreadsheet
objExcelapp.Visible = True
Set objExcelfile = Nothing 'this will close the file
Set objExcelapp = Nothing
End If
End If
quickEnd:
Set objWordfile = Nothing 'this will close the file
Set objWordapp = Nothing
Set objExcelfile = Nothing
Set objExcelapp = Nothing
End Sub