简体   繁体   English

VBA将Excel表复制并粘贴到Word文档中

[英]VBA Copy and Paste Excel Table into Word Document

The code I'm writing is taking the used range (excluding the first two rows) of a worksheet and copying that table (or tables) onto its own page in a word document. 我正在编写的代码将使用工作表的使用范围(不包括前两行),并将该表(或多个表)复制到Word文档中自己的页面上。 The code was working fine until I had to make some changes to it... Right now the problem is that it is looping through all the worksheets in the workbook, but re-pasting the contents of the last worksheet only. 在我不得不对其进行一些更改之前,代码工作正常。现在的问题是,它遍历了工作簿中的所有工作表,但仅重新粘贴了最后一个工作表的内容。 Also, I can't seem to get VBA to recognize the pasted tables as tables -- so it isn't letting me center them in the word document. 另外,我似乎无法让VBA将粘贴的表识别为表-因此,这并不能让我将它们放在Word文档中。 Any ideas for how I can solve these problems? 关于如何解决这些问题的任何想法? Thanks in advance. 提前致谢。

Sub toWord()
Dim ws As Worksheet
Dim fromWB As Variant
Dim wdApp As Object
Dim wdDoc As Object
Dim docName As Variant
Dim rng As Range

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False


Set wdApp = CreateObject("Word.Application")
wdApp.Visible = True
Set wdDoc = wdApp.Documents.Add
wdDoc.Activate
'Creates InputBox that allows user to enter name to save document as
docName = Application.InputBox(Prompt:="Enter Document Name", Title:="Save Word Document", Type:=2)
wdDoc.SaveAs2 fileName:=docName, FileFormat:=wdFormatDocument 'Saves document under user-provided name

fromWB = Application.GetOpenFilename(FileFilter:="Excel Workbook(*.xlsx),*.xlsx", Title:="Open Merged Data")
If fromWB <> False Then
Set fromWB = Workbooks.Open(fromWB)
Set fromWB = ActiveWorkbook
ElseIf fromWB = False Then
    MsgBox "No File Selected"
    GoTo ResetSettings
End If


For Each ws In fromWB.Worksheets
    Range("A2").CurrentRegion.Offset(2).Resize(Range("A2").CurrentRegion.Rows.Count - 2).Select
    Selection.Copy
    Set wdApp = GetObject(, "Word.Application")
    wdApp.Visible = True
    wdDoc.Activate
    wdDoc.Range(wdDoc.Characters.Count - 1).Paste
    wdDoc.Range(wdDoc.Characters.Count - 1).InsertBreak Type:=7
    ActiveDocument.Tables(1).Select
    Selection.Tables(1).Rows.Alignment = wdAlignRowCenter
Next ws
   wdDoc.Styles("Normal").NoSpaceBetweenParagraphsOfSameStyle = True
wdDoc.Save
Set wdDoc = Nothing
Set wdApp = Nothing
MsgBox "Imported into Word Document"


ResetSettings:
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True

End Sub

The issues I see with your code are:- 我在您的代码中看到的问题是:-

  1. Set fromWB = ActiveWorkbook is superfluous and can be removed Set fromWB = ActiveWorkbook是多余的,可以删除
  2. You are referencing Range without a worksheet prefix, so it will always default to the current active worksheet (which is the one that would be visible when you open the workbook). 您引用的Range没有工作表前缀,因此它将始终默认为当前活动的工作表(这是打开工作簿时可见的工作表)。 Normally I would say to do this: ws.Range("A2") but in this case, I would suggest ws.Activate as the first statement inside your For Each loop since you are using physical actions such as copy/paste. 通常,我会说要这样做: ws.Range("A2")但是在这种情况下,我建议将ws.Activate作为For Each循环中的第一条语句,因为您正在使用诸如复制/粘贴之类的物理操作。
  3. To be a good resource citizen, you should be quitting Excel and setting fromWB = Nothing to release the memory. 要成为一个好的资源公民,您应该退出Excel并设置fromWB = Nothing来释放内存。 In fact, in VBA, you should be setting all assigned object variables to Nothing before you leave the method. 实际上,在VBA中,应该在离开方法之前将所有分配的对象变量设置为Nothing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM