• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2765
  • Last Modified:

Changing Readonly status in Word macro

I want to open a readonly Word document, turn off the readonly flag, allow changes, save the document and then turn the readonly flag back on.  I am using the following code in my Document_Open() event which does turn off the readonly flag on the file, but apparently Word still thinks the document is readonly and won't let me save it.  Is there anyway to "refresh" the activedocument readonly property to make it match the current file attribute?

Private Sub Document_Open()
    Dim ROname As String
    Dim ROobj, ROcr
   
    ROname = ActiveDocument.FullName
    If ActiveDocument.ReadOnly = True Then
        Set ROobj = CreateObject("Scripting.FileSystemObject")
        Set ROcr = ROobj.GetFile(ROname)
        ROcr.Attributes = 0
        Set ROcr = Nothing
        Set ROobj = Nothing
        ActiveDocument.ProtectionType = wdNoProtection
    End If

End Sub
0
rbecker54
Asked:
rbecker54
  • 6
  • 3
1 Solution
 
pra_kumar03Commented:
You can set the read only flga off using the FileSystemObject to read the current attribute . then change to allow changes. THen open the worrd object , do whatever you want to do. Next you save it and close it. then use the FileSystemObject to set it back to the actual attribute.
0
 
pra_kumar03Commented:
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objFile = objFileSystem.GetFile(tFiles.sSrcfiles(icounter))
objFile.Attributes = 0      ' This sets it to not read only
0
 
rbecker54Author Commented:
If I understand your suggestion, you are talking about changing the file attribute in code external to the Word document, which I can and have been doing.  I am trying to accomplish the same thing with VBA code within the document_open() and docuement_close() events of the activedocument.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
pra_kumar03Commented:
Do it in the VBA Code it self.

Private Sub Document_Open()
    Dim ROname As String
    Dim ROobj, ROcr
    dim fileattribute1 as int
        Set ROobj = CreateObject("Scripting.FileSystemObject")
        Set ROcr = ROobj.GetFile(ROname)
        fileattribute1 = ROcr.attributes
        ROcr.Attributes = 0
    ROname = ActiveDocument.FullName

    If ActiveDocument.ReadOnly = True Then
               ActiveDocument.ProtectionType = wdNoProtection
    End If

End Sub


and then use fileattribute1 for setting it back.( for this set fileattribute1  as global variable)
0
 
rbecker54Author Commented:
A couple of things in your code won't work...(1) ROname is set to ActiveDocument.FullName AFTER it is used in the Set ROcr statement and (2) ActiveDocument.ProtectionType is a readonly property and can't be changed at runtime.
0
 
pra_kumar03Commented:
Sorry , I didn't see that.
We could have done it more easily if it was a VB program running, but that is not case here. Let me think something else for it
0
 
pra_kumar03Commented:
How about this wild guess...
1. The document open event calls a VB program thru shell and passes the file information. And then closes the document.
2. The VB prog called first finds the read attribute setting using the FileSystemObject. Then sets it to allow write. Opens up the document thru it . Doing this it passes the read flag to the document. Exit out of VB prog
3. the During the document close event another VB program is called which closes the document and then sets the read flag on.
In all this you would also need to take care to decide whether the doc was opened thru VB or directly by maintaing another flag and passing them thru each opening and closing of the word. This variable will decide the actions to be taken.
0
 
rbecker54Author Commented:
Actually, this is part of a Visual Foxpro application.  What I ended up doing is setting the readonly attribute on the file off in VFP using Filesystemobject and then opening the Word document.  Code in the Word document close event saves the the document and then uses Filesystem object to turn the readonly flag back on.
Similar to your suggestion.  Not exactly what I wanted...but it will work.
0
 
pra_kumar03Commented:
Yeah that was pretty much what i said in my first comment. But later on I thought you didn't have a encapsualting app around the word doc.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now