none
Information in a Word document to a 3rd party api RRS feed

  • Question

  • All,

    I'm not a word developer.

    We're using lots of Word documents to gather information.

    If a document has lots of "fields"/control(s).

    Can these be extracted easily i.e. button on the document and the information gathered and then be sent to a 3rd party api ?

    Thanks

    W

    Wednesday, September 27, 2017 3:22 PM

All replies

  • Reading the contents/state of a document's formfields/content controls is fairly straightforward; whether & how that might be done with a given 3rd party api depends on what that api supports. As a demo, the following Excel macro extracts data from all formfields & content controls in all Word documents in a selected folder and populates the first available row in the active Excel worksheet with the data for each document.

    Sub GetFormData()
    'Note: this code requires a reference to the Word object model.
    'See under the VBE's Tools|References.
    Application.ScreenUpdating = False
    Dim wdApp As New Word.Application, wdDoc As Word.Document
    Dim FmFld As Word.FormField, 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
    'Disable any auto macros in the documents being processed
    wdApp.WordBasic.DisableAutoMacros
    strFile = Dir(strFolder & "\*.doc", vbNormal)
    While strFile <> ""
      i = i + 1
      Set wdDoc = wdApp.Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
      With wdDoc
        j = 0
        For Each FmFld In .FormFields
          j = j + 1
          With FmFld
            Select Case .Type
              Case Is = wdFieldFormCheckBox
                WkSht.Cells(i, j) = .CheckBox.Value
              Case Else
                If IsNumeric(FmFld.Result) Then
                  If Len(FmFld.Result) > 15 Then
                    WkSht.Cells(i, j) = "'" & FmFld.Result
                  Else
                    WkSht.Cells(i, j) = FmFld.Result
                  End If
                Else
                  WkSht.Cells(i, j) = FmFld.Result
                End If
            End Select
          End With
        Next
        For Each CCtrl In .ContentControls
          With CCtrl
            Select Case .Type
              Case Is = wdContentControlCheckBox
                j = j + 1
                WkSht.Cells(i, j) = .Checked
              Case wdContentControlDate, wdContentControlDropdownList, wdContentControlRichText, wdContentControlText
                j = j + 1
                If IsNumeric(.Range.Text) Then
                  If Len(.Range.Text) > 15 Then
                    WkSht.Cells(i, j) = "'" & .Range.Text
                  Else
                    WkSht.Cells(i, j) = .Range.Text
                  End If
                Else
                  WkSht.Cells(i, j) = .Range.Text
                End If 
              Case Else
            End Select
          End With
        Next
        .Close SaveChanges:=False
      End With
      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 a folder", 0)
        If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
        Set oFolder = Nothing
    End Function
    

    If you want to record the document's name as part of the data, change:
    j = 0
    to:
    j = 1: WkSht.Cells(i, j) = strFile


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Wednesday, September 27, 2017 9:44 PM