GetObject Function On Text Files
Hi,
The openText creates a new book and add it to the Workbooks collection. That
is, the index of this new book in the Workbooks collection is now equals to
Workbooks.Count. Try something like:
Dim MyFirstWorkbook As Workbok '<--- workbook instead of object
On Error Resume Next
Workbooks.OpenText ...... '<<<< change here
If Err<0 Then 'case Open failed
msgbox "Cannot open file
Set MyFirstWorkbook = Nothing
Else 'case Open succes
Set MyFirstWorkbook = Workbooks(Workbooks.Count)
End if
On Error Goto 0
--
Regards,
Sébastien
"William Horton" wrote:
I have the below portion of code in a program that collects a bunch of text
files and imports them into multiple worksheets in one Excel Workbook.
Dim InputFiles As Variant
Dim MyFirstWorkbook As Object
InputFiles = Application _
.GetOpenFilename("Text Files (*.txt), *.txt", , "*** PLEASE SELECT
INPUT FILES ***", , True)
If IsArray(InputFiles) < False Then
Lower = LBound(InputFiles)
Upper = UBound(InputFiles)
Set MyFirstWorkbook = GetObject(InputFiles(1))
The code line "Set MyFirstWorkbook = GetObject(InputFiles(1))" produces the
error "File name or class name not found during Automation operation." I
realize that I must have to add some sort of class argument to the GetObject
function but do not know what. When I try the function with
GetObject(InputFiles(1), "Excel.Workbook") I get the error "ActiveXcomponent
can't create object." All the input files will be text (.txt) files. What I
need to know is how to adjust the GetObjectFunction class argument (or
something else) to get this to work properly. Can anyone help???
Further on in my code after I have opened a text (.txt) file in Excel using
the Workbooks.OpenText method. There I can successfully use the GetObject
function. I guess once the file is opened in Excel you are able to do this.
The code I am using here is:
Dim MyWorkbook As Workbook
Set MyWorkbook = GetObject(InputFiles(InputFileNo))
However, the text file is already opened as an Excel file when I do this.
That is not the case when I am trying to set the MyFIRSTWorkbook object.
Any help would be greatly appreciated.
Thanks,
Bill Horton
|