Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
| 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 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 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!
 
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

 
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?
 
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
 
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.





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





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