PDA

View Full Version : [SOLVED:] TypeText Cell Into Word Formatting



ajhez
05-20-2020, 03:44 AM
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? :doh:

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

macropod
05-20-2020, 05:00 AM
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/threads/keep-excel-text-formatting-when-transferring-data-to-word-with-excel-vba.1134374/post-5487942