从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 (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...