1

最適化のためにマクロ コードを変更することを考慮します。私のマクロは、Excel ファイルの各行に対して新しいドキュメントを作成し、それを行 j の 2 列目の名前で保存します。 5行のExcelファイル、複数のユーザーがいる5ワード文書と数が膨大になります!したがって、1 つの単語ドキュメントにまとめたいと思います。つまり、ドキュメントごとに新しい行を作成する代わりに、マクロは 1 つのドキュメントに新しいページを作成する必要があり、その結果、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 
4

0 に答える 0