From: Peter on
I've got a VBA application that allows teachers to easily save and reuse
autotext entries when grading assignments.

Generally things are going well but if the user enters a margin comment then
highlights that commment then saves it, the autotext will contain the final
paragraph mark and this contains a field for the page number. If I carefully
do not include the final paragraph mark and then store the autotext then the
field for the page number is not included.

The reusable text is refered to as range.selection. I would like to
programatically strip the page number out of the autotext entry. I also need
to allow images and objects and links to be included in the autotext entry to
do not want to convert theslection to a string.

I've prepared a screen movie demonstrating the problem ... the first
reusable comment does not inlcude the final paragraph mark and then second
does including the page number field.

http://screencast.com/t/NjUxMjdk

The relevant part of the code is:
theSelection = Selection.Range
theName = Trim(InputBox("(less than 31 characters)", "enter a name for the
comment"))
NormalTemplate.AutoTextEntries.Add Name:=theName, Range:=Selection.Range


and the complete code is:

Public Sub SaveComment()
'save the currently selected text as a reusable comment
Dim anyChanges
Dim toAdd As String
Dim maxcount As Integer
Dim myArray(1000) As String
Dim temp As String
Dim theName As String
Dim theresult As String
Dim theSelection As Variant
Dim inputTitle As String
Dim inputPrompt As String
On Error GoTo ErrorHandler
Debug.Print autoTextItemName
If daysToGo < -3 Then showUnregistered
theSelection = Selection.Range
If theSelection = "" Then
MsgBox "You must select the text of the comment."
Exit Sub
End If
inputTitle = "What should this reusable comment be called?"
inputPrompt = "Suggest the name should:"
inputPrompt = inputPrompt & vbCrLf & "- start with a '.'"
inputPrompt = inputPrompt & vbCrLf & "- use categories to make it easier
to find in future"
inputPrompt = inputPrompt & vbCrLf & "- be less than 32 characters long."
inputPrompt = inputPrompt & vbCrLf & " e.g. .acad.writ.use of 1st
person"
inputPrompt = inputPrompt & vbCrLf & "The name of the last comment you
reused is inserted"
inputPrompt = inputPrompt & vbCrLf & "in case you want to redefine it."
theName = Trim(InputBox(inputPrompt, inputTitle, autoTextItemName))

If theName = "" Then
MsgBox "You must give the comment a name."
Exit Sub
End If

autoTextItemName = theName

temp = NormalTemplate.AutoTextEntries(theName)
Debug.Print temp
If temp <> "" Then
theresult = MsgBox("There is already a comment with that name. Do
you want to replace it?", vbYesNoCancel)
If theresult = vbNo Then Exit Sub
End If

NormalTemplate.AutoTextEntries.Add Name:=theName, Range:=Selection.Range

displayAutotext
Exit Sub
ErrorHandler:
' there is no existing autotext with that name
If err.Number = 5941 Then
temp = ""
Resume Next
End If

If err.Number = 5854 Then
MsgBox ("Sorry. The name must be less than 32 characters long.")
End If
End Sub

Thanks in advance for any assistance,
Peter Evans
eMarkingAssistant.com
From: Doug Robbins - Word MVP on
Replace

If theSelection = "" Then
MsgBox "You must select the text of the comment."
Exit Sub
End If

With

If theSelection = "" Then
MsgBox "You must select the text of the comment."
Exit Sub
ElseIf Right(theSelection.Text, 1) = vbCr Then
theSelection.End = theSelection.End - 1
End If


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com

"Peter" <Peter(a)discussions.microsoft.com> wrote in message
news:07ACF4FF-02D0-48AF-B63B-CCB8C0BA76D4(a)microsoft.com...
> I've got a VBA application that allows teachers to easily save and reuse
> autotext entries when grading assignments.
>
> Generally things are going well but if the user enters a margin comment
> then
> highlights that commment then saves it, the autotext will contain the
> final
> paragraph mark and this contains a field for the page number. If I
> carefully
> do not include the final paragraph mark and then store the autotext then
> the
> field for the page number is not included.
>
> The reusable text is refered to as range.selection. I would like to
> programatically strip the page number out of the autotext entry. I also
> need
> to allow images and objects and links to be included in the autotext entry
> to
> do not want to convert theslection to a string.
>
> I've prepared a screen movie demonstrating the problem ... the first
> reusable comment does not inlcude the final paragraph mark and then second
> does including the page number field.
>
> http://screencast.com/t/NjUxMjdk
>
> The relevant part of the code is:
> theSelection = Selection.Range
> theName = Trim(InputBox("(less than 31 characters)", "enter a name for
> the
> comment"))
> NormalTemplate.AutoTextEntries.Add Name:=theName, Range:=Selection.Range
>
>
> and the complete code is:
>
> Public Sub SaveComment()
> 'save the currently selected text as a reusable comment
> Dim anyChanges
> Dim toAdd As String
> Dim maxcount As Integer
> Dim myArray(1000) As String
> Dim temp As String
> Dim theName As String
> Dim theresult As String
> Dim theSelection As Variant
> Dim inputTitle As String
> Dim inputPrompt As String
> On Error GoTo ErrorHandler
> Debug.Print autoTextItemName
> If daysToGo < -3 Then showUnregistered
> theSelection = Selection.Range
> If theSelection = "" Then
> MsgBox "You must select the text of the comment."
> Exit Sub
> End If
> inputTitle = "What should this reusable comment be called?"
> inputPrompt = "Suggest the name should:"
> inputPrompt = inputPrompt & vbCrLf & "- start with a '.'"
> inputPrompt = inputPrompt & vbCrLf & "- use categories to make it
> easier
> to find in future"
> inputPrompt = inputPrompt & vbCrLf & "- be less than 32 characters
> long."
> inputPrompt = inputPrompt & vbCrLf & " e.g. .acad.writ.use of 1st
> person"
> inputPrompt = inputPrompt & vbCrLf & "The name of the last comment you
> reused is inserted"
> inputPrompt = inputPrompt & vbCrLf & "in case you want to redefine
> it."
> theName = Trim(InputBox(inputPrompt, inputTitle, autoTextItemName))
>
> If theName = "" Then
> MsgBox "You must give the comment a name."
> Exit Sub
> End If
>
> autoTextItemName = theName
>
> temp = NormalTemplate.AutoTextEntries(theName)
> Debug.Print temp
> If temp <> "" Then
> theresult = MsgBox("There is already a comment with that name. Do
> you want to replace it?", vbYesNoCancel)
> If theresult = vbNo Then Exit Sub
> End If
>
> NormalTemplate.AutoTextEntries.Add Name:=theName,
> Range:=Selection.Range
>
> displayAutotext
> Exit Sub
> ErrorHandler:
> ' there is no existing autotext with that name
> If err.Number = 5941 Then
> temp = ""
> Resume Next
> End If
>
> If err.Number = 5854 Then
> MsgBox ("Sorry. The name must be less than 32 characters long.")
> End If
> End Sub
>
> Thanks in advance for any assistance,
> Peter Evans
> eMarkingAssistant.com