지금까지 문서를 구문 분석하고 두 제목 사이에 제목, 제목 및 텍스트를 가져 오는 작업 코드가 있습니다. 내가 추출하려고하는 내용은 글 머리 기호, 줄 바꿈 등이 있으며 셀에 붙여 넣을 때 형식을 유지하고 싶습니다. 주변을 둘러보고 많은 포럼을 읽었지 만 서식을 그대로 유지하는 방법을 찾아 낼 수 없었습니다. 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 2007에 저장할 수있는 최대 텍스트 길이를 알고 계십니까? 예 : Excel 2007의 경우 32767 자입니다. – PatricK