简体   繁体   中英

Word-VBA: checkbox range

I'm not quite familiar with VBA in general or working with Range. I'd like to add a checkbox after a line of text but the following code outputs all the checkboxes at the very end of the document. I'd think the range parameter in setting the checkbox needs to be fixed but I don't know how to identify it.

'http://wordribbon.tips.net/T010727_Inserting_Multiple_Graphics_in_a_Document.html
Sub GenerateLab()
Dim sPic As String
Dim sPath As String

sPath = "C:\Users\lab\Documents\PDF Gen 12-1\TestImages\"
sPic = Dir(sPath & "*.png")

Do While sPic <> ""
    Selection.TypeText ("Is this an ***?")
    Selection.TypeParagraph
    Selection.TypeText ("***")
    Dim objCC As ContentControl
    Set objCC = ActiveDocument.ContentControls _
        .Add(wdContentControlCheckBox)
    Selection.TypeParagraph
    Selection.TypeText ("Not ***")
    Dim objCC2 As ContentControl
    Set objCC2 = ActiveDocument.ContentControls _
        .Add(wdContentControlCheckBox)
    Selection.InlineShapes.AddPicture _
      FileName:=sPath & sPic, _
      LinkToFile:=False, SaveWithDocument:=True
    sPic = Dir
    Selection.InsertBreak (7)
Loop
End Sub

The problem is not that the checkbox is added at the end of the document but that it's added after the current selection/insertion point (which is the same as the end of the document). Then everything else is added in front of the box and it stays at the end. A short fix would be to move the cursor to the end of the line (or document) by adding the line

Selection.EndKey wdLine

or

Selection.EndKey wdStory

right after .Add .

Another possibility would be to move the cursor 3 characters to the right:

Selection.Move Unit:=wdCharacter, Count:=3

This would be an alternative if you don't add the checkbox at the end of a line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM