當然可以,我以前寫的,你可以參考一下:
Private Sub CommandButton1_Click()
On Error Resume Next
Dim iRow As Integer, myPath As String
Dim wdApp As Word.Application, wdDoc As Word.Document, wdRange As Word.Range
Dim 收文日期 As String, 標題 As String, 來文單位 As String, 文號 As String, 擬辦情況 As String
"--------------------------------------------------------------------------------------------------------
Label3.Caption = "封面正在生成中..."
iRow = TextBox1.Text
"獲取待填寫資訊
來文單位 = Cells(iRow, 3).Text
來文單位 = Replace(來文單位, Chr(10), "^p") "將excel中的換行替換成word中的換行
文號 = Cells(iRow, 4).Text
文號 = Replace(文號, Chr(10), "^p") "將excel中的換行替換成word中的換行
標題 = Cells(iRow, 5).Text
標題 = Replace(標題, Chr(10), "^p") "將excel中的換行替換成word中的換行
收文日期 = CStr(Year(Now())) & Cells(iRow, 6).Text
擬辦情況 = TextBox2.Text
myPath = ThisWorkbook.Path & "\封面\"
"檔案若已開啟,則關閉已開啟檔案
For Each wdDoc In Documents
If InStr(1, wdDoc.Name, myPath & "(" & 收文日期 & ")" & 標題 & ".doc", 1) Then
wdDoc.Close savechanges:=wdDoNotSaveChanges
Exit For
End If
Next wdDoc
Set wdDoc = CreateObject(myPath & "空白模板.doc") "開啟word
wdDoc.Activate
"填寫文件
Set wdRange = wdDoc.Content "將word的文件內容賦予wdrange
wdRange.Find.Execute FindText:="{來文單位}", ReplaceWith:=來文單位, Replace:=wdReplaceAll
wdRange.Find.Execute FindText:="{文號}", ReplaceWith:=文號, Replace:=wdReplaceAll
wdRange.Find.Execute FindText:="{收文時間}", ReplaceWith:=收文日期, Replace:=wdReplaceAll
wdRange.Find.Execute FindText:="{內容摘要}", ReplaceWith:=標題, Replace:=wdReplaceAll
wdRange.Find.Execute FindText:="{辦公室擬辦}", ReplaceWith:=擬辦情況, Replace:=wdReplaceAll
"文件另存為
wdDoc.SaveAs Filename:=myPath & "(" & 收文日期 & ")" & 標題 & ".doc"
End Sub
當然可以,我以前寫的,你可以參考一下:
Private Sub CommandButton1_Click()
On Error Resume Next
Dim iRow As Integer, myPath As String
Dim wdApp As Word.Application, wdDoc As Word.Document, wdRange As Word.Range
Dim 收文日期 As String, 標題 As String, 來文單位 As String, 文號 As String, 擬辦情況 As String
"--------------------------------------------------------------------------------------------------------
Label3.Caption = "封面正在生成中..."
"--------------------------------------------------------------------------------------------------------
iRow = TextBox1.Text
"獲取待填寫資訊
來文單位 = Cells(iRow, 3).Text
來文單位 = Replace(來文單位, Chr(10), "^p") "將excel中的換行替換成word中的換行
文號 = Cells(iRow, 4).Text
文號 = Replace(文號, Chr(10), "^p") "將excel中的換行替換成word中的換行
標題 = Cells(iRow, 5).Text
標題 = Replace(標題, Chr(10), "^p") "將excel中的換行替換成word中的換行
收文日期 = CStr(Year(Now())) & Cells(iRow, 6).Text
擬辦情況 = TextBox2.Text
"--------------------------------------------------------------------------------------------------------
myPath = ThisWorkbook.Path & "\封面\"
"檔案若已開啟,則關閉已開啟檔案
For Each wdDoc In Documents
If InStr(1, wdDoc.Name, myPath & "(" & 收文日期 & ")" & 標題 & ".doc", 1) Then
wdDoc.Close savechanges:=wdDoNotSaveChanges
Exit For
End If
Next wdDoc
"--------------------------------------------------------------------------------------------------------
Set wdDoc = CreateObject(myPath & "空白模板.doc") "開啟word
wdDoc.Activate
"--------------------------------------------------------------------------------------------------------
"填寫文件
Set wdRange = wdDoc.Content "將word的文件內容賦予wdrange
wdRange.Find.Execute FindText:="{來文單位}", ReplaceWith:=來文單位, Replace:=wdReplaceAll
wdRange.Find.Execute FindText:="{文號}", ReplaceWith:=文號, Replace:=wdReplaceAll
wdRange.Find.Execute FindText:="{收文時間}", ReplaceWith:=收文日期, Replace:=wdReplaceAll
wdRange.Find.Execute FindText:="{內容摘要}", ReplaceWith:=標題, Replace:=wdReplaceAll
wdRange.Find.Execute FindText:="{辦公室擬辦}", ReplaceWith:=擬辦情況, Replace:=wdReplaceAll
"--------------------------------------------------------------------------------------------------------
"文件另存為
wdDoc.SaveAs Filename:=myPath & "(" & 收文日期 & ")" & 標題 & ".doc"
End Sub