Microsoft KB Archive/280463

= WD97: Error Message: &quot;Can't Assign to Read-Only Property&quot; When You Use Macro to Change URL =

Article ID: 280463

Article Last Modified on 1/29/2007

-

APPLIES TO


 * Microsoft Word 97 Standard Edition

-



This article was previously published under Q280463



SYMPTOMS
When you attempt to use a Visual Basic for Applications macro to change the URL (Universal Resource Locator) of a hyperlink, you receive the following error message:

Can't assign to read-only property.



CAUSE
The Address property of a Hyperlink object returns the address (for example, a file name or URL) of the specified hyperlink. However, in Microsoft Word 97, the Hyperlink.Address property is read-only.



WORKAROUND
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

For more information about how to use the sample code in this article, click the article number below to view the article in the Microsoft Knowledge Base:

173707 OFF97: How to Run Sample Code from Knowledge Base Articles

To work around this problem, modify the Code property of the hyperlink Field object, and then update the field. This allows you to change a hyperlink address by using a Visual Basic for Applications macro.

The Field.Code property, which is a read/write property, returns a range object that represents a field's code. A field's code is everything that is enclosed by the field characters ({}), including the leading space and trailing space characters. To view a hyperlink's address (URL), do one of the following:
 * Right-click the hyperlink. On the shortcut menu that appears, point to Hyperlink and then click Edit Hyperlink.

-or-
 * If screen tips are turned on, hold the mouse pointer over the hyperlink.

NOTE: To turn on screen tips (on by default), click Options on the Tools menu. On the View tab, click to select the ScreenTips check box, and then click OK.

-or-
 * Press ALT+F9 to turn the HYPERLINK field on or off, in order to show the field code and address (URL).

Microsoft Word documents can contain other types of fields besides hyperlinks (for example, page numbers, date fields, and so on). To ignore these other fields and modify only the hyperlink fields, you can use a conditional statement to modify the Field.Type property.

The hyperlink display text does not necessarily have to be the same as the hyperlink address (URL). In this example, the hyperlink address (URL) changes to &quot;http://www.microsoft.com/support&quot;, but the hyperlink display text still appears as &quot;http://www.microsoft.com/&quot;.

To test the sample macro code in this article, you can use the following sample macro to create 20 hyperlinks with the address &quot;http://www.microsoft.com/&quot;. Sub CreateHyperlinks

Dim i As Variant

For i = 1 To 20 Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _ Text:=&quot;HYPERLINK &quot;&quot;http://www.microsoft.com/&quot;, _ PreserveFormatting:=False Selection.TypeParagraph Selection.TypeParagraph Next i

End Sub

Macro to Modify the Addresses of Hyperlink Fields
The following sample macro does the following:
 * Determines whether the field is a HYPERLINK field. If the field is not a HYPERLINK field, the macro immediately proceeds to the next field.

-then-
 * After the field is determined to be a HYPERLINK field, removes leading and trailing spaces from the address (URL) contained in the Field.Code property.

-then-
 * Modifies the address (URL) of the text of the Field.Code property.

-then-
 * Updates the modified HYPERLINK field.

Sub ModifyHyperlinkField

'Dimension two string variables Dim trimCode As String, modFldCode As String Dim Field As Field

'For each field in the document For Each Field In ActiveDocument.Fields

'Modify only fields of type, wdFieldHyperlink If Field.Type = wdFieldHyperlink Then

'Trim the Code string to remove leading and trailing spaces trimCode = Trim(Field.Code)

'Modify the string as needed modFldCode = trimCode & &quot;support&quot;

'Plug the modified string back into the field Field.Code.Text = modFldCode

'Update Field.Update End If

Next

End Sub NOTE: This workaround is not necessary when you are programming in Microsoft Word 2000, because the properties for Hyperlink.Address have been changed to read/write.

Additional query words: vba Can t assign to read only property

Keywords: kbmacroexample kbprb KB280463

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.