P
prs16001
I'm trying to open and excel file from word to put text from word into the
excel file. I cannot even get the excel file to open using the code from
http://word.mvps.org/faqs/interdev/ControlXLFromWord.htm
I have my code practically exact. At this point, I'm just trying to open
the file and then close it, so the code is stripped down and does nothing
with the file. I can't even open the file and get the error 91. I couldn't
find what I'm doing wrong on the site. Initially, I want to count the number
of rows in the excel file and in the past have been using
ActiveCell.CurrentRegion.Select
rcount = selection.rows.count
Here is the stripped down code.
sub get_excel_file()
Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim ExcelWasNotRunning As Boolean
Dim excel_file As String
Dim excel_file_path As String
excel_file_path = "C:\Documents and Settings\"
excel_file = excel_file_path & "test.xls"
On Error Resume Next
Set oXL = GetObject(, "Excel.Application")
If Err Then
ExcelWasNotRunning = True
Set oXL = New Excel.Application
oXL.Visible = True
End If
On Error GoTo Err_Handler
oWB = oXL.Workbooks.Open(FileName:=excel_file)
If ExcelWasNotRunning Then
oXL.Quit
End If
Set oSheet = Nothing
Set oWB = Nothing
Set oXL = Nothing
Err_Handler:
MsgBox excel_file & " caused a problem. " & Err.Description, vbCritical,
"Error:" & Err.Number
If ExcelWasNotRunning Then
oXL.Quit
End If
End Sub
excel file. I cannot even get the excel file to open using the code from
http://word.mvps.org/faqs/interdev/ControlXLFromWord.htm
I have my code practically exact. At this point, I'm just trying to open
the file and then close it, so the code is stripped down and does nothing
with the file. I can't even open the file and get the error 91. I couldn't
find what I'm doing wrong on the site. Initially, I want to count the number
of rows in the excel file and in the past have been using
ActiveCell.CurrentRegion.Select
rcount = selection.rows.count
Here is the stripped down code.
sub get_excel_file()
Dim oXL As Excel.Application
Dim oWB As Excel.Workbook
Dim oSheet As Excel.Worksheet
Dim ExcelWasNotRunning As Boolean
Dim excel_file As String
Dim excel_file_path As String
excel_file_path = "C:\Documents and Settings\"
excel_file = excel_file_path & "test.xls"
On Error Resume Next
Set oXL = GetObject(, "Excel.Application")
If Err Then
ExcelWasNotRunning = True
Set oXL = New Excel.Application
oXL.Visible = True
End If
On Error GoTo Err_Handler
oWB = oXL.Workbooks.Open(FileName:=excel_file)
If ExcelWasNotRunning Then
oXL.Quit
End If
Set oSheet = Nothing
Set oWB = Nothing
Set oXL = Nothing
Err_Handler:
MsgBox excel_file & " caused a problem. " & Err.Description, vbCritical,
"Error:" & Err.Number
If ExcelWasNotRunning Then
oXL.Quit
End If
End Sub