Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > Beginning VB 6
|
Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning VB 6 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
 
Old March 25th, 2008, 07:16 AM
Authorized User
 
Join Date: Aug 2007
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default Error on trying to open passworded files

I have written a macro to process files emailed in to our office. The macro works well except when someone sends in a file which requires a password for it to open. Such files cause an error.
I am not interested in processing these passworded files as users should not be protecting their returns. I would rather simply ignore them.
Is there a way to test whether files are password protected against opening so that I can ignore these files?

 
Old March 25th, 2008, 09:52 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

The approach should be to add an error handler that has code to handle this particular error.
You add to the routine that opens the file, at the top, On Error GoTo Er. (That "Er" is a label in the same routine. You can call the label whatever you like. I like Er...)

Then at the botom of your code, add an exit that will cause program flow to exit the routine prior to the label Er when there is no error. You can use this exit point when there is an error if you add a lable just before the exit point. (Again, whatever you like, but I like Rs for "resume" point.)

Then, after the label in the On Error statement, handle the error. Let's presume the error is 15. THen the code would be something like:
Code:
Public Sub Whatchamo()

    On Error GoTo Er

    ' Your code here that might raise the error.

Rs: ' Do any cleanup here, closing files, etc.
    Exit Sub

Er:
    If Err.Number = 15 Then
        MsgBox "The file " & <put FilNam here, so msg is informative> & " " & _
               "could not be opened.", vbCritical, "File Open Error"
    Else
        MsgBox "Error " & Err.Description, vbCritical, "Error"
    End If

    Resume Rs

Exit Sub
 
Old March 25th, 2008, 10:32 AM
Authorized User
 
Join Date: Aug 2007
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Brian.
I had considered using a general error handling technique but was worried that it might inappropriately handle unexpected errors that might crop up in future. Hence, I was looking for some sort of specific test for passworded files.
Your suggestion of specifically addressing the error number tackles this problem so it'll work great. Just wanted to say thanks...






Similar Threads
Thread Thread Starter Forum Replies Last Post
Pdf files doesn't open Ciupaz BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 2 September 11th, 2008 04:36 PM
How to open downloaded files from local machine kumarop Pro JSP 0 April 1st, 2006 08:52 AM
Headers to Open and display files AAA PHP How-To 1 March 17th, 2004 09:31 PM
Auto Open Files Hugh Excel VBA 1 December 17th, 2003 01:00 PM





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