如何将格式化的段落从 Word 2013 复制到 Excel? [英] How to copy a formatted paragraph from Word 2013 to Excel?
问题描述
我想打开一个 Word 文档并将每个格式化的段落复制到 Excel 工作表中的自己的单元格中,保留格式以供进一步处理.我已经能够将每个段落复制到自己的单元格中,甚至可以将项目符号/列表保留在相邻的单元格中.
I want to open a Word document and copy each formatted paragraph into its own cell in an Excel worksheet, retaining the formatting for further processing. I have been able to copy each paragraph into its own cell, and even have the bullet/list retained in an adjacent cell.
但是,我可以将段落复制为对象,而不是格式化文本,也可以复制为纯文本.我无法复制格式化的文本.
However, I can either have the paragraph copied as an object, not formatted text, or as plain text. I have not been able to get the formatted text copied over.
这是我进行复制的子程序代码:
Here is my Subroutine code that does the copying:
Private Sub Load_Schedule()
Dim ParaCount As Integer
Sheets(FileName).Activate
Sheets(FileName).Columns(1).AutoFit
For ParaCount = 1 To wDoc.Paragraphs.Count
wDoc.Paragraphs(ParaCount).Range.FormattedText.Copy
Sheets(FileName).Cells(ParaCount, 1).PasteSpecial Paste:=xlPasteFormats
Next ParaCount
End Sub
我在 wDoc 端尝试过 Range.Copy
并且在 Sheets 端尝试了各种 Paste:= 和 Cells(ParaCount, 1).Paste
.似乎没有什么将格式带入单元格.
I have tried Range.Copy
on the wDoc side and I have tried various Paste:= and just Cells(ParaCount, 1).Paste
on the Sheets side. Nothing seems to bring the formatting into the cell.
推荐答案
对此有几个技巧以及一些需要牢记的事项.一、代码:
There are a couple tricks to this and some things to keep in mind. First, the code:
Option Explicit
Sub ParaCopy()
Dim wApp As Word.Application
Dim wDoc As Word.Document
Set wApp = CreateObject("Word.Application")
Set wDoc = wApp.Documents.Open("C:Temp estdoc.docx", ReadOnly:=True)
Dim i As Long
i = 0
Dim wPara As Word.Paragraph
For Each wPara In wDoc.Paragraphs
If wPara.Range.Words.Count > 1 Then
wPara.Range.Copy
Sheet1.Range("A1").Offset(i, 0).Activate
Sheet1.Paste
i = i + 1
End If
Next wPara
wDoc.Close
wApp.Quit
End Sub
所以这从两个方面起作用:
So this works from two aspects:
- 我们正在使用 Word
Paragraph
的Range.Copy
方法.这会捕获段落的所有属性,包括格式. - 要复制到特定单元格中,这是需要使用
Activate
的少数情况之一.这向 Excel 发出信号,即将进行的Copy
操作将应用传入对象(在本例中为 Word 段落对象)的所有属性.
- We're using the
Range.Copy
method of the WordParagraph
. This captures all of the attributes of the paragraph including the formatting. - To copy into a specific cell, this is one of the rare instances in which using
Activate
is necessary. This signals to Excel that the upcomingCopy
operation will apply all of attributes of the incoming object (which is a Word paragraph object in this case).
确保您检查该段落中是否至少包含一个单词.否则粘贴操作会失败.
Make sure you check that the paragraph has at least one word in it. Otherwise the paste operation will fail.
这是我的测试 Word 文档:
Here's my test Word document:
这是粘贴后的工作簿 Sheet1:
And here is the workbook Sheet1 after the Paste:
这篇关于如何将格式化的段落从 Word 2013 复制到 Excel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!