lundi 27 août 2018

VBA Excel Launching Word Doc can't create clickable checkboxes

I have an Excel Document, where I am looping through the cells to generate a word document with clickable check boxes. I am taking 1 cell value as the header, the next column cell value as the value that needs a check box next to it. Basically it is a list of tasks I want to cut and paste in email to ensure they have been completed. I am using late binding to generate the word document from Excel and am wondering if that has something to do with my issues. I have tried all different examples of creating the checkboxes and usually get an Object Required error message.

Dim wrdApp As Object

Set wrdApp = CreateObject("Word.Application")

Set wrdDoc = wrdApp.Documents.Add

Set objSelection = wrdApp.Selection

For Row = 2 to lRow

   objSelection.TypeText Chr(13) & Cells(Row, 1).Value & Chr(13)

   'Here is where I would like to write a checkbox so it is to the left of the column 2 value

   objSelection.TypeText Cells(Row, 2).Value)

Next Row

I have never written from Excel to Word before, it seems like I should be able to have a simple one line of code of writing the checkbox in place but maybe I am wrong.

I recorded a macro of adding a check box and when I try this code:

 Selection.FormFields.Add Range:=Selection.Range, Type:=wdFieldFormCheckBox  

I get Wrong number of arguments or invalid property assignment.

When I try going along these lines:

   wrdDoc.Selection.FormFields.Add Range:=InsertAfter, Type:=wdFieldFormCheckBox   

I get Object doesn't support this property or method.

Trying something like this

Selection.FormFields.Add Range:=Selection.Range, Type:=wdFieldFormCheckBox    

I get Wrong Number of Arguments or invalid property assignment.

Any help is appreciated. Thanks!




Aucun commentaire:

Enregistrer un commentaire