2012-09-28 48 views
0

我有兩個部分工作位的代碼放在一起。將整個工作表內容複製到已打開的Word文檔中

我有一個標籤爲'word'的工作表,我想導出並自動保存在一個變量下。

Sub CreateNewWordDoc() 

Dim wrdApp As Word.Application 
Dim wrdDoc As Word.Document 
Dim i As Integer 
docname = Worksheets("input").Range("b10").Value 

Data1 = Worksheets("word").Range("a1:d103").Value 
Set wrdApp = CreateObject("Word.Application") 
wrdApp.Visible = True 
Set wrdDoc = wrdApp.Documents.Open("C:\Results\ResultsTemplate.doc") 


'******THIS IS TO EDIT THE WORD DOCUMENT****** 
With Worksheets("word") 
    CopyRangeToWord wdDoc, .Range("A1:d104") 



'******THIS IS THE END TO EDIT THE WORD DOCUMENT***** 


    If Dir("C:\Results\" & docname & ".doc") <> "" Then 
     Kill "C:\Results\" & docname & ".doc" 
    End If 
    .SaveAs ("C:\Results\" & docname & ".doc") 
    .Close ' close the document 
End With 
wrdApp.Quit ' close the Word application 
Set wrdDoc = Nothing 
Set wrdApp = Nothing 
End Sub 

我喜歡這第一個最好的。它會打開我的模板,其中包含這些生成的報告所需的所有官方信息(公司信息等),並將自動保存並使用正確的文件名關閉。但是,我無法找到一種方法將它從工作表'單詞'中的所有信息複製到文檔的文本正文中。它正在保存一個空白文檔。

進行故障診斷時,我碰到這個代碼就來了:

Private Sub CopyRangeToWord(ByRef wdDoc As Word.Document, rng_to_copy As Range, Optional page_break As Boolean = True) 
' Will copy the range given into the word document given. 
Application.StatusBar = "Copying data from " & rng_to_copy.Parent.Name & "..." 
rng_to_copy.Copy 
wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.InsertParagraphAfter 
wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.Paste 
Application.CutCopyMode = False 
wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.InsertParagraphAfter 
' insert page break after all worksheets except the last one 
If page_break Then 
    With wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range 
     .InsertParagraphBefore 
     .Collapse Direction:=wdCollapseEnd 
     .InsertBreak Type:=wdPageBreak 
    End With 
End If 

End Sub 


Sub CopyWorksheetsToWord() 

    Dim wdApp As Word.Application, wdDoc As Word.Document, ws As Worksheet 
    Application.ScreenUpdating = False 
    Application.StatusBar = "Creating new document..." 
    Set wdApp = New Word.Application 
    Set wdDoc = wdApp.Documents.Add 
    docname = Worksheets("input").Range("b10").Value 

    With Worksheets("word") 
     CopyRangeToWord wdDoc, .Range("A1:d104") 

    End With 

    Set ws = Nothing 
    Application.StatusBar = "Cleaning up..." 
    'apply normal view 
    With wdApp.ActiveWindow 
     If .View.SplitSpecial = wdPaneNone Then 
      .ActivePane.View.Type = wdNormalView 
     Else 
      .View.Type = wdNormalView 
     End If 
    End With 

    Set wdDoc = Nothing 
    wdApp.Visible = True 
    Set wdApp = Nothing 
    Application.StatusBar = False 

End Sub 

它做的第一代碼的完全相反:它會打開一個新的文檔(不是模板),將所有數據完全複製,但不會保存或關閉正確的文件名。

我猜測,更新代碼第一節複製工作表內容會更容易,而且我更喜歡。

回答

1
Private Sub CopyRangeToWord(ByRef wdDoc As Word.Document, rng_to_copy As Range, Optional page_break As Boolean = True) 
' Will copy the range given into the word document given. 
    Application.StatusBar = "Copying data from " & rng_to_copy.Parent.Name & "..." 
    rng_to_copy.Copy 
    wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.InsertParagraphAfter 
    wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.Paste 
    Application.CutCopyMode = False 
    wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range.InsertParagraphAfter 
    ' insert page break after all worksheets except the last one 
    If page_break Then 
     With wdDoc.Paragraphs(wdDoc.Paragraphs.Count).Range 
      .InsertParagraphBefore 
      .Collapse Direction:=wdCollapseEnd 
      .InsertBreak Type:=wdPageBreak 
     End With 
    End If 


End Sub 

Sub CopyWorksheetsToWord() 

Dim wdApp As Word.Application, wdDoc As Word.Document, ws As Worksheet 
    Application.ScreenUpdating = False 
    Application.StatusBar = "Creating new document..." 
    Set wdApp = New Word.Application 
    Set wdDoc = wdApp.Documents.Add 
    docname = Worksheets("input").Range("b10").Value 


    With Worksheets("word") 
     CopyRangeToWord wdDoc, .Range("A1:d104") 

    With wdDoc 
    .SaveAs ("C:\Results\" & docname & ".doc") 
    .Close 
    End With 

    End With 

End Sub 

這個工作原理:但不打開我的模板。儘管如此 - 它將從一個工作表創建一個文檔並自動將其保存到具有在定義的單元格中引用的文件名的目錄中。

相關問題