Consulting

Results 1 to 6 of 6

Thread: Excel Data Matching Word Tables & Looping

  1. #1
    VBAX Newbie
    Joined
    Jun 2013
    Posts
    4
    Location

    Excel Data Matching Word Tables & Looping

    Hi, I am fairly new to VBA so I have been searching thoughthis forum for examples. There seem to be plenty of similar macros butnothing suitable.
    What I am trying to do is to match data at the top of theworkbook in a range of tables in multiple word files. Then pull the information in the cell to theright into the workbook in the appropriate cell.
    I am using 2010 edition of both excel and word, I have attached a sample file with tables that I am trying to pull information from. Any from "Product Name", "Reference Code", "Stones" and "Contains Gluten?"
    The following code worked to pull information from contentcontrolled sections of a word file (from a table). I have been unable to workthis for non content controlled though

    [VBA]Sub GetFormData()
    'Note: this coderequires a reference to the Word object model
    Application.ScreenUpdating = False
    Dim wdApp As NewWord.Application
    Dim wdDoc AsWord.Document
    Dim CCtrl AsWord.ContentControl
    Dim strFolder AsString, strFile As String
    Dim WkSht AsWorksheet, i As Long, j As Long
    strFolder =GetFolder
    If strFolder ="" Then Exit Sub
    Set WkSht =ActiveSheet
    i =WkSht.Cells(WkSht.Rows.Count, 1).End(xlUp).Row
    strFile =Dir(strFolder & "\*.docx", vbNormal)
    While strFile<> ""
    i = i + 1
    Set wdDoc =wdApp.Documents.Open(Filename:=strFolder & "\" & strFile,AddToRecentFiles:=False, Visible:=False)
    With wdDoc
    j = 0
    For EachCCtrl In .ContentControls
    j = j + 1
    WkSht.Cells(i, j) = CCtrl.Range.Text
    Next
    End With
    wdDoc.CloseSaveChanges:=False
    strFile =Dir()
    Wend
    wdApp.Quit
    Set wdDoc =Nothing: Set wdApp = Nothing: Set WkSht = Nothing
    Application.ScreenUpdating= True
    End Sub

    Function GetFolder() As String
    Dim oFolder AsObject
    GetFolder =""
    Set oFolder =CreateObject("Shell.Application").BrowseForFolder(0, "Choose afolder", 0)
    If (Not oFolder IsNothing) Then GetFolder = oFolder.Items.Item.Path
    Set oFolder =Nothing
    End Function[/VBA]

    Any help would be greatly appreciated, thanks
    Stephen
    Attached Files Attached Files

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Stephen,

    I you would please format you code with some white space to show the logical sections and use the Green VBA button in the Editor windo to place [VBA] tags around your code,. It will make things a lot easier for us.

    You can paste your code in the Editor, then Select it and click the VBA tag button, or click the button and paste your code inside the tags.

    I have done this for you this time.

    [VBA]Option Explicit


    Sub GetFormData()
    'Note: this code requires a reference to the Word object model
    Application.ScreenUpdating = False

    Dim wdApp As NewWord.Application
    Dim wdDoc As Word.Document
    Dim CCtrl As Word.ContentControl
    Dim strFolder As String, strFile As String
    Dim WkSht As Worksheet, i As Long, j As Long

    strFolder = GetFolder
    If strFolder = "" Then Exit Sub

    Set WkSht = ActiveSheet
    i = WkSht.Cells(WkSht.Rows.Count, 1).End(xlUp).Row
    strFile = Dir(strFolder & "\*.docx", vbNormal)

    While strFile <> ""
    i = i + 1
    Set wdDoc = wdApp.Documents.Open(Filename:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)

    With wdDoc
    j = 0
    For Each CCtrl In .ContentControls
    j = j + 1
    WkSht.Cells(i, j) = CCtrl.Range.Text
    Next
    End With

    wdDoc.Close SaveChanges:=False
    strFile = Dir()
    Wend

    wdApp.Quit
    Set wdDoc = Nothing: Set wdApp = Nothing: Set WkSht = Nothing
    Application.ScreenUpdating = True
    End Sub

    Function GetFolder() As String
    Dim oFolder As Object

    GetFolder = ""

    Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose afolder", 0)

    If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path

    Set oFolder = Nothing
    End Function
    [/VBA]

    You will have to use Word's Tables, Cells and Cell objects to accomplish what you need.

    If it were my problem, I would write a DocumentOpen Event procedure in Normal.doc that would test newly opened documents for Tables.Count = 5 and Tables(1).Cells(2, 1) for "PRODUCT SPECIFICATION." IF those tests are True then...

    I would initiate a new Class object in Normal.doc that had code to retrieve the required values from the just opened Document and store them in Class Properties that Excel could always access directly from Normal.doc.

    This would have the advantage that all code to get the Values would be in Word VBA, making the code that much easier to develop, troubleshoot, and maintain.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    To retrieve that information from a Word document in folder 'G:\OF\' and the name 'test product.doc" and write it into the active Excel worksheet:

    [vba]
    Sub M_snb()
    With GetObject("G:\OF\Test product.doc")
    With .Tables(1)
    thisworkbook.sheets(1).Resize(, 4) = Array(.Cell(4, 2), .Cell(5, 2), .Cell(9, 2), .Parent.Tables(2).Cell(2, 1))
    End With
    .Close 0
    End With
    End Sub

    [/vba]

  4. #4
    VBAX Newbie
    Joined
    Jun 2013
    Posts
    4
    Location
    Thanks for the comments, I will let you know if I can get it to work

    Regards,
    Stephen

  5. #5
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    'TypeText method
    ' http://www.excelforum.com/excel-prog...ml#post1946784
    ' http://www.excelforum.com/showthread.php?p=1946784
    ' http://vbaexpress.com/forum/showthread.php?p=169877
    ' http://vbaexpress.com/forum/showthread.php?t=24693
    ' http://www.excelforum.com/excel-prog...age-setup.html

    'Copy from Excel, paste to Word
    'Lucas, http://vbaexpress.com/forum/showthread.php?p=178364

    'FormFields
    ' http://www.mrexcel.com/forum/showthread.php?p=1639696
    ' http://www.mrexcel.com/forum/showthread.php?t=333200
    ' http://www.excelforum.com/excel-prog...from-word.html
    ' Content Controls
    ' http://www.vbaexpress.com/forum/showthread.php?t=39654

    'Add Hyperlink to Bookmark
    ' http://www.excelforum.com/excel-prog...ml#post2006430
    'Steiner, http://www.vbaexpress.com/kb/getarticle.php?kb_id=126
    'Colin_L, http://www.mrexcel.com/forum/showthread.php?t=358054

    'Save OLEObject as MSWord Document
    ' http://vbaexpress.com/forum/showthread.php?t=21619

    'Add Table to MSWord
    ' http://vbaexpress.com/forum/showthread.php?t=23975
    ' http://vbaexpress.com/forum/showthread.php?p=168731

    'Import Word Tables
    'vog, http://www.mrexcel.com/forum/showthread.php?t=382541
    'Ruddles, http://www.mrexcel.com/forum/showthread.php?t=524091

    'snb, Word Tables
    ' http://www.vbaexpress.com/forum/showthread.php?t=45520
    ' http://www.vbaexpress.com/forum/showthread.php?t=46472

    'Save OLEObject as MSWord DOC
    ' http://vbaexpress.com/forum/showthread.php?t=21619

    'Get Optionbutton info from MSWord DOC
    ' http://vbaexpress.com/forum/showthread.php?t=22454

    'FindReplace Text
    ' http://www.excelforum.com/excel-prog...-ms-excel.html
    ' http://www.vbaexpress.com/forum/showthread.php?t=38958
    ' http://www.vbaexpress.com/forum/showthread.php?p=250215
    ' http://www.vbaexpress.com/forum/showthread.php?t=42833
    ' http://support.microsoft.com/kb/240157
    ' http://word.tips.net/T001833_Generat...currences.html

    ' http://www.excelforum.com/excel-prog...o-to-word.html

    'Bookmarks
    ' http://vbaexpress.com/forum/showthread.php?p=185718
    'Colin_L, http://www.mrexcel.com/forum/showthread.php?t=358054
    ' http://www.vbaexpress.com/forum/showthread.php?p=253277

    'Mail Merge
    ' http://www.excelforum.com/excel-prog...rom-excel.html
    ' http://www.excelforum.com/excel-prog...-document.html
    'Word 's Catalogue/Directory Mailmerge facility (the terminology depends on the Word version). _
    To see how to group records with any mailmerge data source supported by Word, _
    check out my Microsoft Word Catalogue/Directory Mailmerge Tutorial at:
    ' http://lounge.windowssecrets.com/ind...owtopic=731107
    ' or
    ' http://www.gmayor.com/Zips/Catalogue%20Mailmerge.zip

  6. #6
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Thanks Ken,

    I just saved all that to my reference files.

    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •