Results 1 to 17 of 17

Thread: Word Content Control Data Import to Excel Table

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #4
    VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,273
    Location
    Presumably the Hadkins page is: http://www.techrepublic.com/article/...n-excel-table/

    To use the named Excel table approach, you might do something along the lines of (untested):
    Sub GetFormData()
         'Note: this code requires a reference to the Word object model
         'To do this, go to Tools|References in the VBE, then scroll down to the Microsoft Word entry and check it.
        Application.ScreenUpdating = False
        Dim strFolder As String, strFile As String
        Dim WkSht As Worksheet, xlRw As ListRow, c As Long
        strFolder = GetFolder: If strFolder = "" Then Exit Sub
        Dim wdApp As New Word.Application, wdDoc As Word.Document, CCtrl As Word.ContentControl
        Set WkSht = ActiveSheet
        strFile = Dir(strFolder & "\*.docx", vbNormal)
        While strFile <> ""
            Set wdDoc = wdApp.Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
            Set xlRw = WkSht.ListObjects("tbl_Employees").ListRows.Add(AlwaysInsert:=True)
            With wdDoc
                c = 0
                For Each CCtrl In .ContentControls
                    c = c + 1
                    With CCtrl
                        Select Case .Type
                            Case Is = wdContentControlCheckBox
                                xlRw.Range.Cell(c) = .Checked
                            Case wdContentControlDate, wdContentControlDropdownList, wdContentControlRichText, wdContentControlText
                                If IsNumeric(.Range.Text) Then
                                    If Len(.Range.Text) > 15 Then
                                        xlRw.Range.Cell(c) = "'" & .Range.Text
                                    Else
                                        xlRw.Range.Cell(c) = .Range.Text
                                    End If
                                Else
                                    xlRw.Range.Cell(c) = .Range.Text
                                End If
                            Case Else
                        End Select
                    End With
                Next
            End With
            wdDoc.Close SaveChanges:=False
            strFile = Dir()
        Wend
        wdApp.Quit
        Set wdDoc = Nothing: Set wdApp = Nothing: Set WkSht = Nothing: Set xlRw = Nothing
        Application.ScreenUpdating = True
    End Sub
     
    Function GetFolder() As String
        Dim oFolder As Object
        GetFolder = ""
        Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
        If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
        Set oFolder = Nothing
    End Function
    Last edited by macropod; 10-17-2022 at 02:10 PM. Reason: Data export enhancements
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Tags for this Thread

Posting Permissions

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