Consulting

Results 1 to 5 of 5

Thread: Formatted Word text to Excel

  1. #1
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location

    Formatted Word text to Excel

    I have procedure that finds text in Word and the puts the Word paragraph text (where the term was fount) into and Excel cell:

    Sheets("Test").Cells(xlRow, 3).Value = oRng.Paragraphs(1).RangeText 'Where oRng is the found range in the word document.

    This results in unformatted text in the Excel cell. If my word text has formatting, e.g., bold, italic, underline etc. How can I adapt this line of code so that the result in the Excel cell matches the formatted text in the Word document.
    Greg

    Visit my website: http://gregmaxey.com

  2. #2
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,634
    Sub M_snb()
      GetObject(, "word.application").activedocument.Paragraphs(1).Range.Copy
      ActiveSheet.Cells(20, 1).PasteSpecial "HTML"
    End Sub

  3. #3
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    snb, thanks! One other question if you don't mind. Using either method the resulting text is strung out as one line. Is there a way to make it wrap to a certain cell width?
    Greg

    Visit my website: http://gregmaxey.com

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,634
    Is this what you mean ?
    Sub M_snb()
      GetObject(, "word.application").activedocument.Paragraphs(1).Range.Copy
      ActiveSheet.Cells(20, 1).PasteSpecial "HTML"
      ActiveSheet.Cells(20, 1).wraptext=true
    End Sub
    I also was somewhat disappointed to find out that Paragraphs(1).range.formattedtext won't work either from Word to Excel (and I suspect to any other Office application).

  5. #5
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Thanks!!
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

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