用vba删除一行excel vba

时间:2017-04-18 18:04:16

标签: excel-vba vba excel

我目前想知道是否有可能删除单行vba而不会弹出错误1004。我不希望用户必须进入设置和信任访问vba项目对象模型。这可能吗? 当前代码(如果未选择信任则会发生错误):     There was an unexpected error. Msg: TypeError: testCase.debugContext.currentCommand(...) is undefined Url: chrome://selenium-ide/content/selenium-runner.js, line: 239, column: 7 .commandComplete@chrome://selenium-ide/content/selenium-runnerjs:239:7 TestLoop.prototype.continueTestWhenConditionIsTrue@chrome://selenium-ide/content /selenium-core/scripts/selenium-executionloop.js:163:13 .continueTestWhenConditionIsTrue@chrome://selenium-ide/content/selenium-runner.js:210:7 fnBind/retval@chrome://selenium-ide/content/selenium-core/scripts/htmlutilsjs:60:12

2 个答案:

答案 0 :(得分:2)

  

我不希望用户必须进入设置和信任访问vba项目对象模型。这可能吗?

不,这是一项安全功能。使用VBA以编程方式切换此安全设置是不可能的。

Per Microsoft(强调补充):

  

此设置适用于开发人员,用于故意锁定   或者允许从任何程序访问VBA对象模型   自动化客户端换句话说,它提供了一个安全选项   为自动化Office程序而编写的代码   以编程方式操作Microsoft Visual Basic for   应用程序(VBA)环境和对象模型。这是每个用户   和每个应用程序设置,并默认拒绝访问。这个   安全选项使未经授权的程序更加困难   建立"自我复制"可能损害最终用户系统的代码。 对于任何人   自动化客户端能够访问VBA对象模型   以编程方式,运行代码的用户必须明确授予   访问。要开启访问权限,请选中复选框。

答案 1 :(得分: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