[Word VBA] Save changes to a ReadOnly document-VBForums
Results 1 to 11 of 11

Thread: [Word VBA] Save changes to a ReadOnly document

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    3

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

  2. #2
    Hyperactive Member
    Join Date
    Oct 2010
    Location
    Indiana
    Posts
    448

    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

  3. #3
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: [Word VBA] Save changes to a ReadOnly document

    Welcome to VBF

    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!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    20,059

    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

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    3

    Re: [Word VBA] Save changes to a ReadOnly document

    Ok, so any ideas on how to achieve what I need?

  6. #6
    Hyperactive Member
    Join Date
    Oct 2010
    Location
    Indiana
    Posts
    448

    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 05:49 PM.

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    20,059

    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

  8. #8

    Thread Starter
    New Member
    Join Date
    Mar 2011
    Posts
    3

    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?

  9. #9
    Hyperactive Member
    Join Date
    Oct 2010
    Location
    Indiana
    Posts
    448

    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.

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    20,059

    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

  11. #11
    Hyperactive Member
    Join Date
    Oct 2010
    Location
    Indiana
    Posts
    448

    Re: [Word VBA] Save changes to a ReadOnly document

    Quote Originally Posted by poporacer View Post
    Any ideas on how to accomplish this?

    From my first msg:
    Quote Originally Posted by nO_OnE View Post
    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

    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 05: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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.