deleting the final space character in a field

B

Bob S

What is a good way to delete the final character (a space character)
in a field? Word 2002 SP2

What I have been trying is

Set rngWork = Selection.Range

Set fldWork = ActiveDocument.Fields.Add(Range:=Selection.Range,
Type:=wdFieldEmpty, PreserveFormatting:=False)

rngWork.Select
' this lets me check the selection; it seem to be selecting the whole
' field including the braces

' there is an extra space character at each end of the field contents,
' because an empty field actually contains two space characters

Selection.Range.Characters(2).Delete
' this line successfully deletes the leading space


' This next line crashed Word hard
Selection.Range.Characters(Selection.Range.Characters.Count -
1).Delete

When run in a macro that last line crashes Word completely (please
send a report, recovering the documents, etc.).

When run in the immediate window it doesn't crash, but doesn't delete
the space (or anything else) either! If I use 2 instead of 1, it
successfully deletes the character before the space.



Bob S
 
J

Jay Freedman

Hi Bob,

Trying to fiddle around with the selection in VBA is a sure route to
the funny farm. Instead, use the field object's .Code property, which
is a range covering the field's contents when the field code is
displayed. The following macro will create a field and set its code
without any surrounding spaces:

Dim ofld As Field
Set ofld = ActiveDocument.Fields.Add(Range:=Selection.Range, _
Type:=wdFieldEmpty, PreserveFormatting:=False)
ofld.Code.Text = "EQ \o(x,¯)"
ofld.Update
 
B

Bob S

Hi Bob,

Trying to fiddle around with the selection in VBA is a sure route to
the funny farm. Instead, use the field object's .Code property, which
is a range covering the field's contents when the field code is
displayed. The following macro will create a field and set its code
without any surrounding spaces:

Dim ofld As Field
Set ofld = ActiveDocument.Fields.Add(Range:=Selection.Range, _
Type:=wdFieldEmpty, PreserveFormatting:=False)
ofld.Code.Text = "EQ \o(x,¯)"
ofld.Update

I looked at that, but using the Code property apparently would have
meant chopping the existing formatted text that I am trying to
enclose, saving it somewhere (another document?), then sticking it
back in. This seemed excessive.

I have found the following circumlocution that appears to work so
far...

Set rngWork = Selection.Range
Set fldWork = ActiveDocument.Fields.Add(Range:=Selection.Range,
Type:=wdFieldEmpty, PreserveFormatting:=False)
' This apparently shrinks the Selection, so we save and restore it
rngWork.Select
' Selection now includes the whole field including the magic braces

' There is an extra space character at each end of the field contents,
' because an empty field actually contains two space characters
Selection.Range.Characters(2).Delete

' The three lines below are being used because the obvious way crashed
Selection.Collapse Direction:=wdCollapseEnd
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdMove
Selection.TypeBackspace

' This line crashed Word hard
' Selection.Range.Characters(Selection.Range.Characters.Count -
1).Delete

Thank you for the idea.

Bob S
 
J

Jay Freedman

Bob said:
I looked at that, but using the Code property apparently would have
meant chopping the existing formatted text that I am trying to
enclose, saving it somewhere (another document?), then sticking it
back in. This seemed excessive.

I have found the following circumlocution that appears to work so
far...

Set rngWork = Selection.Range
Set fldWork = ActiveDocument.Fields.Add(Range:=Selection.Range,
Type:=wdFieldEmpty, PreserveFormatting:=False)
' This apparently shrinks the Selection, so we save and restore it
rngWork.Select
' Selection now includes the whole field including the magic braces

' There is an extra space character at each end of the field contents,
' because an empty field actually contains two space characters
Selection.Range.Characters(2).Delete

' The three lines below are being used because the obvious way crashed
Selection.Collapse Direction:=wdCollapseEnd
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdMove
Selection.TypeBackspace

' This line crashed Word hard
' Selection.Range.Characters(Selection.Range.Characters.Count -
1).Delete

Thank you for the idea.

Bob S

Hi Bob,

I didn't realize you had formatted text selected when you did the
..Fields.Add -- I thought you were just trying to insert an empty field and
then fill in its code.

While your method works, it's a little more work than necessary. This macro
does it more efficiently (although I was disappointed to find that
Selection.Characters.Last.Delete doesn't work the way
Selection.Characters.First.Delete does; in fact it doesn't work at all).

Dim ofld As Field
Set ofld = ActiveDocument.Fields.Add(Range:=Selection.Range, _
Type:=wdFieldEmpty, PreserveFormatting:=False)
ofld.Code.Select
Selection.Characters.First.Delete
ofld.Code.Select
Selection.Collapse wdCollapseEnd
Selection.TypeBackspace
ofld.Update

The trick here is that ofld.Code.Select will select only the stuff inside
the field braces.
 

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