昨天幫同事做了一道“作業”,具體應用的場景不能詳細地透露,大體上就是如題。用的 Office 是 2010 版本,解決步驟是:
- 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系統的內碼是簡體),則那些參數的漢字在保存成文本文件時會是亂碼,保存文件名時也會是亂碼。但是在如果都是簡體,或者都是繁體的系統中,則不會出現這樣的事情。