Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Save Change to ReadOnly Word document

Status
Not open for further replies.

poporacer

Programmer
Oct 2, 2007
53
US
I have a word doocument that I have the readOnly property set to prevent the user from accidently changing the doc. I have VBA code for filling in text in the document. There is one change to the document that I need to have changed while the user is running the macro and I need it to be invisible to the user. I am able to change the attributes of the file, but it seems like they don't take effect until the document is closed. I even tried to 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?

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.FullName
SetAttr FPath, vbNormal 'this works if you look in explorer it is changed
ActiveDocument.Save
SetAttr FPath, vbReadOnly
 
Have you thought about using a template? That way, you wouldn't be able to do anything but "Save As".

Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.

My newest novel: Wooden Warriors
 
If ActiveDocument=ThisDocument:
Code:
With ThisDocument
    strName = .FullName
    .SaveAs FileName:=strName & "1"
    Kill strName
    .SaveAs FileName:=strName
    Kill strName & 1
End With


combo
 
RonRepp,
I need to save the information in one of the bookmarks while the user is entering the information in the original documents (.doc or .dot) I cannot get it to save the template file (as it opens a new .doc). Since a new document is opened from the template, I cant do a .save. If I do a .SaveAs to the .dot file, I get an error saying that you cant save to a document that is already open.

Combo,
I did try that (I mentioned it it my post) On the Kill command, I get a permission error, but if I look at the file in explorer after the program closes, the ReadOnly property is not set. If I start with the ReadOnly property not set, the Kill method works. But I need the ReadOnly Property set to begin with.

Thanks for your help...any other ideas?
 
In some cases word seems to keep in memory information about original file. Not very nice, but this works for me:
Code:
Dim wdDoc As Document
With ThisDocument
    strName = .FullName
    .SaveAs FileName:=strName & "_tmp"
    SetAttr strName, vbNormal
    With Application
        .ScreenUpdating = False
        Set wdDoc = .Documents.Open(strName)
        wdDoc.Close
        .ScreenUpdating = True
    End With
    Kill strName
    .SaveAs FileName:=strName
    Kill strName & "_tmp"
    SetAttr strName, vbReadOnly
End With

combo
 
This didn't work either. I have some input boxes to get data and after the first one (the users name) I want to save the form so that the user's name will be the default value. I tried your code and it looks like the document doesn't actually close. Then the document re-opens and starts over. But then the code fails at

.SaveAs FileName:=strName & "_tmp"
with an error of Word cannot give a document the same name of an open document. So it looks like it runs the first time and then the "new" document opens and the "strName & "_tmp" is still in memory.
Now what?
 
I don't want to close the document. The idea is:
1. Save document with new name,
2. Open and close original document (to clear memory, problem for changed document),
3. Save document with original name,
4. Delete document with new name.

The code should work at least once. In case of series of actions you could consider adding uniqe extensions.
Alternatively consider protection of the document (wdAllowOnlyFormFields/wdAllowOnlyReading)
or separating document with code from user's document (+ protection). It's not a standard way to planning changes together with making document read-only.

combo
 
Ok, so what would be the solution to my problem?
This is what I need. I need a document that is protected so the user does not accidently overwrite the original document. When the user opens the document, they will input thier name and this will be put into a bookmark. This change will be saved to the original via code (invisible to the user) so that the next time the user opens the document, they will be the default name on the document. Any suggestions?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top