Excel VBA打开多个Word 2010文档并确定是否选中了复选框

时间:2014-01-20 17:30:21

标签: excel vba checkbox ms-word

我正在尝试创建一个报告,分析文件夹中的多个word文档,并分析文档中的复选框,以确定是否通过了一组测试。我有代码循环遍历文件夹中的所有文档,但我很难确定如何确定是否选中了这些框。

我正在尝试评估的第一个复选框标记为“PassCheckBox”。我发现了几篇关于如何执行此操作的语法文章,但似乎没有一种方法与我迭代word文件的方式一起工作。当我尝试运行时,我当前的代码给了我“Object is Required”。

这是我目前的代码:

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 个答案:

答案 0 :(得分:0)

尝试使用:

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

代替

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

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