Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 19th, 2006, 10:07 AM
Registered User
 
Join Date: Dec 2006
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default creating read-only files

Does anybody know how to create read-only csv files within VBA? I am currently using the following line of code:

saveFilename = Application.GetSaveAsFilename(defaultPath, "CSV file (*.csv), *csv", , "Save as CSV file")

However, this doesn't prevent a user from making unwanted changes to the file that got saved.

Thank you!
Reply With Quote
  #2 (permalink)  
Old December 19th, 2006, 10:54 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: London, , United Kingdom.
Posts: 173
Thanks: 0
Thanked 3 Times in 3 Posts
Default

You can do this with the Scripting libaray. Example as follows:

Code:
Sub test()

Dim strSaveFilename As String

Dim scrFSO As Scripting.FileSystemObject
Dim scrFile As Scripting.File

    strSaveFilename = ""

    ' Set up Scripting variables
    Set scrFSO = New Scripting.FileSystemObject
    Set scrFile = scrFSO.GetFile(FilePath:=strSaveFilename)

    ' Set file to be read-only
    scrFile.Attributes = ReadOnly

    ' De-reference variables
    Set scrFSO = Nothing
    Set scrFile = Nothing

End Sub
To get the code to work you'll need to add a refernece to you project to Microsoft Scripting Runtime (Tools -> References...)

Maccas

Reply With Quote
  #3 (permalink)  
Old December 19th, 2006, 02:04 PM
Registered User
 
Join Date: Dec 2006
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you Maccas, but when I try to add the Scripting runtime reference, it says: "Error accessing the system registry."

I tried figuring out how I can fix this problem by going on Microsoft's website (http://support.microsoft.com/kb/269383), but they propose making changes to the registry, which will be difficult for me to do because it would involve working with a budget-constrained IT department.

Does anybody know a way to create read-only files without making changes to registries?
Reply With Quote
  #4 (permalink)  
Old December 20th, 2006, 05:00 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: London, , United Kingdom.
Posts: 173
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Try using late binding then. Here's a reworking of the original post without the requirement to use references:

Code:
Sub test()

Dim strSaveFilename As String

Dim scrFSO As Object 'Scripting.FileSystemObject
Dim scrFile As Object 'Scripting.File

    strSaveFilename = ""

    ' Set up Scripting variables
    Set scrFSO = CreateObject("Scripting.FileSystemObject")
    Set scrFile = scrFSO.GetFile(FilePath:=strSaveFilename)

    ' Set file to be read-only
    scrFile.Attributes = 1 'ReadOnly

    ' De-reference variables
    Set scrFSO = Nothing
    Set scrFile = Nothing

End Sub
Reply With Quote
  #5 (permalink)  
Old December 20th, 2006, 09:24 AM
Registered User
 
Join Date: Dec 2006
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It worked! Thank you so much.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
read pdf files in C# prasannatele Visual Studio 2005 0 September 12th, 2007 05:46 AM
how to read files in evc++ iriskab Visual C++ 1 September 1st, 2006 04:19 AM
Writing Files that can't be read Ron_Bingham BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 0 April 23rd, 2005 02:25 PM
Opening Files Read-Only SerranoG Access VBA 5 May 6th, 2004 02:40 PM



All times are GMT -4. The time now is 02:12 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.