Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 86
Default Subscript Out of Range Error

I'm writing a "Reset Passwords" macro for our office manager to periodically
reset all of her protected sheets. After prompting the user for the folder
to operate on, I use the FoundFiles object for the files in the folder and
its subfolders.

To avoid triggering an error by attempting to open a file that has a
different password than the one supplied (hard-coded currently but eventually
user input), I'd like to check the current password. If it is a match, open
and reset it, otherwise, write the filename to an array to be displayed at
the end of the macro.

The Workbooks.Open line worked, but the test in the outermost If..End If
block fails with a "Subscript Out of Range" error. Can anyone tell me what
I'm doing wrong?

Thank you for all assistance.

j = 0
For i = 1 To fs.FoundFiles.Count

If Workbooks(fs.FoundFiles.Item(i)).Password = "password" Then
Workbooks.Open (fs.FoundFiles.Item(i)), , , , "MaryJane"
With ActiveWorkbook
.Password = "password"
.Save
.Close
End With
Else
' File has a different password
j = j + 1
astrCantOpen(j) = fs.FoundFiles.Item(i)
End If

Next i
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Subscript Out of Range Error

Workbooks("somethinghere.xls")
expects that somethinghere.xls is already open.

I think something like this should work:

Dim wkbk As Workbook

Set wkbk = Nothing
On Error Resume Next
Set wkbk = Workbooks.Open _
(Filename:="C:\my documents\excel\book1.xls", Password:="Password")
On Error GoTo 0

If wkbk Is Nothing Then
MsgBox "not opened"
Else
MsgBox "Either no password, or it matched--either way, do the work"
End If




Sprinks wrote:

I'm writing a "Reset Passwords" macro for our office manager to periodically
reset all of her protected sheets. After prompting the user for the folder
to operate on, I use the FoundFiles object for the files in the folder and
its subfolders.

To avoid triggering an error by attempting to open a file that has a
different password than the one supplied (hard-coded currently but eventually
user input), I'd like to check the current password. If it is a match, open
and reset it, otherwise, write the filename to an array to be displayed at
the end of the macro.

The Workbooks.Open line worked, but the test in the outermost If..End If
block fails with a "Subscript Out of Range" error. Can anyone tell me what
I'm doing wrong?

Thank you for all assistance.

j = 0
For i = 1 To fs.FoundFiles.Count

If Workbooks(fs.FoundFiles.Item(i)).Password = "password" Then
Workbooks.Open (fs.FoundFiles.Item(i)), , , , "MaryJane"
With ActiveWorkbook
.Password = "password"
.Save
.Close
End With
Else
' File has a different password
j = j + 1
astrCantOpen(j) = fs.FoundFiles.Item(i)
End If

Next i


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 86
Default Subscript Out of Range Error

Thanks, Dave. Works great.

Sprinks

"Dave Peterson" wrote:

Workbooks("somethinghere.xls")
expects that somethinghere.xls is already open.

I think something like this should work:

Dim wkbk As Workbook

Set wkbk = Nothing
On Error Resume Next
Set wkbk = Workbooks.Open _
(Filename:="C:\my documents\excel\book1.xls", Password:="Password")
On Error GoTo 0

If wkbk Is Nothing Then
MsgBox "not opened"
Else
MsgBox "Either no password, or it matched--either way, do the work"
End If




Sprinks wrote:

I'm writing a "Reset Passwords" macro for our office manager to periodically
reset all of her protected sheets. After prompting the user for the folder
to operate on, I use the FoundFiles object for the files in the folder and
its subfolders.

To avoid triggering an error by attempting to open a file that has a
different password than the one supplied (hard-coded currently but eventually
user input), I'd like to check the current password. If it is a match, open
and reset it, otherwise, write the filename to an array to be displayed at
the end of the macro.

The Workbooks.Open line worked, but the test in the outermost If..End If
block fails with a "Subscript Out of Range" error. Can anyone tell me what
I'm doing wrong?

Thank you for all assistance.

j = 0
For i = 1 To fs.FoundFiles.Count

If Workbooks(fs.FoundFiles.Item(i)).Password = "password" Then
Workbooks.Open (fs.FoundFiles.Item(i)), , , , "MaryJane"
With ActiveWorkbook
.Password = "password"
.Save
.Close
End With
Else
' File has a different password
j = j + 1
astrCantOpen(j) = fs.FoundFiles.Item(i)
End If

Next i


--

Dave Peterson

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Runtime Error - Subscript out of range despite On Error statement DoctorG Excel Programming 3 July 28th 06 03:56 PM
Subscript out of range error - save copy error bg18461[_16_] Excel Programming 2 June 13th 06 04:53 PM
Subscript out of range error - save copy error bg18461[_15_] Excel Programming 1 June 13th 06 04:36 PM
Subscript out of range error Vishal[_3_] Excel Programming 3 January 4th 06 11:14 PM
Type Mismatch error & subscript out of range error Jeff Wright[_2_] Excel Programming 3 May 14th 05 07:14 PM


All times are GMT +1. The time now is 09:45 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"