View Single Post
  #3   Report Post  
Posted to microsoft.public.word.newusers
Greg Maxey Greg Maxey is offline
external usenet poster
 
Posts: 171
Default Document Property to display more than 1 line

Lene,

While your code appears to work, it seems a bit like driving a tack
with a sledge hammer.

I have used "Replace" build the insert string as follows:

Sub ScratchMacro()
Dim strName As String
Dim strInsert As String
Dim oRng As Range
Set oRange = Selection.Range
strPropName = "Address"
On Error GoTo Err_Handler:
strInsert = ActiveDocument.CustomDocumentProperties(strName).V alue
strInsert = Replace(strInsert, "#", Chr(11))
oRng.InsertAfter strInsert
Exit Sub
Err_Handler:
If Err.Number = 5 Then
MsgBox "The docProperty " & Chr(34) & strName & Chr(34) _
& " is not found in this document."
End If
End Sub





Lene Fredborg wrote:
Word does not allow property values with line breaks and a field cannot span
more than one line. However, by making a minor change to the property values
and by using a macro, you can handle addresses and other "multi-line" text
strings as properties anyway.

Below you will find a macro that fetches the value of a custom document
property named "Address". In order for the macro to work, separate the
different parts of the "Address" value (street name, postcode, state name,
etc.) by # (can be replaced by any other character that is not used in your
addresses). The macro does not insert a property field but creates a new
string in which all # are replaced by manual line breaks (chr(11)). This
string is inserted at the end of the selection. The macro also works if your
addresses consist of more or less than 3 parts.

Example:

Property name: "Address"
Property value: "MyStreetName 23#1234 MyCity#MyCountry"

Result in the document:

MyStreetName 23
1234 MyCity
MyCountry

Here is the macro:

Sub InsertProperty_MultiLine()

Dim oProp As DocumentProperty
Dim strPropName As String
Dim StrInsert As String
Dim bPropFound As Boolean
Dim oRange As Range
Dim oArray As Variant
Dim n As Long

'REPLACE "Address" by the name of your property
strPropName = "Address"

'Define the place to insert the address
Set oRange = Selection.Characters.Last
'=========================
'Check that strPropName is found before inserting
For Each oProp In ActiveDocument.CustomDocumentProperties
If oProp.Name = strPropName Then
bPropFound = True
'get property value
'Split in array
'Replace all # in value by manual line breaks
oArray = Split(oProp.Value, "#")
'Combine each part with a manual line break between
StrInsert = ""
'Build the string
For n = LBound(oArray) To UBound(oArray)
StrInsert = StrInsert & oArray(n) & Chr(11)
Next n
'Remove last chr(11) and string is correct
StrInsert = Left(StrInsert, Len(StrInsert) - 1)
'Insert text in document
oRange.InsertAfter StrInsert
GoTo LineExit
End If
Next oProp
'=========================
'Show msg is property not found
If bPropFound = False Then
MsgBox "Custom document property " & _
strPropName & " not found.", vbOKOnly, _
"Property missing"
GoTo LineExit
End If

LineExit:
Set oRange = Nothing
End Sub

--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word


"Alan T" wrote:

I added a property 'Address' in a word document and defined a field link
with this property.

I got a string, ie address consists of street name, postcode, state name
separated by a return character.
Is it possible to display the address in several lines?