我考虑修改我的宏代码以进行优化,因为:我的宏为 excel 文件的每一行创建一个新文档并将其保存在第 j 行的第二列的名称下 .. 结果我得到一个excel文件5行,5word文档,多用户,数量庞大!所以我想在一个单字文档中组装,我的意思是,宏应该在单个文档中创建一个新页面,而不是为每个文档创建一个新行,结果我只会得到 1 个单词文档 5 页在线获取excel文件5。是 - 我可以请吗?
代码:
Sub MacroAutoJB()
Dim WordApp As Word.Application
Dim WordDoc As Word.Document
Dim oWdApp As Object
Dim i As Byte
Dim sChemin As String
Dim wb As Workbook
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
On Error Resume Next
Dim nom As String
Dim sName As String
Dim sPath As String
On Error Resume Next
Dim j As Integer
j = ActiveSheet.UsedRange.Rows.Count 'count number of lines used in the file
Dim n As Byte
n = Cells(1, Columns.Count).End(xlToLeft).Column
If ActiveWorkbook.Name Like "WPaie*.xls" Then
user = Environ("username")
sName = ActiveWorkbook.Name
sPath = "C:\Documents and Settings\" & user & "\My Documents\"
sName = Replace(sName, ".xls", "_Word")
MkDir sName
For j = 2 To j 'start the loop the opeation until the next will be for each line used in the file
Set WordApp = CreateObject("word.application")
nom = Sheets(1).Cells(j, 2)
mail = Sheets(1).Cells(2, n)
Set oWdApp = CreateObject("Word.Application")
Set WordDoc = oWdApp.Documents.Open("C:\Documents and Settings\" & user & "\Class.doc")
For i = 1 To n - 1
WordDoc.Bookmarks("Sig" & i).Range.Text = Cells(j, i)
Next i
WordDoc.Bookmarks("Signet").Range.Text = Cells(j, 2)
WordDoc.Bookmarks("Sigmail").Range.Text = Cells(j, n)
WordDoc.SaveAs Filename:=sPath & sName & "\" & nom & ".doc"
WordApp.Visible = False
oWdApp.Quit
ActiveDocument.Close True
WordApp.Quit
Next j
ActiveWorkbook.Close