View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Peter T Peter T is offline
external usenet poster
 
Posts: 5,600
Default Protecting VBA (Excel) code

Looks like you are trying to save the workbook with the locked code as a
textfile, indeed that fails for me too.

If you just want to save a sheet as a text file do something like this
(aircode)

activesheet.copy
activeworkbook.saveas ' blah
activeworkbook.close

In effect copy the sheet to a new single sheet workbook, saveas, then close
it.

In passing, try and remove all those Selects none of which are necessary if
you reference the ranges correctly.

Regards,
Peter T


"Malc" wrote in message
...
Guys - thanks for your help, but having taken your advice I still have
the problem.
I took out the error handler because I wanted to see what the error
actually was. I had displayalerts set to false because the file I'm
creating might or might not exist and with it set like this I don't
get the overwrite? message.
I'm still mystified why code that works perfectly well as long as it
isn't protected stops working as soon as it is!
Anyway, as there's a suggestion that the problem isn't in my code,
maybe you'd like to see all of it. I should say that I'm not a
programmer and I've picked up what I know, so please don't laugh too
much at my efforts!
I also noticed that even with the project unlocked and the alerts
turned on I could overwrite the file OK but if I Cancelled or said No
I'd get the failure message.

Thanks again:

Dim DSNDirectory As String
Dim DSNFilename As String
Dim FileSaveDirectory As String
Dim FileSaveName As String
Dim PWEntry As String
Worksheets("Setup").Activate
Range("pwdentry").Select
If ActiveCell.Formula = "********" Then
Range("pwdentry").ClearContents
End If
Range("pwdentry").Select
If ActiveCell.Formula = "" Then
PWEntry = InputBox("Enter SunSystems Password")
Range("pwdentry") = PWEntry
'Exit Sub
End If
Range("pwdentry").Copy
Range("pwd").Select
ActiveSheet.Paste
Selection.NumberFormat = ";;;"
With Selection.Interior
.ColorIndex = 34
.Pattern = xlSolid
End With
Range("pwdentry").Select
ActiveCell.Formula = "********"
Range("appserver").Select

DSNDirectory = "\\" & Range("appserver") & "\" &
Range("appprogdir") & "\utils\"
DSNFilename = Range("dsnfile")

FileSaveDirectory = Range("FileSaveDirectory")
FileSaveName = Range("FileSavename")

Worksheets("DSN").Activate
Worksheets("DSN").Calculate

Application.DisplayAlerts = True
'Application.DisplayAlerts = False

****ERROR OCCURS HERE IF PROJECT IS LOCKED****
ActiveWorkbook.SaveAs Filename:=DSNDirectory & DSNFilename, _
FileFormat:=xlTextMSDOS, CreateBackup:=False
Application.DisplayAlerts = True