Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
696 views
in Technique[技术] by (71.8m points)

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

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...