在Word中的文本之后访问VBA-插入复选框表
问题描述:
我的VBA根据标准动态地写入几个表并且工作得很好。在Word中的文本之后访问VBA-插入复选框表
例如,这里是添加一行,格式化并前进到下一行。
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"
我做这种方式,因为我只是不知道任何给定的表有多少行是 - 所以我创建的行,我需要他们,可能是笨拙,但它的工作原理。
我需要做的是添加一个可检查的复选框到这样的文本行。
计划:☐
我尝试了几种方法,只是似乎没有工作。据我可以告诉它是因为我不创建表然后选择它。我试过录制一个宏,并且首先显示了关于选择的一点。
这是我得到的,它弹出一个错误。
oDoc.Tables(t).Cell(i, 2).Range.Text = "Planned: " & oDoc.Tables(t).Cell(i, 1).Range.ContentControls.Add(wdContentControlCheckBox)
我得到“要求集合的成员不存在。
如果我试图把它放在两行它只是简单地覆盖电池1.与复选框,我似乎无法到它的位置在小区第一结束。任何想法?我试过的insertBefore和作品,但我在同一单元格中插入数个复选框。
任何想法? 谢谢。
答
获取文本+复选框+其他文本+第二个复选框一个细胞很棘手。
这可能是少数情况下,你实际上必须使用Selection
对象。
这个工作对我来说在Word中:
Dim oDoc As Word.Document
Set oDoc = ThisDocument
Const t = 1
Const i = 1
Dim rng As Word.Range
Dim iCheck As Long
Dim sLabel As String
Set rng = oDoc.Tables(t).Cell(i, 2).Range
rng.Select
With Selection
.Collapse Direction:=wdCollapseStart
For iCheck = 1 To 3
sLabel = Choose(iCheck, "Planned: ", " Done: ", " Perhaps: ")
.TypeText Text:=sLabel
.Range.ContentControls.Add wdContentControlCheckBox
' move cursor after checkbox
.MoveRight Unit:=wdCharacter, Count:=2
Next iCheck
End With
在Access,使用oWord.Selection
代替(或者你有作为参考字什么的)。
以下工作适用于单个复选框,但我没有设法在之后创建第二个附加文本。
Dim rng As Word.Range
Set rng = oDoc.Tables(t).Cell(i, 2).Range
rng.Text = "Planned: "
' set range to end of cell
rng.Collapse Direction:=wdCollapseEnd
' I'm not entirely sure why this is needed, but without the checkbox goes into the next cell
rng.MoveEnd Unit:=wdCharacter, Count:=-1
rng.ContentControls.Add (wdContentControlCheckBox)
它的工作。我不敢相信我之前有多接近它,但是通过钉住它。 所有我必须做的与你的单词vba添加oWord.Selection就地选择,我可以添加尽可能多的框,因为我想。 – Chasester
我最终通过修改来使用它。而不是选择我刚刚使用UBound和分裂它。 – Chasester