Hi All,
I have an application on MsAccess2003, generate all sort of reports on Excel
I've got one of the code to change the excel file to read only using the following code its working file
Problem , Now I want to make it Password protected , I 've tried following code but not much help.
1st Try:
Generate error
?err.description
Object variable or With block variable not set
2nd Try
The above code it bit may confuse you , so I've tried two ways one with "ActiveWorkbook" but it generate error :"Object variable or With block variable not set" so I've declare Object/Workbook variable "wbf" but some time its work but the file not password protected and some time its genearte the error:
Error = "Subscript out of range”
I hope make sense
Many thanks for your help in advance
Kind regards
Farhan
I have an application on MsAccess2003, generate all sort of reports on Excel
I've got one of the code to change the excel file to read only using the following code its working file
Code:
Case "XLS"
bEvent = CreateXLSReport(RunningTask)
If Len(rs.Fields("UserDirectory").Value) > 0 Then
'Assign Source and Dsticnation Filename to the vairables
strSourceFile = "" & RunningTask.DestDir & RunningTask.DestinationFilename & ""
strDestFile = "" & rs.Fields("UserDirectory").Value & "\" & RunningTask.DestinationFilename & ""
' Set the values into FS copy variable
Set fs = CreateObject("Scripting.FileSystemObject")
'fs.CopyFile """" & RunningTask.DestDir & "\" & RunningTask.DestinationFilename & """", """" & rs.Fields("UserDirectory").Value & "\" & RunningTask.DestinationFilename & """"
fs.CopyFile strSourceFile, strDestFile
If rs.Fields("ReadStatus").Value <> 0 Then
SetAttr strDestFile, vbReadOnly 'Change the output file to Readonly
End If
Set fs = Nothing
End If
1st Try:
Code:
ActiveWorkbook(strDestFile).ChangeFileAccess WritePassword:="admin" '14/08/2012
Generate error
?err.description
Object variable or With block variable not set
2nd Try
Code:
fs.CopyFile strSourceFile, strDestFile
If rs.Fields("ReadStatus").Value <> 0 Then
'SetAttr strDestFile, vbReadOnly
'Set wbf = Workbooks.Open(strDestFile)
Set wbf = Workbooks(strDestFile)
'wbf.WritePassword = "admin" '14/08/2012
ActiveWorkbook(strDestFile).ChangeFileAccess WritePassword:="admin" '14/08/2012
'FileFormat:=xlNormal, Password:="xxxxx", WriteResPassword:="", _
'ReadOnlyRecommended:=False, CreateBackup:=False
wbf.Save
wbf.Close
Error = "Subscript out of range”
I hope make sense
Many thanks for your help in advance
Kind regards
Farhan