Extract Data from Word Document Using Word and Excel VBA
Dinesh Kumar Takyar
0:00 / 0:00
Extract Data from Word Document Using Word and Excel VBA
21 649 просмотров · 6 лет назад
Dinesh Kumar Takyar
112 тыс. подписчиков
21 649 просмотров · 6 лет назад
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.Documents.Open "c:\bracketdata\bracket-data.docx"
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.Collapse Word.WdCollapseDirection.wdCollapseEnd
cRng.MoveEndUntil Cset:="]"
Cells(i, 1) = cRng
cRng.Collapse Word.WdCollapseDirection.wdCollapseEnd
.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
cRng.Collapse Word.WdCollapseDirection.wdCollapseEnd
cRng.MoveEndUntil Cset:="]", Count:=Word.wdForward
nRng.FormattedText = cRng.FormattedText
nRng.InsertParagraphAfter
nRng.Collapse Word.WdCollapseDirection.wdCollapseEnd
cRng.Collapse Word.WdCollapseDirection.wdCollapseEnd
.Execute
Loop
End With
End Sub
Reference:
https://social.msdn.microsoft.com/For...