Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.word.newusers
|
|||
|
|||
Document Property to display more than 1 line
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? |
#2
Posted to microsoft.public.word.newusers
|
|||
|
|||
Document Property to display more than 1 line
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? |
#3
Posted to microsoft.public.word.newusers
|
|||
|
|||
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? |
#4
Posted to microsoft.public.word.newusers
|
|||
|
|||
Document Property to display more than 1 line
Greg,
I fully agree - "Replace" is absolutely better than the solution I suggested. Thanks. -- Lene "Greg Maxey" wrote: 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? |
#5
Posted to microsoft.public.word.newusers
|
|||
|
|||
Document Property to display more than 1 line
Hi, thanks for all advices.
The problem is the address string was return from a component, the address lines are separated by a carriage return key, ie a little vertical rectangle. In the Replace function, strInsert = Replace(strInsert, "#", Chr(11)) what should I put into the "#" ? "Lene Fredborg" wrote in message ... Greg, I fully agree - "Replace" is absolutely better than the solution I suggested. Thanks. -- Lene "Greg Maxey" wrote: 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? |
#6
Posted to microsoft.public.word.newusers
|
|||
|
|||
Document Property to display more than 1 line
Select, then use the following macro to identify the character
Sub ANSIValue() S1$ = "Because the selected text contains" S2$ = " characters, not all of the ANSI values will be displayed." S3$ = "ANSI Value (" S4$ = " characters in selection)" S5$ = " character in selection)" S6$ = "Text must be selected before this macro is run." S7$ = "ANSI Value" Dim strSel As String Dim strNums As String Dim LastFourChar As String Dim iPos As Integer strSel = Selection.Text If Len(strSel) 0 Then For i = 1 To Len(strSel) strNums = strNums + Str(Asc(Mid(strSel, i))) Next i strNums = LTrim(strNums) If Len(strNums) 255 Then LastFourChar = Mid(strNums, 252, 4) strNums = Left(strNums, 251) + Left(LastFourChar, _ 4 - InStr(" ", LastFourChar)) MsgBox S1$ + Str(Len(strSel)) + S2$ End If If Len(strSel) = 1 Then S4$ = S5$ MsgBox strNums, 0, S3$ + LTrim(Str(Len(strSel))) + S4$ Else MsgBox S6$, 0, S7$ End If End Sub -- Graham Mayor - Word MVP My web site www.gmayor.com Word MVP web site http://word.mvps.org Alan T wrote: Hi, thanks for all advices. The problem is the address string was return from a component, the address lines are separated by a carriage return key, ie a little vertical rectangle. In the Replace function, strInsert = Replace(strInsert, "#", Chr(11)) what should I put into the "#" ? "Lene Fredborg" wrote in message ... Greg, I fully agree - "Replace" is absolutely better than the solution I suggested. Thanks. -- Lene "Greg Maxey" wrote: 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? |
#7
Posted to microsoft.public.word.newusers
|
|||
|
|||
Document Property to display more than 1 line
When you only need to find the character code of a _single_ character, you
can do as follows: In the document, select the character (or position the insertion marker to the left of it) Open the Immediate window in VBA and type: ?asc(selection.characters.first) Hit Return to display the character code. If you often need to determine a _single_ character code, it may be easier to have a macro that displays the value. The macro below can be used: Sub ShowCharacterCode() Displays character code of first character in selection With Selection.Characters MsgBox "Character: " & .First & vbCr & _ "Code: " & Asc(.First) End With End Sub -- Regards Lene Fredborg DocTools Denmark www.thedoctools.com Document automation add-ins, macros and templates for Microsoft Word "Alan T" wrote: Hi, thanks for all advices. The problem is the address string was return from a component, the address lines are separated by a carriage return key, ie a little vertical rectangle. In the Replace function, strInsert = Replace(strInsert, "#", Chr(11)) what should I put into the "#" ? "Lene Fredborg" wrote in message ... Greg, I fully agree - "Replace" is absolutely better than the solution I suggested. Thanks. -- Lene "Greg Maxey" wrote: 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? |
Reply |
Thread Tools | |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Setting title document property affects retrieval of author | Microsoft Word Help | |||
Setting title document property affects retrieval of author | Microsoft Word Help | |||
How to remove a solid line from a document? | Page Layout | |||
Templates | Page Layout | |||
Why is line spacing different between printers in document Office. | Microsoft Word Help |