Extract Data from Word Document Using Word and Excel VBA

preview_player
Показать описание
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:
Рекомендации по теме
Комментарии
Автор

Hello Dinesh, your video is amazing and underrated! 👍

temuradhamson
Автор

Sorry for spelling mistake. Vlookup when data is in filter. Using match for headers. Ex: filter completed when we have to lookup for pending. But the columns are not in sequence.

ORANGEPICTURESOFFICIAL
Автор

Thank you sir,
This code running but,
Upgrade this code with
Add [ Text, image(png, jpg), maths equation, Entry button, etc ]

manishkasana
Автор

nice simple code that executes well, suggestion would be to remove any formatting from source document as a elements such as hyperlinks can break the code execution.

stclassmedia
Автор

I'm trying this on a macro enabled word document (docm files) which things should i change in this code to make it work?

shoutthesilence
Автор

Excellent video sir! saved hrs of my life! I
wish to extract highlighted data instead of data in brackets please guide!

nikitalakkundi
Автор

Thank you so much, ¡Brilliant!.
Anyway, please, could it be possible that the text format (from Word) will be transferred to the Excel? And if possible, could you teach the code to be added to the code in this video?
Tank you very much.
Cheers,
Alberto

AB-zwzg
Автор

Hey Thank you for this help. Stay Blessed.

usmansardarmuhammad
Автор

Use this exact code but I'm getting a "Compile error: User-defined type not defined" and it is referring to... cDoc As Word.Document

schmekis
Автор

Simply how to do vlookup function when any data is in filter. For visible cells only we have to lookup using match function for headers mapping.

ORANGEPICTURESOFFICIAL
Автор

Tqs for the reply sir. But there is no video for how to do vlookup for multer data using match.

ORANGEPICTURESOFFICIAL
Автор

Simply superb sir. Please do a video on filtered data. Vlookup. How to do visible cells vlookup. Please sir

ORANGEPICTURESOFFICIAL
Автор

Hello sir, I’m running into an issue here. When I ran the code for the word document, I could only run for the first page, my results from the second page is not showing up. Also my word document contains text that I want to search for and copy to excel. Is there a video of yours I could look at for more detail? Thanks for all your help in advance

snehachowdary
Автор

hi sir. I have a case. with a worksheet of multiple columns. each data in the column may refer to another sheets on a lan or share point are which need to be validated

second. if one cell value is given. based on cell value next column to be populated based on validation from different sheet. I may have 100 plus reference sheets where data to be validated.

is that a possible case. can you help me sir

srikanthkandalam
Автор

Can this be used to get some data from word and then add it to the meta data of the file? then adding it to say the "TAG" properties?

jorgan
Автор

Thanks a lot. That's really helpful

vigneshr
Автор

Can you show VBA How to auto transfer data to another sheet by using date?

អនុវិទ្យាល័យបឹងនិមល
Автор

can we export date from multiple word files, copying those to excel ?

anilkoli
Автор

Thanks for sharing... Great learning experience. Can it possible to extract a sentence after find a specific word on it?

siba
Автор

Hi Sir. I am running into an error while running this code. when I run the code first time it works fine but when I run it for the second time it throws the error on the below line

set cDoc=ActiveDocument

the error is read as "the remote server machine does not exist or unavailable."

Please advise how to remove this error.

shoaibsaifi
join shbcf.ru