Consulting

Results 1 to 9 of 9

Thread: Setting active font properties within a textbox

  1. #1

    Setting active font properties within a textbox

    Hi,

    This is a fairly basic issue which I've been trying to solve on and off for weeks!

    I currently have a macro which sets the formatting of selected to a predefined format. This works well but I have been asked to edit it so that you can click the button and then type in the desired format as compared to typing first, selecting that text and then clicking the button.

    What am I missing that would allow this?

    Many thanks!

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    Without actually seeing the macro, it's a little hard to see what's missing
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Hi Paul - please see below. Thanks


    Sub RedStrikethrough()
    Dim Text As TextRange2
    If ActiveWindow.Selection.Type = ppSelectionText Then
        Set Text = ActiveWindow.Selection.TextRange2
        With Text
            .Words.Font.Fill.ForeColor.RGB = vbRed
            .Words.Font.Strikethrough = msoTrue
        End With
    End If
    End Suba

  4. #4
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    Not sure I understand how the user will input the required format but be aware you current code will not work if the user selects characters in the word - Only those selected will change. If you need the WORD to change regardless of the selection you wil need more complex code.

    Sub RedStrikethrough()
    
       Dim otr2 As TextRange2
       Dim allText As TextRange2
       Dim L As Long
       If ActiveWindow.Selection.Type = ppSelectionText Then
          Set allText = ActiveWindow.Selection.ShapeRange(1).TextFrame2.TextRange
          Set otr2 = ActiveWindow.Selection.TextRange2
          L = selectedWord(otr2, allText)
          allText.Words(L).Font.Fill.ForeColor.RGB = vbRed
       End If
    End Sub
    
    
    Function selectedWord(otr2 As TextRange2, allText As TextRange2) As Long
    
    
    Dim L As Long
     For L = 1 To allText.Words.Count
            If otr2.Start < allText.Words(L).Start + allText.Words(L).Length Then
            selectedWord = L
            Exit For
            End If
            Next
    End Function
    This might need to be even more complex to NEVER fail. It is not clear if the user can select multiple words.
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  5. #5
    Thanks for the reply John.

    The intention is for the desired format to be pre-defined so there is no need for user input in that regard. In addition, when selecting individual characters/words (or the entire textframe for that matter) my code above functions as desired.

    It is only when the cursor is placed at the end of the text with no text selected, for example, and the button is clicked that it does not behave as desired. In this scenario it should set the active font which will be used when typing starts to the pre-defined format (just as the font colour or bold buttons would work, for example) but instead it does nothing.

    The code definitely executes and
    If ActiveWindow.Selection.Type = ppSelectionText
    returns true so that's not the issue but I can't get any further I'm afraid.

    Please let me know if I have missed the point of your post!

    Thanks

  6. #6
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    I think the only way might be to insert a space set to red and select it., When you type the space would be overwritten. Not elegant but should work.
    Sub StartTypinginRed()ActiveWindow.Selection.TextRange2.InsertAfter(" ").Select
    ActiveWindow.Selection.TextRange2.Font.Fill.ForeColor.RGB = vbRed
    End Sub
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  7. #7
    Works well, thanks very much.

    Interestingly, when I do the above and then press backspace to delete the inserted space, the formatting persists when I then start typing. However, when I inserted a line after your suggestion (ActiveWindow.Selection.Delete) to do the same, this did not have the desired effect. Presumably this is just an oddity of Powerpoint.

    Thanks again.

  8. #8
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,093
    Location
    I don't like using SendKeys but see if this works

    Sub StartTypinginRed()    
    With ActiveWindow.Selection.TextRange2.InsertAfter(" ")
            .Font.Fill.ForeColor.RGB = vbRed
        End With
        SendKeys "({BACKSPACE})"
    End Sub
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  9. #9
    It works flawlessly even if it isn't that elegant! Thanks a lot, John.

Tags for this Thread

Posting Permissions

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