如何将格式化的段落从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
,并且尝试了各种Paste:=,而在Sheets端尝试了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\testdoc.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段落对象)的所有属性 all .
- 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屋!