Results 1 to 3 of 3

Thread: VBA How to pass formatted text from Excel to MS Word

  1. #1
    Junior Member
    Join Date
    Mar 2015
    Posts
    2
    Rep Power
    0

    VBA How to pass formatted text from Excel to MS Word

    I've requirement where in I need to compare two samples of text in Excel using VBA. Since each text sample is more than 10000 characters in LEN, I use a Userform.TextBox field fetch,storing them into string variables.

    Next, I use a VBA code that I found here by 'mikerickson': VBA routine to perform Text Diff

    To compare the texts. The problem is that the output of this code is input into a Cell: A1. Since the text samples contain more than 10000 characters each, the result is not displayed in A1 since excel has limit on the number of characters per cell.

    I would like to know how to get the result (with all the formatting like underline and strikethrough) in to an MS Word Doc.

    Currently, this is the code used to output to cell A1.

    strResult = ComparedText(strOne, strTwo, olStart, olLength, nwStart, nwLength)

    Code:
    With outCell.Cells(1, 1)
        .Clear
        .Value = strResult
        For i = LBound(olStart) To UBound(olStart)
            If olStart(i) <> 0 Then
    
                With .Characters(olStart(i), olLength(i)).Font
                    .ColorIndex = 3
                    .StrikeThrough = True
                End With
            End If
        Next i
        For i = LBound(nwStart) To UBound(nwStart)
            If nwStart(i) <> 0 Then
                With .Characters(nwStart(i), nwLength(i)).Font
                    .ColorIndex = 4
                    .Underline = True
                End With
            End If
        Next i
    End With
    Need a replacement code here which will enter strResult into a word document with the exact same formatting for the mismatches as in the code above.
    No intermediate Excel Cells to be used to store the strResult, it needs to be directly written into the word document.
    Hoping somebody helps. Thanks in advance.
    Last edited by johnweber; 03-01-2015 at 06:38 PM.

  2. #2
    Administrator Excel Fox's Avatar
    Join Date
    Mar 2011
    Posts
    1,402
    Rep Power
    10
    Can you upload a sample output which needs to come in Word?
    A dream is not something you see when you are asleep, but something you strive for when you are awake.

    It's usually a bad idea to say that something can't be done.

    The difference between dream and aim, is that one requires soundless sleep to see and the other requires sleepless efforts to achieve

    Join us at Facebook

  3. #3
    Junior Member
    Join Date
    Mar 2015
    Posts
    2
    Rep Power
    0
    Hi Admin! Thanks for the prompt reply.

    Userform1.Textbox1.Value="Hi I am John."
    Userform1.Textbox2.Value="Hola Hi I,m Hjhn John"

    As per code of MikeEricson's in VBA routine to perform Text Diff
    Expected output is in the image attached.

    1.jpg


    The code which adds this formatting is in my first post on this thread.
    Last edited by johnweber; 03-01-2015 at 08:44 PM.

Similar Threads

  1. Excel Chart into Word VBA
    By dhiraj.ch185 in forum Word Help
    Replies: 3
    Last Post: 12-26-2012, 11:23 AM
  2. VBA To Pass A Variable In An Excel Formula
    By devcon in forum Excel Help
    Replies: 4
    Last Post: 12-17-2012, 09:12 PM
  3. FORMATTED Flexible Concatenation Function
    By Rick Rothstein in forum Rick Rothstein's Corner
    Replies: 1
    Last Post: 10-14-2012, 03:48 PM
  4. How to extract all text from a word file?
    By vsrawat in forum Word Help
    Replies: 3
    Last Post: 09-25-2012, 10:24 PM
  5. Speed up excel to word VBA
    By bcostin in forum Excel Help
    Replies: 3
    Last Post: 05-22-2012, 10:49 AM

Posting Permissions

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