|
现在的code替换模板正文内的文字没问题,但是不知道如何能够替换页眉的文字
有没有大佬能帮忙!
Sub Generate_word()
On Error GoTo Err_cmdExportToWord_Click
Dim objApp As Object 'Word.Application
Dim objDoc As Object 'Word.Document
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim strTemplates As String
Dim strFileName As String
Dim strData As String
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim Num As String
Dim Name As String
Dim Entity As String
Dim Chiname As String
Dim Engname As String
Dim data_areas As Range
Dim total_data As Integer
Dim result As String
Dim n As Long
Set data_areas = Application.InputBox(prompt:="Please choose the data", Title:="Data", Type:=8)
i = data_areas.Row
j = data_areas.Rows.Count
over4Names = ""
With Application.FileDialog(msoFileDialogFilePicker)
.Filters.Add "word", "*.doc*", 1
.AllowMultiSelect = False
If .Show Then strTemplates = .SelectedItems(1) Else Exit Sub
End With
With Application.FileDialog(msoFileDialogFilePicker)
.Filters.Add "excel", "*.xls*", 1
.AllowMultiSelect = False
If .Show Then strData = .SelectedItems(1) Else Exit Sub
End With
With Application.FileDialog(msoFileDialogFolderPicker)
Path = ThisWorkbook.Path
End With
With Application
.DisplayAlerts = False
.ScreenUpdating = False
End With
Set objApp = CreateObject("Word.Application")
objApp.Visible = False
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open(strData)
xlApp.Visible = False
Set xlSheet = xlBook.Worksheets(1)
nameArray = xlSheet.Range("D1:D" & xlSheet.Cells(Rows.Count, "D").End(xlUp).Row).Value
For k = i To i + j - 1
Num = Cells(k, 1)
Name = Cells(k, 2)
Entity = Cells(k, 3)
Chiname = Cells(k, 4)
Engname = Cells(k, 5)
Set objDoc = objApp.Documents.Open(strTemplates, , False)
strFileName = "Independence Declaration" & "-" & Entity & ".doc"
If Not strFileName Like "*.doc" Then strFileName = strFileName & ".doc"
If Dir(strFileName) <> "" Then Kill strFileName
With objApp.Application.Selection
.Find.ClearFormatting
.Find.Replacement.ClearFormatting
’下面这段是本来用于替换页眉的代码,思路和替换正文部分的代码一致,但发现替代不了,所以暂时注释掉了
'ˉWith .Find
' .Text = "{$Entity}"
' .Replacement.Text = Entity
' End With
' .Find.Execute Replace:=wdReplaceAll
With .Find
.Text = "{$Name}"
.Replacement.Text = Name
End With
.Find.Execute Replace:=wdReplaceAll
With .Find
.Text = "{$Chiname}"
.Replacement.Text = Chiname
End With
.Find.Execute Replace:=wdReplaceAll
With .Find
.Text = "{$Engname}"
.Replacement.Text = Engname
End With
.Find.Execute Replace:=wdReplaceAll
End With
objDoc.SaveAs Path & "\" & strFileName
objDoc.Saved = True
Next
objDoc.Close
With Application
.DisplayAlerts = True
.ScreenUpdating = True
End With
result = "Generation Completed"
MsgBox result, vbYes + vbExclamation
Exit_cmdExportToWord_Click:
objApp.Quit
Set objApp = Nothing
Set objDoc = Nothing
Set objTable = Nothing
Set xlApp = Nothing
Set xlBook = Nothing
Set xlSheet = Nothing
Exit Sub
Err_cmdExportToWord_Click:
MsgBox Err.Description, vbCritical, "Error"
Resume Exit_cmdExportToWord_Click
End Sub
|
|