[Word VBA] Save changes to a ReadOnly document
I have a word doocument that I have the readOnly property set to prevent the user from accidently changing the doc. The document has an onOpen macro for creating a form. The user will input his name as a reviewer and this will be placed in a bookmark in the form. This needs to be saved in the original so that the user will now be the default reviewer. I have tried several methods. Change the attributes of the document by SetAttr.Path.vbNormal, which works but when I try to save the document I get a permission error. It seems like the SetAttr isn't recognized by Word until it closes. I even tried to SetAttr, SaveAs with a temporary name and delete the original, but I still get a permission error. Here is the code that I am using so far. If the ReadOnly property is not set, the program runs, and the property is set to ReadOnly after the progrgam finishes. If the ReadOnly property is set the program crashes at the Save function and the ReadOnly property has been removed. Any Suggestions?
Code:
Dim oRng As Word.Range
Dim BodyText As String
Dim FPath As String
Set oRng = ActiveDocument.Bookmarks("bk1").Range
oRng.Text = ActiveDocument.Bookmarks("bk1").Range.Text
UserName = InputBox("Enter your Name", "Name", oRng)
oRng.Text = UserName
ActiveDocument.Bookmarks.Add "bk1", oRng
FPath = ActiveDocument.FullNameSetAttr FPath, vbNormal
ActiveDocument.Save
SetAttr FPath, vbReadOnly
The above code gives me a permission error with the .Save command
I also tried:
Code:
With ThisDocument
strName = .FullName
.SaveAs FileName:=strName & "1"
Kill strName
.SaveAs FileName:=strName
Kill strName & "1"
End With
I get a permission error with this as well.
I also tried to use a template but can't save the one change to the template via code.
Any suggestions on the best way to do this?
Re: [Word VBA] Save changes to a ReadOnly document
There isn't really a way to save a document you opened as read only. The best solution would be to protect the document, or the parts that need to be, and you can programatically remove that protection when needed, and then re-protect it.
http://office.microsoft.com/en-us/wo...001039775.aspx
The other option would be to save the document as something else, then over write the original.
Scroll down to "Figure 6"
http://msdn.microsoft.com/en-us/magazine/cc163637.aspx
Re: [Word VBA] Save changes to a ReadOnly document
:wave:Welcome to VBF:wave:
If a document is "ReadOnly", this is set by the OS, so you can't do anything about that in Word! This is the reason for your permission errors.
As nO_OnE posted, you could do a work-around, by saving a copy using a different name, however to delete the original, you need a permission by the OS!
Re: [Word VBA] Save changes to a ReadOnly document
as he is using setarr (and tried kill), if it is set read only by file attributes, it should work. but it may need to make pauses, to allow changes to take effect, but if it is set as read only by excel (generally as it is open or appears to be open already), it may required a different approach
Re: [Word VBA] Save changes to a ReadOnly document
Ok, so any ideas on how to achieve what I need?
Re: [Word VBA] Save changes to a ReadOnly document
Did you look at the links in my post?
When you said you had permission errors, was that you, or was it your users. If you choose to control your document in that mannor, then they need to have the proper permissions for that folder for each of the users.
Re: [Word VBA] Save changes to a ReadOnly document
why is the file readonly?
does it have its readonly atrribute bit set, or is it because it is already open (or appears to be open) by excel?
or was it saved as readonly recommended?
you have not given enough information
Re: [Word VBA] Save changes to a ReadOnly document
The file is read only because I don't want the user to change the original form, but want one of the bookmarks to be edited (the User name) so that the next time the user opens the document, the user's name will now be the default user. The document should then be saved with the new user and then the rest of the form be filled out. Any ideas on how to accomplish this?
Re: [Word VBA] Save changes to a ReadOnly document
Sorry, I have never really messed with the "Setattr" function before. After your post I started playing with it.
The problem with using the "Setattr" function for what you want, is that when you open the document as readonly, even after you change it's properties Word will still have it flagged as readonly, so you won't be able to save it even though you changed it's properties.
So the only way to make the setattr work, would be to set the attribute, then close the document/re-open it, make the changes, save it, change it back to read-only, and close it, re-open it if necessary.
Or again, you could save it as something else, then over write the original. There is a additional sample in the links on my first post.
Re: [Word VBA] Save changes to a ReadOnly document
Quote:
Any ideas on how to accomplish this?
you could use a field to get the user, instead of hardcoding the users name?
Re: [Word VBA] Save changes to a ReadOnly document
Quote:
Originally Posted by
poporacer
Any ideas on how to accomplish this?
From my first msg:
Quote:
Originally Posted by
nO_OnE
Provide more info...
Does the document have to stay with the same name, when ever someone changes it does it add more things or is it more like a template.
Why not just have the document set as readonly, but only allow users to have access to the form, ie. all print operations and other things could be controlled from the form, open it as modeless so they cannot access the document. If they close the form it closes the document.
There are many different options, it just ultimately depends on what you need, which we are not completely sure of.