(此问题是在Word应用程序界面中的how to work with a document embedded in an Excel workbook(而不是就地)的后续活动。之所以这样做是因为能够将结果保存为独立文档。)< / p>
我想到的问题是某些单元格不符合条件cell.Offset(0, -3).Text
。因此,文本不仅使用Offset(0, -3)
放在一个单元格中,而且在Word中看起来应该像这样:
Legal John Smith
Telephone +4854132155
Email john.smith@mail.com
在Excel中,它们位于单独的行中。
这个想法是要有一个名为“ table”的“ Case”,因此代码将理解它应该逐行复制Case“ table”左侧的所有内容(单词“ table”位于E列)。粘贴到Word中作为表格并执行wdAutoFitWindow
,以使表格完全适合Word文档窗口。
数据在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
当前代码有什么问题:当前代码运行平稳。但是,一旦到达E列中带有单词“ table”的Excel行,它将删除所有已插入的内容,并仅插入带有参数“ table”的最后一行。因此从根本上说,它一直在做到Case“ table”为止的所有事情。然后作为输出,您将得到:
Email john.smith@mail.com
2 Paragraph with number
Text again a lot of text again comes here
代替:
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
这是我尝试实现的部分:
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
答案 0 :(得分:1)
您似乎应该使用:
With wdRng
.Paragraphs.Last.Range.PasteExcelTable False, False, False
.Tables(.Tables.Count).AutoFitBehavior wdAutoFitWindow
.Tables(.Tables.Count).Range.Style = "Normal"
End With