Results 1 to 6 of 6

Thread: [RESOLVED] open file in vb

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    32

    Resolved [RESOLVED] open file in vb

    VB Code:
    1. Sub mnuOpen_Click()
    2.     Dim f As Integer            'for file number
    3.  
    4.     'This first group of commands invokes the Open Dialog
    5.     ' OpenErr1 is invoked if user cancels from Open Dialog
    6.     On Error GoTo OpenErr1
    7.     ' and display the Open dialog (#1)
    8.     CommonDialog1.ShowOpen
    9.  
    10.     ' then get the resulting filename
    11.     FileName = CommonDialog1.FileName
    12.  
    13.     ' Now open a text file
    14.     ' Set the real error handler for file errors
    15.     On Error GoTo OpenErr2
    16.     f = FreeFile
    17.     Open FileName For Input As f
    18.     txtBox.Text = Input$(LOF(f), f) ' Use the better code from the last lesson here
    19.     Close f
    20.  
    21.     ' Nice touch: set the form's caption to contain the filename
    22.     Form1.Caption = "Text Editor : " & FileName
    23.  
    24.     ' this exits if successful open and read
    25.     Exit Sub
    26.  
    27. ' this error handler for actual file errors
    28. OpenErr2:
    29.     MsgBox "Error opening file", vbOK, "Text editor"
    30.     Close f
    31.     Exit Sub
    32.  
    33. ' this error simply exits if user Cancels from Open Dialog
    34. OpenErr1:
    35.     Exit Sub
    36.  
    37. End Sub
    i've tried to use this command to open a file (word/excel) in vb but it said "error opening file".i was hoping that i could open it in vb itself (in text box or OLE or frame) and not in word/excel so that i can edit the data in vb.
    can anyone help?

    thanks!
    Last edited by Hack; Mar 31st, 2006 at 07:27 AM. Reason: Added [vbcode] [/vbcode] tags for more clarity.

  2. #2
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Re: open file in vb

    If what you are doing is trying to open a Word or Excel doc as a text file, it won't work - the files are complex data structures. One way you could do it is use automation to invoke Word/Excel in the background and extract the data to put into your textbox. You can also open the files as binary, but without knowing the data structure of DOC or XLS, you wouldn't be able to interpret what you get. With Excel, there are a couple of ways to extract cell information from spreadsheets without having to actually open the file. Don't know if a similar capability exists for Word or not. The only way I'm aware of to read/write Excel files w/o using Excel itself is via ADO, but the information in the spreadsheet has to be arranged in database fashion.

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    32

    Re: open file in vb

    hey maybe that can help!

    can you tell me how to do the excel "thing" plz and could you tell in detail as i'm stil new to this things so i could'nt understand the complex things in vb

    thanks!

  4. #4
    Fanatic Member VBAhack's Avatar
    Join Date
    Dec 2004
    Location
    Sector 000
    Posts
    617

    Re: open file in vb

    Following is the most simple ADO example I can think of. It reads data from an Excel file and puts it into an array. The data must be db fashion - data in each column must contain the same type data (string, number, etc.) but each column can be a different type of data. Exception: the columns can have text headers, which are ignored (unless all columns have text). Good luck.

    If you want to know more, the following links might be helpful:

    http://support.microsoft.com/default...b;en-us;278973
    http://www.developerkb.com/modules/w...hp?category=23
    http://www.w3schools.com/ado/default.asp
    http://www.firstsql.com/tutor2.htm

    VB Code:
    1. Sub ReadExcelData()
    2.     'requires a reference to the Microsoft ActiveX Data Objects library
    3.     Dim cn As ADODB.Connection
    4.     Dim rs As ADODB.Recordset
    5.     Dim cnString As String
    6.     Dim strFileName As String
    7.     Dim strDataSource As String
    8.     Dim x As Variant
    9.  
    10.     strFileName = "F:\Stuff\TestData.xls"
    11.     cnString = "DRIVER={Microsoft Excel Driver (*.xls)};ReadOnly=1;DBQ=" & strFileName
    12.     strDataSource = "Sheet1$" 'Valid choices: "C1:C7", "Sheet1$", "Sheet1$C1:C7", "RangeName"
    13.  
    14.     Set cn = New ADODB.Connection
    15.     cn.Open cnString
    16.     Set rs = cn.Execute("[" & strDataSource & "]")
    17.     x = rs.GetRows 'from Recordset to array
    18.     PrintIt x
    19.     rs.Close
    20.     cn.Close
    21.     Set rs = Nothing
    22.     Set cn = Nothing
    23. End Sub
    24.  
    25. Sub PrintIt(xArray)
    26.     Dim i As Integer
    27.     Dim j As Integer
    28.     Dim strD As String
    29.    
    30.     For i = LBound(xArray, 2) To UBound(xArray, 2)
    31.         For j = LBound(xArray, 1) To UBound(xArray, 1)
    32.             strD = strD & "  " & xArray(j, i)
    33.         Next j
    34.         Debug.Print strD
    35.         strD = ""
    36.     Next i
    37. End Sub
    Last edited by VBAhack; Apr 2nd, 2006 at 01:03 PM.

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: open file in vb

    As an alternative you can use Automation, see the Excel link in my signature for a tutorial.

  6. #6

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    32

    Re: open file in vb

    thanks a lot guys!i think that settles it

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width