Consulting

Results 1 to 3 of 3

Thread: Determine the extent of the text that has just been inserted in vba

  1. #1
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,872

    Determine the extent of the text that has just been inserted in vba

    Say I have just used vba code to insert some plain text into the middle of a document (by using a placeholder bookmark, or a range variable, or less likely, a selection related location), now I want further to manipulate the newly inserted text in vba.
    1. Is there a 'best-practice' or method of choice to refer to the newly inserted text.
    2. Would the same method be used to identify the range having pasted from Excel using the likes of .PasteExcelTable?

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    I'd define the range, then add text to it and use the range for any further manipulations of that text. For example:
    Sub Demo()
    Dim Rng As Range
    With ActiveDocument
      Set Rng = .Sections.First.Range.Tables(1).Range.Cells(1).Range.Paragraphs(1).Range.Characters.Last
      With Rng
        .Collapse wdCollapseStart
        .Text = "Hello World"
        With .Words.First.Font
          .Bold = True
          .ColorIndex = wdPink
        End With
        With .Words.Last.Font
          .Italic = True
          .ColorIndex = wdBrightGreen
        End With
      End With
    End With
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,872
    Thankyou for this macropod, I'll try to use this in the thread: http://www.vbaexpress.com/forum/show...on-cell-values where I found the process of formatting inserted text to be more convoluted than I would want/expect.

Posting Permissions

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