到目前爲止,我已經接近解析文檔並獲取兩個標題之間的標題,標題和文本的工作代碼。我試圖提取的內容包括子彈,換行符等,我希望在將它粘貼到單元格中時保留該格式。一直在環顧四周,閱讀很多論壇,但無法弄清楚如何保持格式不變。我查看了PasteSpecial,但是將內容粘貼到多個單元格中,並且我希望儘可能避免複製/粘貼。使用VBA將文本從Word轉換爲Excel
下面是一個非常早期的代碼,我有(有我調試錯誤/固定):
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的工作不需要登錄: https://docs.google.com/file/d/0B_UNDFf6UzJHZHk3VC0xelFnV0U/編輯?usp = sharing – user2723524
您是否知道可以在Excel單元格中存儲的文本的最大長度?例如Excel 2007中的32767個字符。 – PatricK