Noisewater
Noisewater

Reputation: 53

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. I don't want users to have to go into settings and Trust access to the vba project object model. Is this possible? Current code (error occurs if not trust not selected): ActiveWorkbook.Application.VBE.ActiveVBProject.VBComponents("Sheet1").CodeModule.DeleteLines 170, 1

Upvotes: 0

Views: 437

Answers (2)

Shai Rado
Shai Rado

Reputation: 33672

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

Upvotes: 2

David Zemens
David Zemens

Reputation: 53623

I don't want users to have to go into settings and Trust access to the vba project object model. Is this possible?

No, this is a security feature. It is impossible to programmatically toggle this security setting using 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. 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. 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. To turn on access, select the check box.

Upvotes: 2

Related Questions