如何使用excel vba从行创建多个word文档?
Posted
技术标签:
【中文标题】如何使用excel vba从行创建多个word文档?【英文标题】:How to create multiple word documents from rows using excel vba? 【发布时间】:2020-08-20 13:24:11 【问题描述】:我有一个包含多个个人信息的 Excel 工作表。我需要为每个人填充一个单词模板。所有名称都列在Column A
中,每个信息子集都列在Columns B:N
中。我有一个预先制作的模板,其中包含某些文本值,我将用特定列中的数据替换这些值。我的代码目前非常适合单个行。我需要它循环,以便我可以一键创建和填写所有文档。我还需要一些帮助来编写基于“”单元格中值 0、1、2 或 3 的代码部分,将插入一行文本。我尝试使用IF
公式在excel 中编写公式,但我需要输入的文本行太长。任何帮助都将不胜感激。
我的代码如下...
Sub ReplaceText()
Dim wApp As Word.Application
Dim wDoc As Word.Document
Set wApp = CreateObject("Word.Application")
wApp.Visible = True
Set wDoc = wApp.Documents.Open("z:\Sound Sleepers\forms\PCP.dotx")
With wDoc
.Application.Selection.Find.Text = "<<PCP>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("D2")
.Application.Selection.EndOf
.Application.Selection.Find.Text = "<<name>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("A2")
.Application.Selection.EndOf
.Application.Selection.Find.Text = "<<dob>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("B2")
.Application.Selection.EndOf
.Application.Selection.Find.Text = "<<dos>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("M2")
.Application.Selection.EndOf
.Application.Selection.Find.Text = "<<results>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("O2")
.Application.Selection.EndOf
.Application.Selection.Find.Text = "<<Sleep>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("L2")
.Application.Selection.EndOf
.Application.Selection.Find.Text = "<<Sleep>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("L2")
.Application.Selection.EndOf
.SaveAs2 Filename:=("Oneida PCP"), _
FileFormat:=wdFormatXMLDocument, AddtoRecentFiles:=False
End With
End Sub
【问题讨论】:
【参考方案1】:这是for
循环。它使用 A 列中的值保存文档。
Sub ReplaceText()
Dim wApp As Word.Application
Dim wDoc As Word.Document
Set wApp = CreateObject("Word.Application")
wApp.Visible = True
lastRow = Cells(Rows.Count, 1).End(xlUp).Row ' all of column A
For r = 2 To lastRow ' each row
DocName = Range("A" & r).Value
Set wDoc = wApp.Documents.Open("z:\Sound Sleepers\forms\PCP.dotx")
With wDoc
.Application.Selection.Find.Text = "<<PCP>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("D" & r)
.Application.Selection.EndOf
.Application.Selection.Find.Text = "<<name>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("A" & r)
.Application.Selection.EndOf
.Application.Selection.Find.Text = "<<dob>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("B" & r)
.Application.Selection.EndOf
.Application.Selection.Find.Text = "<<dos>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("M" & r)
.Application.Selection.EndOf
.Application.Selection.Find.Text = "<<results>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("O" & r)
.Application.Selection.EndOf
.Application.Selection.Find.Text = "<<Sleep>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("L" & r)
.Application.Selection.EndOf
.Application.Selection.Find.Text = "<<Sleep>>"
.Application.Selection.Find.Execute
.Application.Selection = Range("L" & r)
.Application.Selection.EndOf
NormalCheck = Range("O" & r).Value
Phrase = "Invalid Normal Check"
Select Case NormalCheck
Case 0
Phrase = "Normal"
Case 1
Phrase = "Abnormal Questionnaire, Normal ESS"
Case 2
Phrase = "Normal Questionnaire, Abnormal ESS"
Case 3
Phrase = "Abnormal Questionnaire, Abnormal ESS"
Case Else
Phrase = "Invalid Normal Check"
End Select
.Application.Selection.Find.Text = "<<NormalCheck>>"
.Application.Selection.Find.Execute
.Application.Selection = Phrase
.Application.Selection.EndOf
.SaveAs2 Filename:=(DocName), _
FileFormat:=wdFormatXMLDocument, AddtoRecentFiles:=False
End With
Next
End Sub
【讨论】:
我收到一个“编译错误:没有 For 的下一步”对话框,在尝试调试时弹出...有帮助吗?顺便感谢您的帮助。 刚刚整理好。我只需要将“next”移到“end with”语句下方。 关于 0-3 查找,对于每个人,在“O”列中分配了一个值。如果他们有一个 0,我需要它来说一句关于正常的长句。如果该值为“1”,则需要说一个很长的短语,说明问卷异常,但 ESS 正常。如果该值为 2,则需要说出一个关于正常问卷但 ESS 异常的短语。最后,如果值为 3,我需要说一个关于异常 ESS 和异常问卷的短语。 该词组要加到word文档中? 通过正常检查更新答案。为之前的错字道歉。以上是关于如何使用excel vba从行创建多个word文档?的主要内容,如果未能解决你的问题,请参考以下文章