filmov
tv
Extract Data from Word Document Using Word and Excel VBA

Показать описание
How to extract data from a Word document using both Word VBA and Excel VBA.
Here's the complete code:
Sub ExtractText()
Dim cDoc As Word.Document
Dim cRng As Word.Range
Dim i As Long
i = 2
Dim wordapp As Object
Set wordapp = CreateObject("word.Application")
wordapp.Visible = True
Set cDoc = ActiveDocument
Set cRng = cDoc.Content
With cRng.Find
.Forward = True
.Text = "["
.Wrap = wdFindStop
.Execute
Do While .Found
'Collapses a range or selection to the starting or ending position
cRng.MoveEndUntil Cset:="]"
Cells(i, 1) = cRng
.Execute
i = i + 1
Loop
End With
wordapp.Quit
Set wordapp = Nothing
End Sub
Option Explicit
Sub ExtractText()
Dim cDoc As Word.Document, nDoc As Word.Document
Dim cRng As Word.Range, nRng As Word.Range
Set cDoc = ActiveDocument
Set nDoc = Documents.Add
Set cRng = cDoc.Content
Set nRng = nDoc.Content
cRng.Find.ClearFormatting
With cRng.Find
.Forward = True
.Text = "["
.Wrap = wdFindStop
.Execute
Do While .Found
nRng.FormattedText = cRng.FormattedText
nRng.InsertParagraphAfter
.Execute
Loop
End With
End Sub
Reference:
Here's the complete code:
Sub ExtractText()
Dim cDoc As Word.Document
Dim cRng As Word.Range
Dim i As Long
i = 2
Dim wordapp As Object
Set wordapp = CreateObject("word.Application")
wordapp.Visible = True
Set cDoc = ActiveDocument
Set cRng = cDoc.Content
With cRng.Find
.Forward = True
.Text = "["
.Wrap = wdFindStop
.Execute
Do While .Found
'Collapses a range or selection to the starting or ending position
cRng.MoveEndUntil Cset:="]"
Cells(i, 1) = cRng
.Execute
i = i + 1
Loop
End With
wordapp.Quit
Set wordapp = Nothing
End Sub
Option Explicit
Sub ExtractText()
Dim cDoc As Word.Document, nDoc As Word.Document
Dim cRng As Word.Range, nRng As Word.Range
Set cDoc = ActiveDocument
Set nDoc = Documents.Add
Set cRng = cDoc.Content
Set nRng = nDoc.Content
cRng.Find.ClearFormatting
With cRng.Find
.Forward = True
.Text = "["
.Wrap = wdFindStop
.Execute
Do While .Found
nRng.FormattedText = cRng.FormattedText
nRng.InsertParagraphAfter
.Execute
Loop
End With
End Sub
Reference:
Комментарии