Systematically Importing Data to Excel

M

MWS

Hello, I'm trying to attach code to a command button, which allows the users
to a) select one of several Microsoft Windows 2000 Notepad files and then b)
have the data from the file imported into excel.

The code is as follows:

Private Sub CommandButton1_Click()

'launches OPEN file process
Dim fName As Variant
Dim fName1 As String
fName = Application.GetOpenFilename

If fName = False Then
MsgBox "No File Selected - Data Import Aborted"
Exit Sub
End If
fName1 = Dir(fName)
With ActiveSheet.QueryTables.Add(Connection:= _
"TEXT;" & fName, Destination:=Range("A1"))
.Name = Left(fName1, Len(fName1) - 4)

.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlFixedWidth
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1)
.TextFileFixedColumnWidths = Array(15, 10, 8, 33, 21, 31)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False

End With

End Sub

Upon execution, the code halts at the ".Refresh BackgroundQuery:=False" line
and the following error message is displayed:

Run-time error '-2147417848 (80010108)'
Automation error
The object invoked has disconnected from its clients

This is th efirst time I'm trying to automate this process and cannot
determine how to correct the problem. How can I coorect this situation? If a
more streamlined or less problematic approach exists, please share.

Any and All Help Is Appreciated - Thank You
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top