在单词VBA中插入一个复选框

时间:2016-11-21 15:37:57

标签: excel vba checkbox ms-word

您好我有以下代码在单词VBA中插入一个复选框;基本上它从电子表格中提取信息,其中有某些列(我只包含19个,但至少还有15个)是“问题”;意思是,需要插入一个复选框,以及如何回答该问题,可以选中也可以不选中。

除此之外,它填写了字符串或数字格式的信息;但它总是插在下一个书签上。

在word文档中,有一系列书签,迭代标记。意思是,从文档的开头,第一个书签名为bm1,然后是bm2,命名为bm 61。

问题是插入复选框。以下代码插入复选框!但是然后word给出错误:

错误438 - 对象不支持此属性或方法。但它插入了!

如何解决此错误?

 Sub WriteExtension()

    'initialize excel variables
    Dim bmrange As Range

    'initialize excel objects
    Dim oExcel As Excel.Application
    Dim oWorkbook As workbook
    Dim oWorksheet As worksheet

    'start up this instance of excel
    Set oExcel = New Excel.Application
    Set oWorkbook = oExcel.Workbooks.Open("C:\file\path\here")
    Set oWorksheet = oWorkbook.Worksheets(Sheets("Extensions").Index)

    'stores whatever is in the spreadsheet cell
    Dim tempString As String 

   For i = 7 To 61
        'store the current item in worksheet
        tempString = oWorksheet.Cells(4, i)

        'set the range of the current bookmark being iterated through
        Set bmrange = ActiveDocument.Bookmarks("BM" & (i - 6)).Range


        'put in a checkbox
        If i = 19 Then
            'cast it as an int; the value in the spreadsheet is either 1 or 0 
 'at cell(4, 19)
            If CInt(tempString) = 1 Then
                ActiveDocument.Range.InsertAfter    (ActiveDocument.ContentControls.Add(wdContentControlCheckBox, bmrange).Checked)
                Set bmrange = ActiveDocument.Bookmarks("BM" & (i + 1 - 6)).Range
                ActiveDocument.Range.InsertAfter (ActiveDocument.ContentControls.Add(wdContentControlCheckBox, bmrange))
                i = i + 2
            Else
                ActiveDocument.Range.InsertAfter (ActiveDocument.ContentControls.Add(wdContentControlCheckBox, bmrange))
                Set bmrange = ActiveDocument.Bookmarks("BM" & (i + 1 - 6)).Range
                ActiveDocument.Range.InsertAfter (ActiveDocument.ContentControls.Add(wdContentControlCheckBox, bmrange).Checked)
                i = i + 2
            End If

        Else

            ActiveDocument.Bookmarks("BM" & (i - 6)).Range.InsertAfter (tempString)
        End If


    Next i

 end sub

0 个答案:

没有答案