jeudi 5 février 2015

Reading checkbox values with a loop (Microsoft Word VBA)

I'm current trying to write a macro (VBA in Word) that will compile information from a collection of documents into a single document.


I order to do this i have a list of ~20 checkboxes that will determine which documents i want to include in the compilation. My issue is that when writing the macro, i can't figure out a way of checking the state of each checkbox on my list without re-writing the same block of code 20 times, only changing the name of the checkbox. eg CB1 to CB2, CB3 CB4 etc. each time


This is the block of code in question. It does work if i rewrite it multiple times for the changing chekcbox number but i would prefer it in a loop so the code is more compact and robust:


**If ThisDocument.CB1.Value = True Then


Documents.Open(directory).Activate


Selection.WholeStory


Selection.Copy


Documents(NewFile).Activate


Selection.Paste


Documents("file.docx").Close


End If**


Ideally i would like to have the check box named something like CBn, where n is a variable that i can redefine at the end of each loop.


Any ideas would be much appreciated. Thanks!





Aucun commentaire:

Enregistrer un commentaire