![]() Sub DoMerge() Dim appWd As Word.Application Dim WdDoc As Word.Document Dim strBookFullName As String Dim cell As Range Dim wdFind As Object strBookFullName = ActiveWorkbook.FullName Set appWd = CreateObject("Word.Application") appWd.Visible = True With appWd Set WdDoc = ("C:\Users\Elmo\Documents\Aaron\2nd Project 2017\WorkingOn\VendorRebateClientDoc4March17_2.docx") WdDoc.Activate Name:=(strBookFullName), _ ReadOnly:=True, LinkToSource:=0, AddToRecentFiles:=False, _ PasswordDocument:="", PasswordTemplate:="", WritePasswordDocument:="", _ WritePasswordTemplate:="", Revert:=False, Format:=wdOpenFormatAuto, _ Connection:="", SQLStatement:="SELECT * FROM `MergeRange`", SQLStatement1:="" Set wdFind = 'finds text in word doc and replace with merge field For Each cell In Range("MergeText") wdFind.Text = cell.Value Dim MyDoc As String, txt As String, t As String MyDoc = txt = cell.Offset(0, 1).Value t = Replace(MyDoc, txt, "") X = (Len(MyDoc) - Len(t)) / Len(txt) 'ActiveDocument.Bookmarks("Cname").Select Range:=WdDoc.Bookmarks(cell.Offset(0, 2).Value).Range, Name:=txt Next cell End With End Subīelow is the code that i used. ![]() I have also tried to record a macro doing this but Word does not allow me to right click while recording. I have seen that when you add a field manually in a Word document and right click and Edit it there is a check box to Preserve formatting but i cannot find how to this with VBA. I do not mind so much about the "$" signs as i can easily add this to the Word document the decimal places is the real issue. For example in the Excel wb a field might be a currency field with 2 decimal places when it is taken to the word document it does not retain this formatting and instead of $12.30 i end up with 12.3012943. The only problem that i'm having is that formatting is not carried over into the Word document form the Excel workbook. All the work from creating the connection to adding the merge fields happens in a macro in the Excel workbook. The word document contains bookmarks to indicate where merged fields should be added. I have created a Word document and Excel workbook to do a mail merge.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |