Inserting text & Checkbox into a Word Table via Access vba (1 Viewer)

mdnuts

Registered User.
Local time
Today, 17:11
Joined
May 28, 2014
Messages
128
my VBA dynamically writes several tables depending on criteria and works quite well.

For example here is adding a row, formatting it and advancing to the next row.

Code:
            oDoc.Tables(t).Rows.Add 'add a row below control number
            oDoc.Tables(t).Rows(i).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
            oDoc.Tables(t).Rows(i).Shading.BackgroundPatternColor = oTableHeaderColor
            i = i + 1 'advance to row below control number row.
            oDoc.Tables(t).Cell(i, 2).Range.Text = "Check Definition"
I do it in this manner because I just don't know how many rows any given table is - so I create the rows as I need them, might be clumsy but it works.

What I need to do is add a check-able checkbox to rows with text like this.
[FONT=&quot]
Planned: ☐
[/FONT]
[FONT=&quot]
[FONT=&quot]I've trie[/FONT][/FONT][FONT=&quot][FONT=&quot]d several ways that just don't [FONT=&quot]seem[FONT=&quot] to work. [FONT=&quot]as far as I can tell it's because i'm not creating the table then selecting it. I've tried [FONT=&quot]recording a macro and that just shows the bit about selection first.

[FONT=&quot]Here's what I've got[FONT=&quot], it pops an error.

[/FONT][/FONT][/FONT][/FONT][/FONT][/FONT][/FONT][/FONT]
Code:
oDoc.Tables(t).Cell(i, 2).Range.Text = "Planned: " & oDoc.Tables(t).Cell(i, 1).Range.ContentControls.Add(wdContentControlCheckBox)
[FONT=&quot][FONT=&quot][FONT=&quot][FONT=&quot][FONT=&quot][FONT=&quot][FONT=&quot][FONT=&quot]
[/FONT][/FONT][/FONT]
[FONT=&quot]I get "requested member of the collection doesn't exist.

[FONT=&quot]If I try to put it on two lines it will just over[FONT=&quot]write cell 1. with the checkbox and I can't seem to position it to the end of the cell first. Any ideas?
[/FONT][/FONT][/FONT]
[/FONT][/FONT][/FONT][/FONT]
[/FONT]
 

static

Registered User.
Local time
Today, 22:11
Joined
Nov 2, 2015
Messages
823
Code:
Set doc = ActiveDocument
With doc.Tables.Add(Selection.Range, 1, 1).Cell(1, 1).Range
    .Text = "Planned: "
    doc.Range(.End - 1, .End - 1).ContentControls.Add wdContentControlCheckBox
End With
 

mdnuts

Registered User.
Local time
Today, 17:11
Joined
May 28, 2014
Messages
128
Thanks for the reply, i'm getting a similar situation around the selection I think. This pops the error 91 - Object Variable or with block not set.

Code:
        oDoc.Tables(t).Cell(i, 2).Range.Text = fldControlNumber & " - " & fldControl_Title  'this is the main header type text.
        oDoc.Tables(t).Rows.Add  'add a line below the main header text
        oDoc.Tables(t).Cell(i, 1).Merge oDoc.Tables(t).Cell(i, 4) 'merge the Title stuffs.
        oDoc.Tables(t).Rows(i).Shading.BackgroundPatternColor = oTableHeaderColor
        oDoc.Tables(t).Rows(i).Range.Font.Bold = True
        'add bottom comment box stuff.
        oDoc.Tables(t).Rows.Add
        i = i + 1
        oDoc.Tables(t).Cell(i, 1).Merge oDoc.Tables(t).Cell(i, 4)
        'testing adding checkbox
        Set doc = ActiveDocument
        With doc.Tables.Add(Selection.Range, i, 1).Cell(i, 1).Range
          .Text = "Implementation:  Implemented: "
          doc.Range(.End - 1, .End - 1).ContentControls.Add wdContentControlCheckBox
        End With
it errors right on the selection.range part.
 

static

Registered User.
Local time
Today, 22:11
Joined
Nov 2, 2015
Messages
823
My code was just an example on how to add text and a checkbox to a cell.

You have already created a document object and a table so you can use those.

Code:
oDoc.Tables(t).Cell(i, 2).Range.Text = fldControlNumber & " - " & fldControl_Title  'this is the main header type text.
oDoc.Tables(t).Rows.Add  'add a line below the main header text
oDoc.Tables(t).Cell(i, 1).Merge oDoc.Tables(t).Cell(i, 4) 'merge the Title stuffs.
oDoc.Tables(t).Rows(i).Shading.BackgroundPatternColor = oTableHeaderColor
oDoc.Tables(t).Rows(i).Range.Font.Bold = True
oDoc.Tables(t).Rows.Add
i = i + 1
oDoc.Tables(t).Cell(i, 1).Merge oDoc.Tables(t).Cell(i, 4)
With oDoc.Tables(t).Cell(i, 1).Range
    .Text = "Implementation:  Implemented: "
    oDoc.Range(.End - 1, .End - 1).ContentControls.Add wdContentControlCheckBox
End With
 

mdnuts

Registered User.
Local time
Today, 17:11
Joined
May 28, 2014
Messages
128
That worked, I could add multiple ones as well just adding onto it like this.

Code:
oDoc.Range(.End - 1, .End - 1).ContentControls.Add wdContentControlCheckBox
oDoc.Range(.End - 1, .End - 1).Text = " Test2 :"
oDoc.Range(.End - 1, .End - 1).ContentControls.Add wdContentControlCheckBox
oDoc.Range(.End - 1, .End - 1).Text = " Test3 :"
oDoc.Range(.End - 1, .End - 1).ContentControls.Add wdContentControlCheckBox
This morning i had asked over on StackOverflow and Andre had come up with another way that also worked. Like yours I can't believe how close I had banged around the answer on.

here's his method if anyone else runs across this thread.

Code:
Dim rng As Word.Range
Set rng = oDoc.Tables(t).Cell(i, 2).Range
rng.Select
With Selection
    .Collapse Direction:=wdCollapseStart
    .TypeText Text:="Planned: "
    .Range.ContentControls.Add (wdContentControlCheckBox)
    ' move cursor after checkbox
    .MoveRight Unit:=wdCharacter, Count:=2
    ' more text, second checkbox
    .TypeText Text:=" Done: "
    .Range.ContentControls.Add (wdContentControlCheckBox)
    .MoveRight Unit:=wdCharacter, Count:=2
End With

I don't know if any one has any advantage over another one. They both work excellent and are valuable resources for this type of work. Thank you !
 

static

Registered User.
Local time
Today, 22:11
Joined
Nov 2, 2015
Messages
823
.select is like using your mouse to select something and causes the application to update graphically (repaint) which is a waste of resources and slows down the code.

Selection is just a range so you could just say

Code:
With oDoc.Tables(t).Cell(i, 2).Range
    .Collapse Direction:=wdCollapseStart
    .TypeText Text:="Planned: "
    .Range.ContentControls.Add (wdContentControlCheckBox)
    ' move cursor after checkbox
    .MoveRight Unit:=wdCharacter, Count:=2
    ' more text, second checkbox
    .TypeText Text:=" Done: "
    .Range.ContentControls.Add (wdContentControlCheckBox)
    .MoveRight Unit:=wdCharacter, Count:=2
End With

Use whichever works best. Although TypeText looks too much like an application specific version SendKeys to me.
 

mdnuts

Registered User.
Local time
Today, 17:11
Joined
May 28, 2014
Messages
128
Thanks, i didn't know it was a virtual mouse selection.

I reworked and went with this.

Code:
          strTextList = "Test1: ,      Test2: ,      Test3: "
          With oDoc.Tables(t).Cell(i, 1).Range
            .Text = "Implementation Status:  "
            For iCheck = 0 To UBound(Split(strTextList, ","))
              oDoc.Range(.End - 1, .End - 1).Text = Split(strTextList, ",")(iCheck)
              oDoc.Range(.End - 1, .End - 1).ContentControls.Add wdContentControlCheckBox
            Next iCheck
              oDoc.Range(.End - 1, .End - 1).Text = "     (Details in Comments)"
          End With
 

Users who are viewing this thread

Top Bottom