简体   繁体   English

Excel VBA复制范围(左侧的所有单元格)到嵌入式Word文档

[英]Excel VBA copy range (all cells from the left) to embedded Word document

(This question is a follow-up on how to work with a document embedded in an Excel workbook in the Word application interface (instead of in-place). The reason this is necessary is to be able to save the result as an independent document.) (此问题是有关如何使用 Word应用程序界面(而不是就地)中嵌入Excel工作簿中的文档的后续操作。之所以这样做,是因为能够将结果另存为独立文档。)

The problem I came up with is that some cells does not fit to criteria cell.Offset(0, -3).Text . 我想到的问题是某些单元格不符合条件cell.Offset(0, -3).Text So text is not only in one cell with Offset(0, -3) and in Word they should look like: 因此,文本不仅在Offset(0, -3)一个单元格中,而且在Word中,它们应类似于:

Legal             John Smith
                  Telephone         +4854132155
                  Email             john.smith@mail.com

In Excel they are in separate rows. 在Excel中,它们位于单独的行中。

  • "Legal" is in B50. “法律”在B50中。
  • "John Smith" is in C50. “ John Smith”在C50中。
  • "Telephone" is in C51. C51中有“电话”。
  • "+4854132155" is in D51. D51中有“ +4854132155”。
  • "Email" is in C52. “电子邮件”在C52中。
  • "john.smith@mail.com" is in C52. “ john.smith@mail.com”在C52中。

The idea is to have a "Case" called "table" so code will understand that it should copy everything from left of Case "table" row by row (word "table" is located in Column E). 这个想法是要有一个名为“ table”的“ Case”,因此代码将理解它应该逐行复制Case“ table”左侧的所有内容(单词“ table”位于E列)。 Paste to Word as a table and do wdAutoFitWindow so that table would fit perfectly to Word Document Window. 以表格形式粘贴到Word并执行wdAutoFitWindow以使表格完全适合Word文档窗口。

How data looks in Excel: 数据在Excel中的外观:

   A    B                   C                   D                  E
49    Paragraph with number 1                                    main
48    Ok text is text and it is good to have here.. a lot of     normal
50    Legal             John Smith                               table
51                      Telephone         +4854132155            table 
52                      Email             john.smith@mail.com    table
53    Paragraph with number 2                                    main
54    Text again a lot of text again comes here                  normal

What is wrong with current code: Current code runs smoothly. 当前代码有什么问题:当前代码运行平稳。 However once it reaches Excel row with word "table" in Column E, it deletes everything already inserted and inserts only last row with parameter "table". 但是,一旦到达E列中带有单词“ table”的Excel行,它将删除所有已插入的内容,并仅插入带有参数“ table”的最后一行。 So basically it is doing everything right until Case "table". 因此从根本上说,它一直在做到Case“ table”为止的所有事情。 Then as an output you will get: 然后作为输出,您将获得:

              Email             john.smith@mail.com

2   Paragraph with number                                   
    Text again a lot of text again comes here     

Instead of: 代替:

1   Paragraph with number                                   
    Ok text is text and it is good to have here.. a lot of 
    Legal          John Smith                             
                   Telephone         +4854132155              
                   Email             john.smith@mail.com
2   Paragraph with number                                   
    Text again a lot of text again comes here        

Here is the part I have been tried to implement: 这是我尝试实现的部分:

Set xlSht = Sheets("Offer Letter")
  For Each cell In xlRng
    wdRng.InsertAfter vbCr & cell.Offset(0, -3).Text
     Select Case LCase(cell.Value)
        Case "title"
          wdRng.Paragraphs.Last.Style = .Styles("Heading 1")
        Case "main"
          wdRng.Paragraphs.Last.Style = .Styles("Heading 2")
        Case "sub"
          wdRng.Paragraphs.Last.Style = .Styles("Heading 3")
        Case "sub-sub"
          wdRng.Paragraphs.Last.Style = .Styles("Heading 4")
        Case "normal"
          wdRng.Paragraphs.Last.Style = .Styles("Normal")
        Case "contact"
          wdRng.Paragraphs.Last.Style = .Styles("Contact")
          Case "attachment"
          wdRng.Paragraphs.Last.Style = .Styles("Attachment")
          Case "table"

                  xlSht.Range(cell.Offset(0, -3), cell.Offset(0, -1)).Copy
     wdRng.PasteExcelTable False, False, False

      wdRng.Tables(1).AutoFitBehavior wdAutoFitWindow

    End Select
  Next cell

It appears you should be using: 看来您应该使用:

  With wdRng
    .Paragraphs.Last.Range.PasteExcelTable False, False, False
    .Tables(.Tables.Count).AutoFitBehavior wdAutoFitWindow
    .Tables(.Tables.Count).Range.Style = "Normal"
  End With

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

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