Extract Data from Word Document Using Word and Excel VBA

Поділитися
Вставка
  • Опубліковано 5 жов 2024
  • 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:
    social.msdn.mi...

КОМЕНТАРІ • 44

  • @1stclassmedia
    @1stclassmedia Місяць тому

    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.

  • @manishkasana99
    @manishkasana99 Рік тому +1

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

  • @temuradhamson
    @temuradhamson 10 місяців тому

    Hello Dinesh, your video is amazing and underrated! 👍

  • @nikitalakkundi5755
    @nikitalakkundi5755 2 роки тому

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

    • @Exceltrainingvideos
      @Exceltrainingvideos  2 роки тому

      Glad it helped. This link will guide: www.exceltrainingvideos.com/identify-colored-cells-excel-worksheet-and-perform-actions/

  • @shoutthesilence9156
    @shoutthesilence9156 2 роки тому +1

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

  • @AB-zw3zg
    @AB-zw3zg 2 роки тому

    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

  • @Khalnayak_sanju143
    @Khalnayak_sanju143 4 роки тому

    Very nice and Brilliant code created

    • @Exceltrainingvideos
      @Exceltrainingvideos  4 роки тому

      Glad you like it! Please share the Excel VBA tutorial with your friends also.

  • @usmansardarmuhammad961
    @usmansardarmuhammad961 2 роки тому

    Hey Thank you for this help. Stay Blessed.

  • @ORANGEPICTURESOFFICIAL
    @ORANGEPICTURESOFFICIAL 4 роки тому

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

    • @Exceltrainingvideos
      @Exceltrainingvideos  4 роки тому

      Already uploaded. Search this channel or our website: www.exceltrainingvideos.com/

  • @johnmecham9855
    @johnmecham9855 Рік тому

    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

  • @ORANGEPICTURESOFFICIAL
    @ORANGEPICTURESOFFICIAL 4 роки тому

    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.

  • @snehachowdary1202
    @snehachowdary1202 3 роки тому

    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

    • @Exceltrainingvideos
      @Exceltrainingvideos  3 роки тому

      Please search the channel. To get data from multiple pages in your word document, you'll have to use a looping process.

  • @vigneshr2828
    @vigneshr2828 4 роки тому

    Thanks a lot. That's really helpful

    • @Exceltrainingvideos
      @Exceltrainingvideos  4 роки тому

      Glad it was helpful! Please share the VBA tutorial with your friends.

  • @ORANGEPICTURESOFFICIAL
    @ORANGEPICTURESOFFICIAL 4 роки тому

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

  • @anilkoli5190
    @anilkoli5190 3 роки тому

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

  • @srikanthkandalam4051
    @srikanthkandalam4051 4 роки тому

    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

  • @អនុវិទ្យាល័យបឹងនិមល

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

  • @shoryaarora1464
    @shoryaarora1464 4 роки тому

    Interesting

    • @Exceltrainingvideos
      @Exceltrainingvideos  4 роки тому

      Glad you found the Excel VBA tutorial interesting. Please share with your friends too.

  • @jorgan2028
    @jorgan2028 3 роки тому

    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?

  • @siba720
    @siba720 4 роки тому

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

    • @siba720
      @siba720 4 роки тому

      Could you please take another session on this?

    • @Exceltrainingvideos
      @Exceltrainingvideos  4 роки тому +1

      Yes you can! Try it out!

  • @ORANGEPICTURESOFFICIAL
    @ORANGEPICTURESOFFICIAL 4 роки тому

    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.

  • @shoaibsaifi9806
    @shoaibsaifi9806 3 роки тому

    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.