2016-12-05 104 views
0

我對VBA一般不熟悉或者使用Range。我想在一行文本之後添加一個複選框,但以下代碼輸出文檔最後的所有複選框。我認爲在設置複選框的範圍參數需要固定,但我不知道如何識別它。Word-VBA:複選框範圍

'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 

回答

0

問題不是該複選框在文檔的結束時加入,但是,它的當前的選擇/插入點(這是一樣的文件的結束)之後加入。然後,其他所有內容都將添加到包裝箱前,並保留在最後。短的修復將是通過將線

Selection.EndKey wdLine 

Selection.EndKey wdStory 

.Add後立即將光標移動到所述線(或文件)的末尾。

另一種可能性是3個字符將光標移動到右側:

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

這將是一個選擇,如果你沒有在一行的末尾添加複選框。