Re: deleting the final space character in a field
From: Jay Freedman (jay.freedman_at_verizon.net)
Date: 07/30/04
- Next message: booger: "Re: Searching a page for a value and assigning it as a variable"
- Previous message: George Shubin: "Re: Selecting and Printing Text Across Multiple Bookmarks"
- In reply to: Bob S: "Re: deleting the final space character in a field"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 30 Jul 2004 12:47:08 -0400
Bob S wrote:
> On Mon, 26 Jul 2004 08:09:09 -0400, Jay Freedman
> <jay.freedman@verizon.net> wrote:
>
>> 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
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.
-- Regards, Jay Freedman Microsoft Word MVP FAQ: http://word.mvps.org
- Next message: booger: "Re: Searching a page for a value and assigning it as a variable"
- Previous message: George Shubin: "Re: Selecting and Printing Text Across Multiple Bookmarks"
- In reply to: Bob S: "Re: deleting the final space character in a field"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|