question re removing final Paragraph mark (and field) from autotex

P

Peter

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
 
D

Doug Robbins - Word MVP

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
 
P

Peter

Doug,

Thanks, as always, for your reply.

I needed to modify it a little but it got me in the right track. (I needed
to work with reduce the size of the selection as I was using this later on to
define the autotext entry.

The finsihed code is:

theSelection = Selection.Range
If theSelection = "" Then
MsgBox "You must select the text of the comment."
Exit Sub
Else
If Right(Selection.Range, 1) = vbCr Then
Selection.MoveLeft unit:=wdCharacter, count:=1, Extend:=wdExtend
End If
End If

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

Thanks again,
Peter
 
G

Graham Mayor

Having already begun to use a range 'theSelection' you could have stuck with
that and not reverted back to the selected text eg

Dim theSelection As Range
Dim theName As String
theName = "TestABC"
Set theSelection = Selection.Range
If theSelection = "" Then
MsgBox "You must select the text of the comment."
Exit Sub
Else
If theSelection.Characters.Last = vbCr Then
theSelection.End = theSelection.End - 1
End If
End If
NormalTemplate.AutoTextEntries.Add name:=theName, _
Range:=theSelection

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
P

Peter

Graham,

I was not able to get your code to run. I suspect that i am gettng mixed up
with theselection and selection and variant and range

BUT the following gets rid of the last paragraph mark
If Right(Selection.Range, 1) = vbCr Then
Selection.MoveLeft unit:=wdCharacter, count:=1, Extend:=wdExtend
End If

BUT turns out that the field for the page number can be stored in any of the
paragraph marks so I need to go through whole selection and remove the page
field code (where ever it is). Perhpas I will just add this to the list of
things that are too hard.

Thanks for your assistance,
Peter Evans

Peter Evans
 

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