Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > .NET 2.0 and Visual Studio. 2005 > Visual Studio 2005
| Search | Today's Posts | Mark Forums Read
Visual Studio 2005 For discussing Visual Studio 2005. Please post code questions about a specific language (C#, VB, ASP.NET, etc) in the correct language forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Studio 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old July 28th, 2008, 01:32 AM
Registered User
 
Join Date: Jun 2008
Location: , , .
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default [VB2005]Import textfiles to Excel using QueryTable

Hi,

it was the first time I did regarding excel.QueryTable in VB 2005 .

What I am trying to do is to import data from many textfiles to worksheets in ONE workbook.



But there's seem to be some trouble opening.

Code:
 For n = 0 To ListBox1.Items.Count - 1

            selectedfile = ListBox1.Items(n).ToString
            fullpath = System.IO.Path.GetFullPath(selectedfile)
 Dim FileNameOnly1 As String = System.IO.Path.GetFileNameWithoutExtension(fullpath)

Dim j As Integer = 0
            For Each WSheet In WBook.Sheets.Add(selectedfile)

                j += 1
                If j = 1 Then

                    With WSheet.QueryTables.Add(Connection:="TEXT;" & fullpath _
& ".txt", Destination:=range("A1"))


                        .Name = FileNameOnly1
                        .FieldNames = True
                        .RowNumbers = False
                        .FillAdjacentFormulas = False
                        .PreserveFormatting = True
                        .RefreshOnFileOpen = False
                        .RefreshStyle = XlCellInsertionMode.xlInsertDeleteCells
                        .SavePassword = False
                        .SaveData = True
                        .AdjustColumnWidth = True
                        .RefreshPeriod = 0
                        .TextFilePromptOnRefresh = False
                        .TextFilePlatform = 437
                        .TextFileStartRow = 1
                        .TextFileParseType = XlTextParsingType.xlDelimited
                        .TextFileTextQualifier = _
XlTextQualifier.xlTextQualifierDoubleQuote
                        .TextFileConsecutiveDelimiter = False
                        .TextFileTabDelimiter = True
                        .TextFileSemicolonDelimiter = False
                        .TextFileCommaDelimiter = False
                        .TextFileSpaceDelimiter = False
                        .TextFileColumnDataTypes = .Array(1, 1, 1, 1, 1, 1, 1, 1)
                        .TextFileTrailingMinusNumbers = True
                        .Refresh(BackgroundQuery:=False)

                    End With

                    Continue For
                Else
                    WSheet.Delete()
                End If
            Next

        Next n


I have change from this:
Code:
'With WSheet.QueryTables.Add_(Connection:= "TEXT;D:\project1_
\Elysium\code and excel\spreadsheet\10 June 2008.txt",Destination:=range_
("A1"))
to this:

Code:
 With WSheet.QueryTables.Add(Connection:="TEXT;" & fullpath 
_& ".txt", Destination:=range("A1"))

The error point to this line:

Code:
For Each WSheet In WBook.Sheets.Add(selectedfile)


Sorry, can anyone help?

thanks :)



Similar Threads
Thread Thread Starter Forum Replies Last Post
[VB2005]Finding last cell of the column in excel bellaelysium Visual Basic 2005 Basics 2 June 22nd, 2008 11:33 PM
Refresh Excel QueryTable using VBA buefordTJ Excel VBA 0 October 5th, 2006 03:40 PM
multiple textfiles to one textfile for import Marleen Access VBA 5 September 28th, 2006 09:03 AM
Add and Refresh Excel QueryTable from VBA DaveJoyG Excel VBA 2 September 28th, 2006 07:23 AM
images and Textfiles are on server alyeng2000 SQL Server 2000 7 May 12th, 2004 08:48 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.