【发布时间】:2015-03-02 10:04:58
【问题描述】:
到目前为止,我已经接近了解析文档并获取两个标题之间的标题、标题和文本的工作代码。我试图提取的内容有项目符号、换行符等,我想在将其粘贴到单元格时保持格式。一直在环顾四周并阅读很多论坛,但无法弄清楚如何保持格式不变。我查看了 PasteSpecial ,但它会将内容粘贴到多个单元格中,而且我希望尽可能避免复制/粘贴。
下面是我早期的代码(有一些我正在调试/修复的错误):
Sub GetTextFromWord()
Dim Paragraph As Object, WordApp As Object, WordDoc As Object
Dim para As Object
Dim paraText As String
Dim outlineLevel As Integer
Dim title As String
Dim body As String
Dim myRange As Object
Dim documentText As String
Dim startPos As Long
Dim stopPos As Long
Dim file As String
Dim i As Long
Dim category As String
startPos = -1
i = 2
Application.ScreenUpdating = True
Application.DisplayAlerts = False
file = "C:\Sample.doc"
Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True
Set WordDoc = WordApp.Documents.Open(file)
Set myRange = WordDoc.Range
documentText = myRange.Text
For Each para In ActiveDocument.Paragraphs
' Get the current outline level.
outlineLevel = para.outlineLevel
' Cateogry/Header begins outline level 1, and ends at the next outline level 1.
If outlineLevel = wdOutlineLevel1 Then 'e.g., 1 Header
category = para.Range.Text
End If
' Set category as value for cells in Column A
Application.ActiveWorkbook.Worksheets("Sheet1").Cells(i - 1, 1).Value = category
' Title begins outline level 1, and ends at the next outline level 1.
If outlineLevel = wdOutlineLevel2 Then ' e.g., 1.1
' Get the title and update cells in Column B
title = para.Range.Text
Application.ActiveWorkbook.Worksheets("Sheet1").Cells(i, 2).Value = title
startPos = InStr(nextPosition, documentText, title, vbTextCompare)
If startPos <> stopPos Then
' this is text between the two titles
body = Mid$(documentText, startPos, stopPos)
ActiveSheet.Cells(i - 1, 3).Value = body
End If
stopPos = startPos
i = i + 1
End If
Next para
WordDoc.Close
WordApp.Quit
Set WordDoc = Nothing
Set WordApp = Nothing
End Sub
【问题讨论】:
-
保留格式的最佳方法是...复制和粘贴,不幸的是。所以,首先尝试充分探索这个方向。显然这不是唯一的选择,但另一个选项将使您的代码增加两倍(甚至更多)。链接到您的文件无效,要求登录:(
-
感谢您的回复。我尝试复制/粘贴,但遇到的问题是文本跨多个单元格传播。在 excel 中,我希望将 1.1 和 1.2 之间的所有内容放入一个单元格中,并保留一定数量的格式(如果没有,至少换行)。下面的 Word Doc 链接无需登录即可使用:docs.google.com/file/d/0B_UNDFf6UzJHZHk3VC0xelFnV0U/…
-
您知道 Excel 单元格中可以存储的最大文本长度吗?例如Excel 2007 中的 32767 个字符。