2013-05-01 135 views
1

所以我創建了一個表格,用戶可以填寫表格。他們選擇一個將整個表單複製到剪貼板上的按鈕。但是,我希望表單禁止複製,如果他們沒有填寫一節,或者如果他們鍵入NA。目前,當表單執行此操作時,只有在底部單元格上留下了NA或空白答案的情況下才起作用。如何編輯範圍以包含整個表格(單元格1-15)?以下是我的代碼。 (編輯:修改爲回答,但得到所請求的成員不存在FormFields開始在表2行的錯誤範圍錯誤

Private Sub Contactcopy_Click() 
' Contact Copy Macro 
'Unprotect the file 
If ActiveDocument.ProtectionType <> wdNoProtection Then 
ActiveDocument.Unprotect Password:="" 
End If 
T = 1 
For X = 2 To 15 
Set r = ActiveDocument.Tables(2).Rows(X).Cells(1).Range.FormFields(1) 
If ((r = "NA") Or (r = "")) Then 
MsgBox "BLANK QUESTION OR NA ENTERED" 
T = 0 
Exit For 
Else 
With ActiveDocument 
    Set myRange = .Range(.Tables(2).Rows(2).Cells(1).Range.Start, _ 
       .Tables(2).Rows(15).Cells(1).Range.End) 
    myRange.Select 
    Selection.Copy 
End With 
End If 
Next X 
'Reprotect the document. 
If ActiveDocument.ProtectionType = wdNoProtection Then 
ActiveDocument.Protect _ 
Type:=wdAllowOnlyFormFields, NoReset:=True, Password:="" 
End If 
End Sub 

回答

2

嘗試使用週期測試細胞的條件:。

T=1 
For x = 1 To 15 
    Set R = ActiveDocument.Tables(2).Rows(x).Cells(1).Range.FormFields(1) 
    If ((R = "NA") Or (R = "")) Then 
    MsgBox "BLANK QUESTION OR NA ENTERED" 
    T=0 
    Exit For 
    End If 
Next x 

如果T = 1,則選擇可與此建設取得:

With ActiveDocument 
     Set myRange = .Range(.Tables(2).Rows(1).Cells(1).Range.Start, _ 
        .Tables(2).Rows(15).Cells(1).Range.End) 
     myRange.Select 
     Selection.Copy 
    End With 
+0

我得到一個編譯錯誤:對象在'.Result' – 2013-05-01 15:27:28

+0

必需此代碼只是品嚐如果你發送給我。你的文件,我嘗試找到工作方案。 – n158 2013-05-01 19:35:32