Word文档中的信息到第三方API [英] Information in a Word document to a 3rd party api

查看:120
本文介绍了Word文档中的信息到第三方API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部,


我不是开发人员。


我们正在使用大量Word文档来收集信息。


如果文档有很多"字段"/控件。


这些是否可以轻松提取,即文档上的按钮和收集到的信息,然后发送给第三方API?


谢谢


W

解决方案

读取文档的表单/内容控件的内容/状态非常简单;是否&如何使用给定的第三方api取决于api支持的内容。作为演示,以下Excel宏从所有
formfields&中提取数据。所选文件夹中所有Word文档中的内容控件,并使用每个文档的数据填充活动Excel工作表中的第一个可用行。

 Sub GetFormData( )
'注意:此代码需要引用Word对象模型。
'参见VBE的工具|参考资料。
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
如果strFolder =""然后退出Sub
设置WkSht = ActiveSheet
i = WkSht.Cells(WkSht.Rows.Count,1).End(xlUp).Row
'禁用正在处理的文档中的任何自动宏
wdApp.WordBasic.DisableAutoMacros
strFile = Dir(strFolder&" \ * .doc",vbNormal)
虽然strFile<> ""
i = i + 1
设置wdDoc = wdApp.Documents.Open(FileName:= strFolder&" \"& strFile,AddToRecentFiles:= False,Visible:= False)
使用wdDoc
j = 0
每个FmFld In .FormFields
j = j + 1
使用FmFld
选择Case .Type
Case Is = wdFieldFormCheckBox
WkSht.Cells(i,j)=。CheckBox.Value
Case Else
If IsNumeric(FmFld.Result)Then
If Len(FmFld.Result)> 15然后
WkSht.Cells(i,j)="'" &安培; FmFld.Result
Else
WkSht.Cells(i,j)= FmFld.Result
End If
Else
WkSht.Cells(i,j)= FmFld。结果
结束如果
结束选择
结束
下一个
每个CCtrl In .ContentControls
使用CCtrl
选择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然后
WkSht.Cells(i,j)="'" &安培; .Range.Text
Else
WkSht.Cells(i,j)=。raange.Text
End if
Else
WkSht.Cells(i,j)= .Range.Text
结束如果
Case Else
结束选择
结束
下一个
。关闭SaveChanges:= False
以$结尾b $ b strFile = Dir()
Wend
wdApp.Quit
Set wdDoc = Nothing:Set wdApp = Nothing:Set WkSht = Nothing
Application.ScreenUpdating = True
End Sub

函数GetFolder()As String
Dim oFolder As Object
GetFolder =""
设置oFolder = CreateObject(" Shell.Application")。BrowseForFolder(0," Choose a folder",0)
If(not oFolder Is Nothing)Then GetFolder = oFolder.Items.Item。路径
设置oFolder =无
结束函数

如果要将文档名称记录为数据的一部分,请更改:

j = 0

到:

j = 1:WkSht.Cells(i,j)= strFile


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

解决方案

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


这篇关于Word文档中的信息到第三方API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