atw: Re: Insert wrapper text VBA for Geoffrey

  • From: Howard Silcock <howard.silcock@xxxxxxxxx>
  • To: "austechwriter@xxxxxxxxxxxxx" <austechwriter@xxxxxxxxxxxxx>
  • Date: Thu, 4 Dec 2014 22:05:04 +1100

What, no text? But Geoffrey's question was: "Is it possible with VBA to
wrap *selected text* in some specified characters?"

Oh well, I agree that a different approach is needed if there is no text.
But I can't understand why you're so keen to avoid "interfering with the
selected range" - to the extent of introducing two duplicates. I would just
use

Sub WrapTextRoundSelection(Prefix as String, Suffix as String)
    With Selection.Range
        .InsertBefore Prefix
        .InsertAfter Suffix
    End With
End Sub

Yes, this will cause the range that starts out as Selection.Range to expand
twice to include the prefix and the suffix, but so what? You can't use it
again for anything - it doesn't even have a name by which you could refer
to it again.

I do agree about avoiding continually changing the selection, but here
that's not really relevant - the selection here is only used as the initial
input.

Howard

On 4 December 2014 at 20:24, Steve Hudson <sh1448291904@xxxxxxxxx> wrote:

> The problem with the suggested solution is there is no .text in Geoffrey's
> fields - they are graphics. So, we have to work with the range object as
> shown below.
>
> Remember, the Range object is king for working in the document. Avoid
> using Selection in VBA as when it changes it triggers all sorts of funky
> background processing as if we were truly typing.
>
>
> Sub BraKet()
>    InsertWrapper Selection.Range, "(", ")"
> End Sub
>
>
> Sub InsertWrapper(R As Range, Prefix As String, Suffix As String)
> Dim Worker As Range
>
>
> 'lets insert the suffix first
> Set Worker = R.Duplicate 'so we dont interfere with the selected range
> Worker.Collapse wdCollapseEnd
> Worker.InsertAfter Suffix
>
> 'now the prefix
> Set Worker = R.Duplicate 'no interfere
> Worker.Collapse wdCollapseStart
> Worker.InsertAfter Prefix
>
> 'cleanup
> Set Worker = Nothing
>
> End Sub
>
>
>

Other related posts: