DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 13 of 13

Thread: Using the Open-files statement problem

  1. #1
    Join Date
    Nov 2004
    Posts
    293

    Using the Open-files statement problem

    I am using the open file statement in my VB6 program so that I can see if a file exists or not. If the file opens then it exists, if not then it generates an error and I handle it accordingly. My problem is the file I am trying to open is a .PDF and when I use this open file statement it damages the file. What I mean by damages the file is that after running my program I go and just manualy open the file and I see this message "Adobe Reader could not open "xxxx".pdf because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded)." and all of a sudden the file size is 0 kB.

    All I want to do is verify that some PDF files are there or not.

  2. #2
    Join Date
    Mar 2009
    Location
    Italy - Breganze (VI)
    Posts
    120

  3. #3
    Join Date
    Nov 2004
    Posts
    293
    I simple use the comamd:
    Open sc For Output As #1
    where sc has the path to the file. I don't know how to open in read only.

  4. #4
    Join Date
    Nov 2003
    Location
    Alameda, CA
    Posts
    1,737
    Open filename For Input As #1
    "There are two ways to write error-free programs. Only the third one works."
    Unknown

  5. #5
    Join Date
    Nov 2004
    Posts
    293
    That works great now. Thanks for the help.

  6. #6
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    Quote Originally Posted by AM003295 View Post
    I am using the open file statement in my VB6 program so that I can see if a file exists or not. If the file opens then it exists, if not then it generates an error and I handle it accordingly
    If all you want to do is see if it exists, why are you trying to open it? Just check to see if it is there
    Code:
    Private Sub Command1_Click()
    If Dir$("c:\myfile.pdf") <> vbNullString Then
       MsgBox "file exists"
    Else
       MsgBox "file doesn't exist"
    End If
    End Sub
    And, if for some reason you actually do want to open a PDF file, I would suggestion using the ShellExecute API rather than the "Open" method.

  7. #7
    Join Date
    Nov 2004
    Posts
    293
    Boy that makes much better sence then to try and open something and depend on an error or no error to decide.

    Thanks, I'll use it.

  8. #8
    Join Date
    Nov 2004
    Posts
    293
    Hack,
    In the above I was looking to see if a file exists or not. Is there also a way to see the size of the file if it does exist? Some of my files that do exist are damaged and even though the file exists it has a 0 size if it is damaged, so I would like to check for that also.

  9. #9
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    Sure...
    Code:
    Private Sub Command1_Click()
    Dim MySize As Long
    If Dir$("c:\myfile.pdf") <> vbNullString Then
       MySize = FileLen("c:\myfile.pdf")
       MsgBox "File exists and is " & MySize & " bytes large"
    Else
       MsgBox "file doesn't exist"
    End If
    End Sub

  10. #10
    Join Date
    Nov 2004
    Posts
    293
    That work so slick !

    Much thanks.

  11. #11
    Join Date
    Nov 2003
    Location
    Alameda, CA
    Posts
    1,737
    In fact, you can use only the filelen function to find out both if the file exists and to get its size:

    Code:
    Public Property Get FileExists(filePath As String, ByRef fLen As Long) As Boolean
        On Error Resume Next
        fLen = fileLen(filePath)
        FileExists = (Err.Number = 0)
    End Property
    "There are two ways to write error-free programs. Only the third one works."
    Unknown

  12. #12
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,663
    That is pretty slick as well.

  13. #13
    Join Date
    Nov 2004
    Posts
    293
    Wow, you guys are impressive ! I love this stuff.

Similar Threads

  1. Switch Statement..return problem
    By XanFox in forum Java
    Replies: 3
    Last Post: 04-14-2011, 09:03 PM
  2. open multiple files at the same time
    By WXY595 in forum C++
    Replies: 2
    Last Post: 05-07-2007, 04:07 AM
  3. Problem with SQL 'LIKE' Statement
    By silencer01 in forum .NET
    Replies: 1
    Last Post: 08-11-2005, 09:00 AM
  4. use a vb form to open txt files
    By LABYRNTH in forum VB Classic
    Replies: 5
    Last Post: 03-14-2005, 01:01 AM
  5. Replies: 1
    Last Post: 04-09-2001, 08:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center