hyperlink; read-only

T

TRM

I am trying to find out if it is possible to change the settings of a
hyperlink so that a document will be opened as "Read-only". Thanks!
 
L

Larry

This will do what you want. However, it's not clear to me yet how you
would unset the ReadOnly atttribute by VBA once you've set it. The
comments explain the macro step by step.

Sub SetLinkedDocToReadOnlyAndOpen()

' First Step. Get the address of the document in the hyperlink.
' (Part of this code was provided by Jay Freedman last year.)
' Copies text of selected hyperlink into the clipboard.

' The hyperlink doesn't have to be completely selected.
' If cursor is in the hyperlink, of if you right-click the Hyperlink
' and have installed this macro in the Hyperlink shortcut menu, this
will work.

System.Cursor = wdCursorIBeam

' If no hyperlink is selected, the macro will stop.
If Selection.Hyperlinks.Count = 0 Then
MsgBox "No hyperlink is selected."
Exit Sub
End If

Dim MyData As DataObject
Dim strURL As String
strURL = Selection.Hyperlinks(1).Address
Set MyData = New DataObject
Set oDataObject = New DataObject
MyData.SetText strURL
MyData.PutInClipboard
Dim sAddress As String

' Get the document's address from Clipborad, assign address to variable,
set the ReadOnly attribute.

oDataObject.GetFromClipboard
sAddress = oDataObject.GetText

SetAttr sAddress, vbReadOnly

' You can comment out this message box.
MsgBox sAddress & " has been set to Read Only"

' Open the document.
Application.Run "HyperlinkOpen"

End Sub

Larry
 
L

Larry

Here's how to close the document and automatically unset the Read Only
attribute from Word without having to find the document in its folder.

First, you need to create a module-wide variable in a public declaration
at the top of the variable. I forget the exact rules for setting this
up, but it's very simple. This creates a string in one macro that can
be carried over to another macro.

Dim myReadOnlyDoc As String

Then, in the macro I gave you before, after the last line of code (where
the document is opened), insert this line:

myReadOnlyDoc = ActiveDocument.FullName

Then create a second macro, as below. This macro closes the document
and then unsets the Read Only attribute.
Note: the Read Only attribute cannot be changed while a document is
open.

Sub CloseReadOnlyDocAndUnset()

ActiveDocument.Close wdDoNotSaveChanges
SetAttr myReadOnlyDoc, 0
' You could comment out this message box.
' I put it here so that you will see that the macro
' is doing what it's supposed to do.
MsgBox "The read only attribute of " & vbCr & myReadOnlyDoc & vbCr & "
has been unset."

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top