2013-08-29 48 views
1

到目前爲止,我已經接近解析文檔並獲取兩個標題之間的標題,標題和文本的工作代碼。我試圖提取的內容包括子彈,換行符等,我希望在將它粘貼到單元格中時保留該格式。一直在環顧四周,閱讀很多論壇,但無法弄清楚如何保持格式不變。我查看了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 

Link to Sample Doc

+0

保持格式化的最佳方式是......複製並粘貼,不幸的是。因此,首先嚐試充分探索這個方向。顯然,它不是唯一的選擇,但另一個選項會使您的代碼翻倍(甚至更多)。鏈接到您的文件不工作,要求登錄:( –

+0

感謝您的答覆。我試過複製/粘貼,但我遇到的問題是文本傳播在多個單元格。在Excel中,我希望1.1和1.2之間的一切放入一個單元格中,並保留一定的格式(至少在沒有任何內容的情況下會斷行)。下面鏈接到Word Doc的工作不需要登錄: https://docs.google.com/file/d/0B_UNDFf6UzJHZHk3VC0xelFnV0U/編輯?usp = sharing – user2723524

+0

您是否知道可以在Excel單元格中存儲的文本的最大長度?例如Excel 2007中的32767個字符。 – PatricK

回答

1

你可能找到了一個解決方案現在,但我會做的是打開的Excel ,開始宏錄製,然後選擇一個單元格,單擊圖標展開單元格輸入字段,然後粘貼一些格式化的文本。然後停止宏並查看代碼。關鍵是粘貼到頂部的單元格區域。抓住你的單詞宏所需的一點代碼。希望這可以幫助。

+1

這可能更適合作爲註釋。 – Chrismas007