Results 1 to 8 of 8
  1. #1
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    1,294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Obtain selections format & then apply format (office XP SP3)

    Hi

    I would like to perform the following code:

    WIth current selection of text
    obtain the formatting ie
    Font is bold
    Case is TitleCas etc

    Then delete the current selection
    add some other text
    and then apply the formatting

    How would I do that via code? Ive have some code but its not happening?

    Diana :-0

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 28 Times in 28 Posts

    Re: Obtain selections format & then apply format (office XP SP3)

    Why not simply use

    Selection.TypeText "This is the new text"

    Or do you want to add the new text in another location?

  3. #3
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    1,294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Obtain selections format & then apply format (office XP SP3)

    Hey Hans

    I want my code to go goe something like this.

    Dim blnBold as boolen
    dim strCase as string
    'obtain the selections formatting
    With Selection
    blnBold = .Font.bold
    strCase = .Case
    End With

    delete Selection

    'insert the new text
    Add an autotext entry that contains text

    'apply the formatting
    Select text
    With Selection
    Font.bold = blnBold
    Case. = strCase
    End With

    Diana

  4. #4
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,805
    Thanks
    4
    Thanked 235 Times in 217 Posts

    Re: Obtain selections format & then apply format (

    Hans' point is that there are a huge number of font attributes other than bold and case that might be applied to the text. Surely it is easier to just insert the autotext entry as unformatted text in the same position as the text it is replacing. This will give you replacement text with all the same attributes as the text it is replacing.

    By doing this you won't need to find out the initial attributes, nor will you need to apply the attributes after inserting the autotext.

    To put the autotext in as unformatted text you can either omit the RichText attribute or specifically set it to false.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  5. #5
    3 Star Lounger
    Join Date
    Apr 2004
    Location
    Boston, Massachusetts, USA
    Posts
    389
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Obtain selections format & then apply format (

    Hi Diana,

    While I agree with Hans and Andrew that there are probably better ways to achieve the result you're shooting for, you've raised a very interesting question, one whose answer is applicable in other situations -- namely, how to easily capture the numerous Font properties of certain text.

    The way I'd do it is by using the Duplicate method with the Font object. Select some text, apply a variety of Font formatting (bold, underline, etc), and then run this macro:
    <pre>Sub DuplicateSelectionFontProperties()
    Dim f As Font
    Set f = Selection.Font.Duplicate
    Selection.Font.Reset
    MsgBox "The selection's font has been reset (temporarily)"
    Selection.Font = f
    MsgBox "Now it's back to what it was"
    End Sub
    </pre>

    Using Font.Duplicate allows you to grab all of those Font properties with one simple line of code. Note that this doesn't work nearly as well on text that uses multiple diffferent Font formats (where some of the text is bold, but some not, for example).

    One practical use for this is to remove a Character style from some text, but retain all the Font formatting associated with that style, as discussed here, in relation to those pesky Char Char styles (http://www.windowsdevcenter.com/pub/a/wind...dex.html?page=2).

    Cheers!

  6. #6
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Obtain selections format & then apply format (office XP SP3)

    <P ID="edit" class=small>(Edited by st3333ve on 09-Mar-05 18:04. Oops. I see Mr. Savikas has beaten me to the punch.)</P>An alternative approach -- and one that would also work for you if you were putting the new text in a different location from the deleted text -- would be to use a Font object variable to store all the old Selection's font attributes and then apply them to the new text. Word VBA makes this really easy. (If only all types of object variables supported Duplicates that work like this...) Here's a sample:
    <pre> Dim oFont As Word.Font

    With Selection
    Set oFont = .Font.Duplicate
    .Delete
    .InsertAfter "Some new text."
    .Font = oFont
    End With

    Set oFont = Nothing</pre>


  7. #7
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    1,294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Obtain selections format & then apply format (

    Hi Andrew

    When I was inserting the autotext entry I didnt use RichText. it was inserting as unformatted text. I'll use RichText & observe the outcome.

    Diana

  8. #8
    Bronze Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    1,294
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Obtain selections format & then apply format (office XP SP3)

    HI

    that worked! thanks for that thats exactly what i wanted. i didnt know that the 'duplicate' syntax existed
    you learn something new every day

    diana :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •