2017-04-18 223 views
0

我現在想知道是否可以刪除一行vba而不會彈出錯誤1004。我不希望用戶必須進入vba項目對象模型的設置和信任訪問。這可能嗎? 當前的代碼(如果不相信自己選擇的錯誤發生): ActiveWorkbook.Application.VBE.ActiveVBProject.VBComponents("Sheet1").CodeModule.DeleteLines 170, 1用vba刪除一行excel vba

+0

爲了不「選擇」是,有一個代碼,但不那麼短... –

+0

看到我的答案和下面的代碼,我用它在我使用的一些自動工具 –

回答

2

我不希望用戶必須進入設置和信任對VBA項目對象模型。這可能嗎?

不,這是一項安全功能。使用VBA以編程方式切換此安全設置是不可能的。

Microsoft(強調):

此設置是爲開發人員和用於故意鎖定 或允許來自任何 自動化客戶端VBA對象模型編程訪問。換句話說,它爲 代碼編寫了一個安全選項,用於自動執行Office程序,而 以編程方式處理Microsoft Visual Basic的 應用程序(VBA)環境和對象模型。這是每個用戶 和每個應用程序設置,默認情況下拒絕訪問。這種安全選項使未經授權的程序難以構建可能損害最終用戶系統的「自我複製」代碼。 對於任何 自動化客戶端,要能夠以編程方式訪問VBA對象模型 ,運行代碼的用戶必須明確授予 訪問權限。要打開訪問權限,請選中複選框。

+0

因此,沒有用戶不得不信任訪問權限的情況下刪除一行宏是沒有辦法的? – Noisewater

+0

如果沒有明確允許它作爲應用程序級設置,就無法以編程方式訪問VBA項目對象模型。 –

1

你可以嘗試像下面的代碼,看看它是否適合您的需要:

Option Explicit 

Sub CheckTrustAccess_toVBAProjModule() 

' display Windows Version installed on this PC 
' Win7.  (=6.1, 64bit) 
' Win8  (=6.2, 64bit) 
' Win8.1  (=6.3*) 
' Win10  (=10.0*) 
' 
' MsgBox "Windows Version is: " & getVersion 

Dim TrustAccess_VBAProjModule_Path   As String 
Dim TrustAccess_VBAProjModule    As Integer 

' ----- first check if "Trust Access to the VBA Project module is on ------ 
TrustAccess_VBAProjModule_Path = "HKEY_CURRENT_USER\Software\Microsoft\Office\" & Application.Version & "\Excel\Security\AccessVBOM" 

' check if Trust Access to the VBA Projct Module in the Registry is 1 or 0 
TrustAccess_VBAProjModule = CInt(RegKeyRead(TrustAccess_VBAProjModule_Path)) 

If TrustAccess_VBAProjModule = 1 Then 
    ' run your code here ... 


Else ' need to auto-click the "v" by modifying the registry settings 

    ' loop until Trust Access to the VBA Projct Module in the Registry is 1 
    ' it might take time to modify, so use this instead of timers 
    While TrustAccess_VBAProjModule = 0 
     Call RegKeySave(TrustAccess_VBAProjModule_Path, "1") 

     TrustAccess_VBAProjModule = CInt(RegKeyRead(TrustAccess_VBAProjModule_Path)) 
    Wend 
    MsgBox "Initiated VBA settings, please run the Export again", vbInformation 
End If 

End Sub 

'=========================================================== 

Function RegKeyRead(i_RegKey As String) As String 

' reads the value for the registry key i_RegKey 
' if the key cannot be found, the return value is "" 
' Link : http://vba-corner.livejournal.com/3054.html 

Dim myWS As Object 

On Error Resume Next 
'access Windows scripting 
Set myWS = CreateObject("WScript.Shell") 
'read key from registry 
RegKeyRead = myWS.RegRead(i_RegKey) 

End Function 

'=========================================================== 

Sub RegKeySave(i_RegKey As String, i_Value As String, Optional i_Type As String = "REG_DWORD") 

' sets the registry key i_RegKey to the value i_Value with type i_Type 
' if i_Type is omitted, the value will be saved as string 
' if i_RegKey wasn't found, a new registry key will be created 
' Link : http://vba-corner.livejournal.com/3054.html 

Dim myWS As Object 

'access Windows scripting 
Set myWS = CreateObject("WScript.Shell") 
'write registry key 
myWS.RegWrite i_RegKey, i_Value, i_Type 

End Sub 
+0

@Noisewater你看過我的代碼嗎?你有沒有試過看它是否適合你的需求? –

+0

我剛測試過它,它確實有效。用戶點擊加載項兩次沒有什麼大不了的。謝謝! – Noisewater

+0

只是一個問題。雖然它確實檢查,如果它不被信任,檢查框。它僅在用戶打開信任中心/宏安全性並選擇確定時纔有效。這是朝正確方向邁出的一步,但用戶仍然需要給予正式的確定。 – Noisewater