0

I have to fetch specific data from many Word documents in sub-folders & paste into the next cell. For ex: First page of document contains "Application id= 1234" & next Word document first page contains "Application id=4563". I want those application id's to a new cell in Excel under B column.

When I tried using the below code, I got the whole first page data in a column.

Current result (which is wrong)

Expecting image

First page of word document

Option Explicit

Dim FSO As Object
Dim strFolderName As String
Dim FileToOpenVdocx As String
Dim FileToOpenvdoc1 As String
Dim FileToOpenVdoc As String
Dim FileToOpenvdocx1 As String
Dim wrdApp As Word.Application
Dim wrdDoc As Word.Document
Dim fsoFolder As Object

 'To copy data from word to excel

   'Copy data from word to excel


Sub FindFilesInSubFolders()
Dim fsoFolder As Scripting.Folder
Sheets("Sheet1").Cells.Clear
FileToOpenVdocx = "*V2.1.docx*"
FileToOpenvdoc1 = "*v2.1.doc*"
FileToOpenVdoc = "*V2.1.doc*"
FileToOpenvdocx1 = "*v2.1.docx*"

If FSO Is Nothing Then
   Set FSO = CreateObject("Scripting.FileSystemObject")
End If

'Set the parent folder for the new subfolders
strFolderName = "C:\Test1"

Set fsoFolder = FSO.GetFolder(strFolderName)
Set wrdApp = CreateObject("Word.Application")

OpenFilesInSubFolders fsoFolder
wrdApp.Quit
End Sub

Sub OpenFilesInSubFolders(fsoPFolder As Scripting.Folder)
Dim fsoSFolder As Scripting.Folder
Dim fileDoc As Scripting.File
Dim wrdRng As Object
Dim strText As String
'Dim outRow As Long ' newly added

   'outRow = 1 'you appear to want to start at the second row

For Each fsoSFolder In fsoPFolder.SubFolders
For Each fileDoc In fsoSFolder.Files

        If fileDoc.Name Like FileToOpenVdocx And Left(fileDoc.Name, 1) <> "~" Then
        Set wrdDoc = wrdApp.Documents.Open(fileDoc.Path)
        Set wrdRng = wrdDoc.Content

           If wrdRng.Find.Execute(FindText:="Application ID:[0-9]{1,}", MatchWildcards:=True) = True Then
    MsgBox "Text not found!", vbExclamation
    End If
     strText = wrdRng.Text

     'Cells(outRow & "B").Value = strText 'newly added
        'outRow = outRow + 1 'newly added

       Range("B2").Value = strText

        With wrdApp
           .ActiveDocument.Tables(1).Select
           .Selection.Copy
           ThisWorkbook.Worksheets("Sheet1").Cells(Rows.Count, "D").End(xlUp)(1).PasteSpecial xlPasteValues
       End With
       wrdDoc.Close False
       'wrdApp.Quit

       ElseIf fileDoc.Name Like FileToOpenvdoc1 And Left(fileDoc.Name, 1) <> "~" Then
       Set wrdDoc = wrdApp.Documents.Open(fileDoc.Path)

        With wrdApp
           .ActiveDocument.Tables(1).Select
           .Selection.Copy
           ThisWorkbook.Worksheets("Sheet1").Cells(Rows.Count, "D").End(xlUp)(1).PasteSpecial xlPasteValues
       End With
       wrdDoc.Close False

       ElseIf fileDoc.Name Like FileToOpenVdoc And Left(fileDoc.Name, 1) <> "~" Then
       Set wrdDoc = wrdApp.Documents.Open(fileDoc.Path)

        With wrdApp
           .ActiveDocument.Tables(1).Select
           .Selection.Copy
           ThisWorkbook.Worksheets("Sheet1").Cells(Rows.Count, "D").End(xlUp)(1).PasteSpecial xlPasteValues
       End With
       wrdDoc.Close False

       ElseIf fileDoc.Name Like FileToOpenvdocx1 And Left(fileDoc.Name, 1) <> "~" Then
       Set wrdDoc = wrdApp.Documents.Open(fileDoc.Path)

        With wrdApp
           .ActiveDocument.Tables(1).Select
           .Selection.Copy
           ThisWorkbook.Worksheets("Sheet1").Cells(Rows.Count, "D").End(xlUp)(1).PasteSpecial xlPasteValues
       End With
       wrdDoc.Close False
      End If
    Next fileDoc
   'Debug.Print fsoSFolder

   OpenFilesInSubFolders fsoSFolder
