I'm using mail merge function to merge data from excel sheets to word letter. and I use the following connection and command.
Sub WriteInWord()
Dim WdApp As Object
Dim WdDoc As Object
result = Path.GetFileName(TextBox1.Text)
Dim BatchNumber = Mid(result, 18, 5)
WdApp = CreateObject("Word.Application")
WdDoc = CreateObject("Word.Document")
WdDoc = WdApp.Documents.Open(TextBox2.Text, ConfirmConversions:=
False, ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="",
PasswordTemplate:="", Revert:=False, WritePasswordDocument:="",
WritePasswordTemplate:="", XMLTransform:="")
WdDoc.MailMerge.OpenDataSource(Name:=TextBox1.Text,
ConfirmConversions:=False, ReadOnly:=False, LinkToSource:=True,
AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="",
WritePasswordDocument:="", WritePasswordTemplate:="", Revert:=False, Connection:=
"Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin; Mode=Read; Extended Properties=""HDR=YES;IMEX=1;"";Jet OLEDB:System database=""""; Jet OLEDB:Registry Path="""";Jet OLEDB:Engine Type=35;Jet OLEDB:Database Locking Mode=0; Jet OLEDB:Global", SQLStatement:="SELECT * FROM `Sheet1$`", SQLStatement1:="")
' .Destination 0 = DOCUMENT, 1 = PRINTER
WdApp.ActiveDocument.MailMerge.Destination = 0 'wdSendToNewDocument
WdApp.ActiveDocument.MailMerge.SuppressBlankLines = True
With WdApp.ActiveDocument.MailMerge.DataSource
.FirstRecord = 1 'wdDefaultFirstRecord
.LastRecord = -16 'wdDefaultLastRecord
End With
WdApp.ActiveDocument.MailMerge.Execute(Pause:=False)
WdDoc.Close(savechanges:=False) 'Close the original mail-merge template file.
Dim outputFilename As String = Path.ChangeExtension(inputFilePath + "\" + BatchNumber + ".docx", "pdf")
Dim fileFormat As Object = WdSaveFormat.wdFormatPDF
WdApp.ActiveDocument.SaveAs(outputFilename, fileFormat)
WdApp.Quit()
WdDoc = Nothing
WdApp = Nothing
MsgBox("FINISHED WITH MERGE")
End Sub
the excel value is this Merged value from excel to word is this
As you can see, the value from excel to word has changed. How can I fix this? Thank you.