Consulting

Results 1 to 2 of 2

Thread: TypeText Cell Into Word Formatting

  1. #1
    VBAX Regular
    Joined
    May 2015
    Posts
    44
    Location

    TypeText Cell Into Word Formatting

    Hey everyone,

    The section of my code below is used to take cell data from excel and insert it into various bookmarks within a word document template, before moving onto the next cell(s) to do the same again.

    Normally this works fine, but due to a recent change in the information being stored in the cells (sometimes paragraphs of formatted text rather than just numbers or the occasional one word of text) - I now need to try to modify it so it retains the formatting (or merges formatting with the text format in the target word document.

    At the moment it just inserts as plain unformatted text. Assuming this is because of TypeText solution is too simple?

    Any ideas would be great.

    Sub GenerateLetters()Dim doc As Word.Document
    wd.Visible = True
    Dim CustomerRange As Range
    Range("B2").Select
    Set CustomerRange = Range( _
    ActiveCell, _
    ActiveCell.End(xlToRight))
    For Each CustomerCell In CustomerRange.SpecialCells(xlCellTypeVisible).Cells
    Set doc = wd.Documents.Open(MainFilePath & "Test Template" & ".docx")
    'admin
    CopyCell "bmcustomer", 0
    CopyCell "bm1", 1
    CopyCell "bm2", 2
    CopyCell "bm3", 3
    doc.SaveAs2 FilePath & "\" & CustomerCell.Value & ".docx", FileFormat:=wdFormatDocumentDefault
    doc.Close
    Next CustomerCell
    wd.Quit
    MsgBox "Created Files in " & FilePath & "!"
    End Sub
    Sub CopyCell(bmcustomer1 As String, ColumnOffset As Integer)
    wd.Selection.GoTo What:=wdGoToBookmark, Name:=bmcustomer1
    wd.Selection.TypeText CustomerCell.Offset(ColumnOffset, 0).Text
    End Sub

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Anything that manipulates text from external sources changes only the text; formatting information is lost. To work around that, see:
    https://www.mrexcel.com/board/thread...4/post-5487942
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

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
  •