Re: deleting the final space character in a field

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Jay Freedman (jay.freedman_at_verizon.net)
Date: 07/30/04


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


Relevant Pages

  • Re: Multiple dependant combo boxes.
    ... >>Access expert or anything. ... >>table name to be used as the recordsource. ... First combo selection ... is from Table A. The value may be BOB so the second combo sources it's rows ...
    (microsoft.public.access.forms)
  • Re: Zoom to Selection ZoomPicBox
    ... Thanks Bob I have a few projects going but when I get time to wrap this ... > by the selection operation, as the position to which the scroll should be ... > which the zoom should be set. ... >>trying to add the ability to zoom to a selection rectangle and zoom into ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Re-entrant function
    ... Bob & Dana, ... Dim x As Double ... ReDim CP ... the third pass when the selection is ...
    (microsoft.public.excel.programming)
  • Re: Combo Box Display Question
    ... Thanks Bob, your idea works great! ... and the width of the second column to the entire size of the combo ... >> combo box to see both columns and make a selection. ... Is it possible to display the name field ...
    (microsoft.public.access.forms)