- EXCEL用几列存储规定的内容。比如B列存负责输出,把几个参数写入文本文件,放置到指定目录下。一个参数做成一个文本文件,
- EXCEL打开WORD模板文件(.DOCM)。
- 这个WORD模板文件,有AutoOpen宏,目的是一打开即运行。该巨集的作用是:从那些文本文件中一一取参数,放在指定的地方。然后另存为一个DOC文档。
- 因为在AutoOpen巨集中不能直接调用Quit方法,所以在宏执行完后,手动关闭WORD文档。
EXCEL文件 之 自定义宏的代码:
Sub BuildParaAndRunWord()
Dim aFileName(4)
nRow = InputBox("请输入行号(用数字表示)", "日期、部门、拼音和入厂日期这四项要完整!")aFileName(1) = Range("B" & Trim(Str(nRow))).Value 'Name
Dim aFileName(4)
nRow = InputBox("请输入行号(用数字表示)", "日期、部门、拼音和入厂日期这四项要完整!")aFileName(1) = Range("B" & Trim(Str(nRow))).Value 'Name
aFileName(2) = Range("C" & Trim(Str(nRow))).Value 'Depart
aFileName(3) = Range("D" & Trim(Str(nRow))).Value 'PinYin
aFileName(4) = Range("E" & Trim(Str(nRow))).Value 'InFactory Date
aFileName(3) = Range("D" & Trim(Str(nRow))).Value 'PinYin
aFileName(4) = Range("E" & Trim(Str(nRow))).Value 'InFactory Date
' 把这四项分别存入1、2、3、4个文本文件
For I = 1 To 4
cFullName = "C:\123\" & Trim(Str(I)) & ".txt"
Open cFullName For Output As #1
Print #1, aFileName(I)
Close #1
Next
' 打开WORD,接下来的活儿由WORD模板文件自行完成
Open cFullName For Output As #1
Print #1, aFileName(I)
Close #1
Next
' 打开WORD,接下来的活儿由WORD模板文件自行完成
Dim appWD As Word.Application, doc As Object
Set appWD = CreateObject("Word.Application")
appWD.Visible = True
appWD.Documents.Open Filename:="C:\123\Module.docm"
End Sub
Set appWD = CreateObject("Word.Application")
appWD.Visible = True
appWD.Documents.Open Filename:="C:\123\Module.docm"
End Sub
WORD模板文件 之 AutoOpen宏的代码:
Sub OpenOpen()
Dim aComment(4)
' 把参数从1、2、3、4文本文档中读取出来
' 把参数从1、2、3、4文本文档中读取出来
For I = 1 To 4
FilePath = "C:\123\" & Trim(Str(I)) & ".txt"
Set FileObj = CreateObject("Scripting.FileSystemObject")
Set TextObj = FileObj.OpenTextFile(FilePath)
txtLine = Trim(TextObj.ReadLine)
aComment(I) = txtLine
'MsgBox txtLine
Next
'跳到指定位置,更改不同内容
Selection.MoveDown Unit:=wdLine, Count:=4
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=aComment(1)
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=aComment(2)
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=aComment(3)
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=Left(aComment(4), 4) & "/" & Mid(aComment(4), 5, 2) & "/" & Right(aComment(4), 2)
' 用替代法找到模板中的邮件地址,并更改为新收件者英文名字
FilePath = "C:\123\" & Trim(Str(I)) & ".txt"
Set FileObj = CreateObject("Scripting.FileSystemObject")
Set TextObj = FileObj.OpenTextFile(FilePath)
txtLine = Trim(TextObj.ReadLine)
aComment(I) = txtLine
'MsgBox txtLine
Next
'跳到指定位置,更改不同内容
Selection.MoveDown Unit:=wdLine, Count:=4
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=aComment(1)
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=aComment(2)
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=aComment(3)
Selection.MoveRight Unit:=wdCell
Selection.MoveRight Unit:=wdCell
Selection.TypeText Text:=Left(aComment(4), 4) & "/" & Mid(aComment(4), 5, 2) & "/" & Right(aComment(4), 2)
' 用替代法找到模板中的邮件地址,并更改为新收件者英文名字
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "Sample"
.Replacement.Text = aComment(3)
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
' 另存为DOC文档
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "Sample"
.Replacement.Text = aComment(3)
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
' 另存为DOC文档
ActiveDocument.SaveAs FileName:="C:\123\" & aComment(1) & ".doc"
End Sub
顺便说一句:
End Sub
顺便说一句:
如果在“控制面板”里的“区域与语言选项”,把“非UNICODE程序所使用的当前语言”,设置成了“中文(繁体*)”,(其实WIN7系统的内码是简体),则那些参数的汉字在保存成文本文件时会是乱码,保存文件名时也会是乱码。但是在如果都是简体,或者都是繁体的系统中,则不会出现这样的事情。