Excel VBA to Open Multiple Word 2010 Documents and Determine if Checkboxes are Checked

眉间皱痕 提交于 2019-12-25 16:49:29

问题


I'm trying to create a report that analyzes multiple word documents in a folder and analyzes checkboxes in the document to determine if a set of tests passed or failed. I have code that loops through all documents in a folder, but I'm having a hard time determining how to determine if the boxes are checked.

The first checkbox I'm trying to evaluate is tagged "PassCheckBox". I've found several articles with syntax on how to do this, but none seem to work with the way I'm iterating through the word files. My current code give me "Object is Required" when I try to run.

Here is my current code:

Sub ParseTestFiles()
  Dim FSO As Object
  Dim fPath As String
  Dim myFolder, myFile
  Dim wdApp As Object
  Dim PassValue As Boolean

  fPath = ActiveWorkbook.Path
  Set FSO = CreateObject("Scripting.FileSystemObject")
  Set myFolder = FSO.GetFolder(fPath).Files
  For Each myFile In myFolder
    If LCase(myFile) Like "*.doc" _
    Or LCase(myFile) Like "*.docx" Or LCase(myFile) Like "*.docm" Then

      On Error Resume Next
      Set wdApp = GetObject(, "Word.Application")
      If Err.Number <> 0 Then 'Word not yet running
        Set wdApp = CreateObject("Word.Application")
      End If

      On Error GoTo 0
      wdApp.Documents.Open CStr(myFile)
      wdApp.Visible = True
      ' Here is where I'm having an issue
      PassValue = ActiveDocument.FormFields("PassCheckBox").Checked

      Set wdApp = Nothing
    End If 'LCase
  Next myFile

End Sub

回答1:


Try to use:

Dim c, wdDoc
Set wdDoc = wdApp.Documents.Open(CStr(myFile))
wdApp.Visible = True
For Each c In wdDoc.ContentControls
    If c.Title = "PassCheckBox" Then
        PassValue = c.Checked
        Exit For
    End If
Next

instead

wdApp.Documents.Open CStr(myFile)
wdApp.Visible = True

PassValue = ActiveDocument.FormFields("PassCheckBox").Checked


来源:https://stackoverflow.com/questions/21240457/excel-vba-to-open-multiple-word-2010-documents-and-determine-if-checkboxes-are-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!