简体   繁体   English

用vba删除一行excel vba

[英]Remove one line of excel vba with vba

I currently am wondering if it is possible to remove a single line of vba without the error 1004 popping up.我目前想知道是否可以在不弹出错误 1004 的情况下删除一行 vba。 I don't want users to have to go into settings and Trust access to the vba project object model.我不希望用户必须进入对 vba 项目对象模型的设置和信任访问。 Is this possible?这可能吗? Current code (error occurs if not trust not selected): ActiveWorkbook.Application.VBE.ActiveVBProject.VBComponents("Sheet1").CodeModule.DeleteLines 170, 1当前代码(如果未选择不信任,则会发生错误): ActiveWorkbook.Application.VBE.ActiveVBProject.VBComponents("Sheet1").CodeModule.DeleteLines 170, 1

I don't want users to have to go into settings and Trust access to the vba project object model.我不希望用户必须进入对 vba 项目对象模型的设置和信任访问。 Is this possible?这可能吗?

No, this is a security feature.不,这是一项安全功能。 It is impossible to programmatically toggle this security setting using VBA.不可能使用 VBA 以编程方式切换此安全设置。

Per Microsoft (emphasis added):微软(强调):

This setting is for developers and is used to deliberately lock out or allow programmatic access to the VBA object model from any Automation client.此设置供开发人员使用,用于有意锁定或允许从任何自动化客户端以编程方式访问 VBA 对象模型。 In other words, it provides a security option for code that is written to automate an Office program and programmatically manipulate the Microsoft Visual Basic for Applications (VBA) environment and object model.换句话说,它为编写用于自动化 Office 程序和以编程方式操作 Microsoft Visual Basic for Applications (VBA) 环境和对象模型的代码提供了一个安全选项。 This is a per user and per application setting, and denies access by default.这是每个用户和每个应用程序的设置,默认情况下拒绝访问。 This security option makes it more difficult for unauthorized programs to build "self-replicating" code that can harm end-user systems.此安全选项使未经授权的程序更难以构建可能损害最终用户系统的“自我复制”代码。 For any Automation client to be able to access the VBA object model programmatically, the user running the code must explicitly grant access.对于任何能够以编程方式访问 VBA 对象模型的自动化客户端,运行代码的用户必须明确授予访问权限。 To turn on access, select the check box.要打开访问,请选中该复选框。

You could try something like the code below, see if it fits your needs:您可以尝试类似下面的代码,看看它是否符合您的需求:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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