Private Sub CommandButton輸出通知到Word檔案_Click()
Dim Word物件 As New Word.Application, 當前路徑, 匯出檔案名, 匯出路徑檔案名, i, j
Dim Str1, Str2
當前路徑 = ThisWorkbook.Path
最后行號 = Sheets("資料").Range("B65536").End(xlUp).Row
判斷 = 0
For i = 2 To 最后行號
匯出檔案名 = "標準說明-"
FileCopy 當前路徑 & "\模板.docx", 當前路徑 & "\" & 匯出檔案名 & Sheets("資料").Range("A" & i) & ".doc"
匯出路徑檔案名 = 當前路徑 & "\" & 匯出檔案名 & Sheets("資料").Range("A" & i) & ".doc"
With Word物件
.Documents.Open 匯出路徑檔案名
.Visible = False
With ActiveDocument.Sections(4).Headers(wdHeaderFooterPrimary)‘呼叫第二次在這爆出462
.Range.Text = VBA.Replace(.Range.Text, "專案名稱", Sheets("資料").Range("A" & i).Text, 1, 1, vbTextCompare)
End With
Do While .Selection.Find.Execute("專案名稱") '尋找客戶這個關鍵詞,將其用表格中的姓名來代替
.Selection.Text = Sheets("資料").Range("A" & i).Text
.Selection.HomeKey Unit:=6
Loop
Do While .Selection.Find.Execute("設計編號值")
.Selection.Text = Sheets("資料").Range("E" & i).Text
.Selection.HomeKey Unit:=6
Loop
Do While .Selection.Find.Execute("專案編碼")
.Selection.Text = Sheets("資料").Range("F" & i).Text '替換字串
.Selection.HomeKey Unit:=6
Loop
Do While .Selection.Find.Execute("月份一")
.Selection.Text = Sheets("資料").Range("G" & i).Text '替換字串
.Selection.HomeKey Unit:=6
Loop
Do While .Selection.Find.Execute("月份二")
.Selection.Text = Sheets("資料").Range("H" & i).Text '替換字串
.Selection.HomeKey Unit:=6
Loop
Do While .Selection.Find.Execute("工程描述")
.Selection.Text = Sheets("資料").Range("I" & i).Text '替換字串
.Selection.HomeKey Unit:=6
Loop
Do While .Selection.Find.Execute("工程總投資值")
.Selection.Text = Sheets("資料").Range("J" & i).Text '替換字串
.Selection.HomeKey Unit:=6
Loop
Do While .Selection.Find.Execute("安裝工程費值")
.Selection.Text = Sheets("資料").Range("K" & i).Text '替換字串
.Selection.HomeKey Unit:=6
Loop
Do While .Selection.Find.Execute("設備購置費值")
.Selection.Text = Sheets("資料").Range("L" & i).Text '替換字串
.Selection.HomeKey Unit:=6
Loop
Do While .Selection.Find.Execute("勘察設計費值")
.Selection.Text = Sheets("資料").Range("M" & i).Text '替換字串
.Selection.HomeKey Unit:=6
Loop
Do While .Selection.Find.Execute("地勘費值")
.Selection.Text = Sheets("資料").Range("N" & i).Text '替換字串
.Selection.HomeKey Unit:=6
Loop
Do While .Selection.Find.Execute("檢測費值")
.Selection.Text = Sheets("資料").Range("O" & i).Text '替換字串
.Selection.HomeKey Unit:=6
Loop
Do While .Selection.Find.Execute("工程監理費值")
.Selection.Text = Sheets("資料").Range("P" & i).Text '替換字串
.Selection.HomeKey Unit:=6
Loop
Do While .Selection.Find.Execute("安全生產費值")
.Selection.Text = Sheets("資料").Range("Q" & i).Text '替換字串
.Selection.HomeKey Unit:=6
Loop
Do While .Selection.Find.Execute("機構審計費值")
.Selection.Text = Sheets("資料").Range("R" & i).Text '替換字串
.Selection.HomeKey Unit:=6
Loop
Do While .Selection.Find.Execute("基站機房裝修值")
.Selection.Text = Sheets("資料").Range("S" & i).Text '替換字串
.Selection.HomeKey Unit:=6
Loop
Do While .Selection.Find.Execute("外電引入值")
.Selection.Text = Sheets("資料").Range("T" & i).Text '替換字串
.Selection.HomeKey Unit:=6
Loop
Do While .Selection.Find.Execute("選址費值")
.Selection.Text = Sheets("資料").Range("U" & i).Text '替換字串
.Selection.HomeKey Unit:=6
Loop
Do While .Selection.Find.Execute("增值稅總計")
.Selection.Text = Sheets("資料").Range("W" & i).Text '替換字串
.Selection.HomeKey Unit:=6
Loop
Do While .Selection.Find.Execute("設備稅金")
.Selection.Text = Sheets("資料").Range("X" & i).Text
.Selection.HomeKey Unit:=6
Loop
Do While .Selection.Find.Execute("勘察稅金")
.Selection.Text = Sheets("資料").Range("Y" & i).Text '替換字串
.Selection.HomeKey Unit:=6
Loop
Do While .Selection.Find.Execute("地勘稅金")
.Selection.Text = Sheets("資料").Range("Z" & i).Text '替換字串
.Selection.HomeKey Unit:=6
Loop
Do While .Selection.Find.Execute("檢測費稅金")
.Selection.Text = Sheets("資料").Range("AA" & i).Text '替換字串
.Selection.HomeKey Unit:=6
Loop
Do While .Selection.Find.Execute("監理費稅金")
.Selection.Text = Sheets("資料").Range("AB" & i).Text '替換字串
.Selection.HomeKey Unit:=6
Loop
Do While .Selection.Find.Execute("審計費稅金")
.Selection.Text = Sheets("資料").Range("AC" & i).Text '替換字串
.Selection.HomeKey Unit:=6
Loop
Do While .Selection.Find.Execute("地址值")
.Selection.Text = Sheets("資料").Range("D" & i).Text '替換字串
.Selection.HomeKey Unit:=6
Loop
Do While .Selection.Find.Execute("經度值")
.Selection.Text = Sheets("資料").Range("B" & i).Text '替換字串
.Selection.HomeKey Unit:=6
Loop
Do While .Selection.Find.Execute("緯度值")
.Selection.Text = Sheets("資料").Range("C" & i).Text '替換字串
.Selection.HomeKey Unit:=6
Loop
Do While .Selection.Find.Execute("運營商值")
.Selection.Text = Sheets("資料").Range("AD" & i).Text '替換字串
.Selection.HomeKey Unit:=6
Loop
Do While .Selection.Find.Execute("塔桅資訊")
.Selection.Text = Sheets("資料").Range("AE" & i).Text '替換字串
.Selection.HomeKey Unit:=6
Loop
Do While .Selection.Find.Execute("其他費用值")
.Selection.Text = Sheets("資料").Range("V" & i).Text '替換字串
.Selection.HomeKey Unit:=6
Loop
Do While .Selection.Find.Execute("專案名稱") '查找到指定字串
.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader '設定位置在頁眉
.Selection.HomeKey Unit:=wdStory '游標置于檔案首
.Selection.Text = Sheets("資料").Range("A" & i).Text '替換字串
.Selection.HomeKey Unit:=6
Loop
End With
Word物件.Documents.Save
Word物件.Quit
Set Word物件 = Nothing
Next i
End Sub
uj5u.com熱心網友回復:
With .ActiveDocument.Sections(4).Headers(wdHeaderFooterPrimary)
前面加個點,所有訪問的物件必須是 Word物件 的下屬,不能呼叫全域物件。
否則第一次創建的 Word物件 就作為全域實體保留,第二次呼叫再創建一個 Word物件;
這時下斷點,你在任務管理器會看到兩個 WINWORD.EXE 行程;
然后你用第二個 Word 實體打開檔案,訪問的卻是第一個 Word 實體的 ActiveDocument,當然會出錯了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/65405.html
標籤:VB基礎類
