如何解决从Excel数据库替换邮件合并MS Word中的内容
我想创建一个宏,以从Excel中的数据替换邮件合并Word模板(使用SQL数据库)中的某些内容,然后另存为新文件。
为弄清我的情况,当我们打开此Word模板时,它基本上更新了SQL数据库中的所有内容,除了Excel数据库中的一些特殊文本。这就是我需要编写代码以自动更新这些文本的原因
我已经写了宏代码,但是内容没有被替换。我想这个问题可能是由邮件合并Word引起的,但我不知道要解决此问题
理想情况下,宏会针对一次存在的所有替换操作执行此操作。
` 子WordFindAndReplace()
Dim IncreaseData As Worksheet
Dim ws As Worksheet
Dim objWord As Object
Dim i As Integer
Dim strValue1 As String
Dim strValue2 As String
Dim strValue3 As String
Dim strValue4 As String
Dim strValue5 As String
Dim strValue6 As String
Dim Path As String
Dim lr As Integer
Set ws = ThisWorkbook.Sheets(1)
Set IncreaseData = ThisWorkbook.Worksheets("Increase Data")
lr = IncreaseData.Cells(Rows.Count,"E").End(xlUp).Row
For i = 2 To lr
strValue1 = [A1].Offset(i - 1).Value
strValue2 = [B1].Offset(i - 1).Value
strValue3 = [C1].Offset(i - 1).Value
strValue4 = [D1].Offset(i - 1).Value
strValue5 = [AH1].Offset(i - 1).Text 'WKN
strValue6 = [AI1].Offset(i - 1).Text '
strValue7 = [G1].Offset(i - 1).Text 'Tranche number
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
objWord.Documents.Open ActiveWorkbook.Path & "\TEMP_Increase_Stock_EUR.docx"
objWord.Activate
With objWord
With .Selection.Find
.Text = Cells(1,1).Text ' Replace the Pulication_Date_Bafin
.Replacement.Text = strValue1
.Execute Replace:=wdReplaceAll
.Text = Cells(1,2).Text 'Replace the Issue Date
.Replacement.Text = strValue2
.Execute Replace:=wdReplaceAll
.Text = Cells(1,3).Text 'Replace the Issued Volume
.Replacement.Text = strValue3
.Execute Replace:=wdReplaceAll
.Text = Cells(1,4).Text 'Replace the Series Number
.Replacement.Text = strValue4
.Execute Replace:=wdReplaceAll
End With
' Create the word file as new name and save in the folder path
.ActiveDocument.SaveAs Filename:=ActiveWorkbook.Path & "EFT_ " & strValue5 & "_" & strValue7 & "_tranche.docx"
.ActiveDocument.Close
.Quit
End With
下一个我
结束字幕 `
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。