|
楼主 |
发表于 2024-1-11 11:06
|
显示全部楼层
压缩文件中有一张表和三个模板,表中有几个不同项目的获奖信息。打开表后点击“加载项”、“显示窗体”,分别选择模板和sheet后,完成邮件合并。但运行到红色语句时错误。请老兄看看怎样修改。源代码如下:
- Private Sub UserForm_Initialize()
- Dim myFiles, sh As Worksheet, myArr
- myFile = Dir(ThisWorkbook.Path & "\*.docx")
- Do While myFile <> ""
- myFile = Dir: n = n + 1
- Loop
- ReDim myFiles(n)
- myFile = Dir(ThisWorkbook.Path & "\*.docx")
- n = 0
- Do While myFile <> ""
- myFiles(n) = myFile: n = n + 1: myFile = Dir
- Loop
- 模板名称.List = myFiles
- s = Worksheets.Count - 1: n = 0
- ReDim myArr(s)
- For Each sh In Worksheets
- If sh.Name <> "评委打分" Then
- myArr(n) = sh.Name: n = n + 1
- End If
- Next
- 表单名称.List = myArr: Erase myArr
- End Sub
- Private Sub 取消_Click()
- Unload Me
- End Sub
- Private Sub 确定_Click()
- Unload Me
- Application.DisplayAlerts = xlAlertsNone
- Application.ScreenUpdating = False
- Dim wordApp As New Word.Application
- mySource = ThisWorkbook.Path & "\获奖名单.xlsm"
- wordApp.Documents.Open (ThisWorkbook.Path & "" & 模板名称)
- wordApp.ActiveDocument.MailMerge.MainDocumentType = wdFormLetters
- wordApp.Visible = True
- Set doc = GetObject(ThisWorkbook.Path & "" & 模板名称)
- doc.MailMerge.OpenDataSource Name:= _
- mySource, LinkToSource:=True, Format:=wdOpenFormatAuto, Connection:="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" _
- & mySource & ";Extended Properties=""HDR=YES;IMEX=1;"";Jet OLEDB:Engine Type=35;Jet OLEDB:Databas" _
- , SQLStatement:="SELECT * FROM `'" & 表单名称.Value & "$'`", SubType:=wdMergeSubTypeAccess
- wordApp.Documents.Application.Activate
- With wordApp.ActiveDocument.ActiveWindow.Selection
- .HomeKey Unit:=wdStory
- [color=Red]wordApp.Selection.MailMerge.Fields.Add Range:=Selection.Range, Name:="获奖者"[/color]
- If ActiveDocument.Name = "书法证书.doc" Then
- myArr1 = Array("度", "荣获"): myArr2 = Array("组别", "获奖等级")
- For n = 0 To UBound(myArr1)
- If .Find.Execute(findtext:=myArr1(n), Forward:=True) Then
- Selection.MoveRight Unit:=wdCharacter, Count:=1
- ActiveDocument.MailMerge.Fields.Add Range:=Selection.Range, Name:=myArr2(n)
- End If
- Next
- ElseIf ActiveDocument.Name = "运动会模板.doc" Then
- myArr1 = Array("荣获", "项目"): myArr2 = Array("年级", "组别", "项目名称", "获奖等级")
- For n = 0 To UBound(myArr1)
- If .Find.Execute(findtext:=myArr1(n), Forward:=True) Then
- .MoveRight Unit:=wdCharacter, Count:=1
- If n = 0 Then
- ActiveDocument.MailMerge.Fields.Add Range:=Selection.Range, Name:=myArr2(0)
- ActiveDocument.MailMerge.Fields.Add Range:=Selection.Range, Name:=myArr2(1)
- ActiveDocument.MailMerge.Fields.Add Range:=Selection.Range, Name:=myArr2(2)
- Else
- ActiveDocument.MailMerge.Fields.Add Range:=Selection.Range, Name:=myArr2(3)
- End If
- End If
- Next
- Else
- myArr1 = Array("在", "第"): myArr2 = Array("学年度", "学期")
- For n = 0 To UBound(myArr1)
- If .Find.Execute(findtext:=myArr1(n), Forward:=True) Then
- Selection.MoveRight Unit:=wdCharacter, Count:=1
- ActiveDocument.MailMerge.Fields.Add Range:=Selection.Range, Name:=myArr2(n)
- End If
- Next
- End If
- End With
- With ActiveDocument.MailMerge
- .Destination = wdSendToNewDocument: .SuppressBlankLines = True
- With .DataSource
- .FirstRecord = wdDefaultFirstRecord: .LastRecord = wdDefaultLastRecord
- End With
- .Execute Pause:=False
- End With
- Application.DisplayAlerts = wdAlertsAll: Application.ScreenUpdating = True
- End Sub
复制代码 |
|