Jump to content
Sign in to follow this  
kiffab

Searching word docs

Recommended Posts

kiffab

Hi Guys

I have hundreds of word templates and need to loop through them checking each one for the existence of checkboxes. I have googled but not found anything useful.

Is this possible? Any tips on how I can achieve it?

Cheers

Share this post


Link to post
Share on other sites
water

You could either use the Word UDF that comes with AutoIt or my of the Word UDF which better handles processing of many files.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
kiffab

Hi water

Thanks for that. Very useful. From this I can build a list of docs and open them. I see there's also a Find/Replace (find being important in my case). But how would I 'find' a checkbox?

Thanks

Share this post


Link to post
Share on other sites
water

What do you want to do with the found checkboxes?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
kiffab

Simply know that they are present. If they exist I will write the name of the file to a text file or something. I have to remove these which I will get someone to do manually - just hoping to narrow it down to save them hunting through every template :

Share this post


Link to post
Share on other sites
water

Which version of Word do you run?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
kiffab

I have 2010 but the templates are from 2000/2003. I can install any version required though.

Share this post


Link to post
Share on other sites
water

Example to scan a Word document for checkboxes using my It would be easy to delete all checkboxes and save the files with a new name.

#include <WordEx.au3>

Global $sDocument = "Test_Checkbox.docx" ; <=== Change path/name

; WdFieldType Enumeration. Specifies a Microsoft Office Word field.
; See: http://msdn.microsoft.com/en-us/library/bb213727%28v=office.12%29.aspx
Global Const $wdFieldFormCheckBox = 71 ; FormCheckBox field

; WdContentControlType Enumeration. Indicates the type of content control.
; See: http://msdn.microsoft.com/en-us/library/ff197873%28v=office.14%29.aspx
Global Const $wdContentControlCheckbox = 8 ; Specifies a checkbox content control

; Create application object
Global $oWord = _Word_Create()
If @error <> 0 Then Exit MsgBox(16, "Word Example", "Error creating a new Word application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
; Open document
Global $oDoc = _Word_DocOpen($oWord, $sDocument)
If @error <> 0 Then Exit MsgBox(16, "Word Example", "Error opening 'Test.doc'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

For $oContentControl In $oDoc.ContentControls
    If $oContentControl.Type = $wdContentControlCheckbox Then
         MsgBox(64, "Word Example", "Checkbox found!")
        ; $oContentControl.Delete(True) <== Delete the Control
    EndIf
Next

_Word_DocClose($oDoc)
_Word_Quit($oWord)
Exit
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
kiffab

Thanks water.

I tried this but I can't see to get it to identify checkboxes within the file. I am reading .dot template files if that's a problem. However, it opens OK.

Changed it slightly just to read a file first :

#include <File.au3>
#include <../_WordEx 1.3/WordEx.au3>

Global $sDocument
; = "C:\AutoIT\Checkboxes\checkbox.dot" ; <=== Change path/name

; WdFieldType Enumeration. Specifies a Microsoft Office Word field.
; See: http://msdn.microsoft.com/en-us/library/bb213727%28v=office.12%29.aspx
Global $wdFieldFormCheckBox = 71 ; FormCheckBox field

; WdContentControlType Enumeration. Indicates the type of content control.
; See: http://msdn.microsoft.com/en-us/library/ff197873%28v=office.14%29.aspx
Global $wdContentControlCheckbox = 8 ; Specifies a checkbox content control

$file = @SCRIPTDIR & "\smalltemplatelist.txt"
FileOpen($file, 0);

For $i = 1 to _FileCountLines($file)
$line = FileReadLine($file, $i)
$sDocument = $line
Check()
Next
FileClose($file)

Func Check()

; Create application object
Global $oWord = _Word_Create()
If @error <> 0 Then Exit MsgBox(16, "Word Example", "Error creating a new Word application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
; Open document
Global $oDoc = _Word_DocOpen($oWord, $sDocument)
If @error <> 0 Then Exit MsgBox(16, "Word Example", "Error opening 'Test.doc'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

For $oContentControl In $oDoc.ContentControls
If $oContentControl.Type = $wdContentControlCheckbox Then
         MsgBox(64, "Word Example", "Checkbox found!")
     ; $oContentControl.Delete(True) <== Delete the Control
EndIf
Next

_Word_DocClose($oDoc)
_Word_Quit($oWord)
EndFunc
Edited by kiffab

Share this post


Link to post
Share on other sites
jdelaney

Google: Word VBA CheckBox

first result:

The following example selects the check box form field named "Check1" in the active document.

Activedocument.FormFields("Check1").CheckBox.Value = True

Edited by jdelaney

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites
water

Unfortunately this is only true for Word 2007. My example above is for Word 2010. See the difference?

In your example you need to know the name of the Checkbox. But the OP needs a way to access the collection of all controls.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
jdelaney

No, that's a way to single out one object of a col...you can get the full collection by removing that, and then loop through:

$oObjCol = $oWord.Activedocument.FormFields
 For $oObj In $oObjCol
  ConsoleWrite($oObj.name &  @CRLF)
 Next

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites
water

I tried this but I can't see to get it to identify checkboxes within the file. I am reading .dot template files if that's a problem. However, it opens OK.

Could you please post one of your .dot files?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
kiffab

Could you please post one of your .dot files?

I can't upload these as they contain data, built in fields etc.However, when I add the code posted by jdelaney it is returning the name of the checkbox in the console.

The template is a 'word 97-2003' template (probably 2003) but my PC is running word 2010 if that helps at all.

Share this post


Link to post
Share on other sites
water

I tried to create a .dot file for Word 97-2003 with Word 2010 but when saving it drops all checkboxes.

Can you create a reproducer .dot file with a checkbox and without company related info with an old version of Word?

Or if you like you can send me a .dot file by private mail of this forum. I promise to delete the file as soon as the problem is solved!


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×