简体   繁体   English

提取Excel表范围并复制到新的Word文档中

[英]extract the excel table range and copy into a new word document

I want to copy a excel range into a new word document .Some range that I don't really want to remain here so I hide those certain rows first manually .Then ,I will run my vb program and paste into a new word document automatically . 我想将一个excel范围复制到一个新的word文档中。我实际上并不想保留某些范围,因此我先手动隐藏了某些行。然后,我将运行vb程序并自动粘贴到一个新的word文档中。

However ,I copy the range and paste into the new word document in a picture format . 但是,我复制范围并以图片格式粘贴到新的Word文档 I want to paste into a word table format .But please remained that ,the word table format should best fit with the landscape A4 word format .How to do this ? 我想粘贴到单词表格式中。但是请注意,单词表格式应该最适合横向A4单词格式。如何执行此操作?

Here is my code : 这是我的代码:

    Sub gen()


    Dim tbl0 As Excel.RANGE
    Dim Tbl As Excel.RANGE
    Dim tbl2 As Excel.RANGE

    Dim wordApp As Word.Application
    Dim myDoc As Word.Document
    Dim WordTable As Word.Table
    Dim wb As Workbook
    Dim ws As Worksheet

    Set wb = ThisWorkbook
    Set ws = wb.Worksheets("17-18")             ' Change e.g. sheet9.Name
    'Optimize Code
    Application.ScreenUpdating = False
    Application.EnableEvents = False

'Value1 = Me.TextBox1.Value
'Value2 = Me.TextBox2.Value
    'ws.Rows("84:89").EntireRow.Hidden = True 'ADJUST AS APPROPRIATE

    'Copy Range from Excel
    'Set tbl0 = ws.RANGE("A78:I83")
    'Set Tbl = ws.RANGE(Value1, Value2)
    Set Tbl = ws.RANGE(Selection.Address(ReferenceStyle:=xlA1, _
                           RowAbsolute:=False, ColumnAbsolute:=False))


    ' Set tbl2 = ws.Range("A90:I92")

    'Create an Instance of MS Word
    On Error Resume Next

    'Is MS Word already opened?
    Set wordApp = GetObject(Class:="Word.Application")

    'Clear the error between errors
    Err.Clear

    'If MS Word is not already open then open MS Word
    If wordApp Is Nothing Then Set wordApp = CreateObject(Class:="Word.Application")

    'Handle if the Word Application is not found
    If Err.Number = 429 Then
        MsgBox "Microsoft Word could not be found, aborting."
        GoTo EndRoutine
    End If

    On Error GoTo 0

    'Make MS Word Visible and Active
    wordApp.Visible = True
    wordApp.Activate

    'Create a New Document
    Set myDoc = wordApp.Documents.Add

    'Trigger copy separately for each table + paste for each table

    Tbl.CopyPicture Appearance:=xlScreen, Format:=xlPicture

    wordApp.Selection.Paste
    wordApp.Selection.TypeParagraph

    wordApp.Selection.PageSetup.Orientation = wdOrientLandscape

   ' resize_all_images_to_page_width myDoc

EndRoutine:
    'Optimize Code
    Application.ScreenUpdating = True
    Application.EnableEvents = True

    'Clear The Clipboard
    Application.CutCopyMode = False

ws.Rows.EntireRow.Hidden = False
End Sub

First of all, you need to trigger standard copy but not .CopyPicture method : 首先,您需要触发标准副本,而不是.CopyPicture method

'Tbl.CopyPicture Appearance:=xlScreen, Format:=xlPicture 'this line ...
Tbl.Copy '...replace with this line

Next, you can trigger .PasteExcelTable method like this: 接下来,您可以触发.PasteExcelTable method如下所示:

'wordApp.Selection.Paste 'instead of this line...
'...try this one...
wordApp.Selection.PasteExcelTable LinkedToExcel:=False, _
                            WordFormatting:=True, _
                            RTF:=True

Please make some tests with both WordFormattin and RTF parameters. 请同时使用WordFormattinRTF参数进行一些测试。 Depending on True or False you can have slightly different results. 根据True or False您可能会得到略有不同的结果。 Proposed solution will try to paste in the way to fit current page layout. 建议的解决方案将尝试以适合当前页面布局的方式进行粘贴。 But if source table is too wide or too high it could not work without additional adjustments. 但是,如果源表太宽或太高,那么在没有其他调整的情况下将无法正常工作。

Please give this a try... 请尝试一下...

wordApp.Visible = True
wordApp.Activate

'Create a New Document
Set myDoc = wordApp.Documents.Add

'Copy the table
tbl.Range.Copy

'Paste the table into the document as a table
myDoc.Range.PasteExcelTable False, True, False
myDoc.Range.InsertParagraphAfter
myDoc.PageSetup.Orientation = 1

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

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