Next fsoSFolder
End Sub
Community
  • 1
  • 1
nihal idiot
  • 35
  • 1
  • 11
  • Is the application ID of a fixed length of characters? – Luuklag Jul 28 '15 at 07:55
  • Hi Luuklag, Application ID: is not fixed chars. It varies from doc to doc. – nihal idiot Jul 28 '15 at 08:59
  • Could you provide some examples of variations that occur? Perhaps the length of the ID number is constant, or it is always exactly "Application ID:" – Luuklag Jul 28 '15 at 09:05
  • I want "Application ID:****" from each doocument, not only id (number). I have "Application ID:1245" in one document & "Application ID:78794" in other document. Now I want this whole "Application ID:1245" & "Application ID:78794" in a cell of B column. – nihal idiot Jul 28 '15 at 09:14
  • My above first image is my current result. Second image is expected result. – nihal idiot Jul 28 '15 at 09:16
  • Try the answer of @Nicolas but replace resultId = Trim(Replace(singleLine, "Application ID:", "")) with resultID = singleLine – Luuklag Jul 28 '15 at 09:18

2 Answers2

1

I have not so many input documents. So, I can't test your whole code. But I got one for you. I prepare a document like your input. And I tested with the following code. It returns the id which we desired. So, you can try with this. I believe that the code will be helpful for you.

Public Sub getID()

    Dim found As Integer
    Dim resultId As String

    Set wordApp = CreateObject("word.Application")

    wordApp.documents.Open ThisWorkbook.Path & "\ID1.docx"

    wordApp.Visible = True

    'Loop all content in line by line from paragraph of active document
    For Each singleLine In wordApp.ActiveDocument.Paragraphs

        'Search "Application ID" in line.
        'If found, value will be greater 0.
        found = InStr(singleLine, "Application ID")

        'If Application ID is found, get ID only
        If found > 0 Then

            'If you want the whole line, try as "resultId = singleLine"
            'The below line is separating id from that string.

            'Get ID by replacing the prefix with space.
            resultId = Trim(Replace(singleLine, "Application ID:", ""))

            MsgBox resultId

            'After getting, stop loop because not need
            Exit For

        End If

    Next singleLine

End Sub
R.Katnaan
  • 2,486
  • 4
  • 24
  • 36
  • This has more potential then my answer, as filtering happens in an earlier stage, so dragging less mess into excel. Only thing that might be wrong is that in not all cases "application id" is present as commented by TS – Luuklag Jul 28 '15 at 09:07
  • Yes, I added that for know how to get that ID. That all. – R.Katnaan Jul 28 '15 at 09:46
  • You are not accept this as right answer. If this is work, mark the answer beside it. That is accepting. – R.Katnaan Jul 29 '15 at 10:31
0

Try replacing:

Range("B2").Value = strText

with

Range("B2").Value = "Application ID: " & Right(Left(strText, Instr(strText, "Application ID") + 19),4)

This only works though if your ID is always 4 digits long.

ps. I didn't try the code myself, so let me know if it works.

Or you can have a look at this: How to find numbers from a string? and combine it with some string length manipulation as I did in my answer.

Community
  • 1
  • 1
Luuklag
  • 3,897
  • 11
  • 38
  • 57
  • I need to copy that whole line, my whole line consists of just "Application ID: 4578". How to achieve this. – nihal idiot Jul 28 '15 at 09:20
  • Yes I understood that, and therefore I copy the number out of the line, and paste it together with Application ID: now as of the most recent edit to my answer. – Luuklag Jul 28 '15 at 09:21
  • Also my answer is not helpful anymore, as it implies ID length of only 4 characters. – Luuklag Jul 28 '15 at 09:25
  • Luuklag, I am getting Application ID:**** correct only. But, it is sitting in next below cell. But if you see my second image, 2nd time fetching "Application ID" must sit in a cell, where "Version" is present. See my second image pls. – nihal idiot Jul 28 '15 at 09:43
  • Well the code you wrote puts it explicitly in cell B2. If you want it next to a cell with version present you should loop through column D and find the rows in which version is, and then use that row number to direct your application ID to a cell in column B – Luuklag Jul 28 '15 at 09:51