-
Mar 24th, 2011, 12:14 PM
#1
Thread Starter
New Member
[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?
-
Mar 24th, 2011, 03:18 PM
#2
Hyperactive Member
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
-
Mar 25th, 2011, 12:44 AM
#3
-
Mar 25th, 2011, 04:08 AM
#4
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
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Mar 29th, 2011, 09:45 AM
#5
Thread Starter
New Member
Re: [Word VBA] Save changes to a ReadOnly document
Ok, so any ideas on how to achieve what I need?
-
Mar 29th, 2011, 11:08 AM
#6
Hyperactive Member
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.
Last edited by nO_OnE; Mar 29th, 2011 at 04:49 PM.
-
Mar 29th, 2011, 03:48 PM
#7
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
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Apr 4th, 2011, 03:43 PM
#8
Thread Starter
New Member
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?
-
Apr 4th, 2011, 04:02 PM
#9
Hyperactive Member
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.
-
Apr 4th, 2011, 04:23 PM
#10
Re: [Word VBA] Save changes to a ReadOnly document
Any ideas on how to accomplish this?
you could use a field to get the user, instead of hardcoding the users name?
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Apr 4th, 2011, 04:54 PM
#11
Hyperactive Member
Re: [Word VBA] Save changes to a ReadOnly document
Originally Posted by poporacer
Any ideas on how to accomplish this?
From my first msg:
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.
Last edited by nO_OnE; Apr 4th, 2011 at 04:59 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|