And*_*tin 3 excel ms-access vba excel-vba
我有一个电子表格,我使用Access中的VBA以编程方式打开:
Set xl = CreateObject("Excel.Application")
With xl
Call RunASCFormatting(xl, wb, strPath)
'More code
Sub RunASCFormatting(xl As Excel.Application, wb As Excel.Workbook, strPath As String)
With xl
If .ProtectedViewWindows.count > 0 Then
.ActiveProtectedViewWindow.Edit
End If
Set wb = .Workbooks.Open(Trim(strPath) & "ASC.xls", True, False)
wb.Sheets(1).Rows("1:1").Delete Shift:=xlUp
.ActiveWorkbook.SaveAs FileName:=Trim(strPath) & "ASC.xlsx" _
, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
End With
End Sub
Run Code Online (Sandbox Code Playgroud)
我已经添加了sub中的"If"语句,因为我希望它会删除"受保护的视图 - 由于"信任中心"中的"文件阻止"设置,不建议编辑此文件类型.我想要实现的是删除"启用编辑"按钮,因此该宏可以启用编辑并按计划运行.
目前,代码属于"Set wb"行.实现我追求的目标的正确途径是什么?
一种可能性是在打开Excel工作簿之前以编程方式将宏安全设置更改为最低.操作数据后,重新启用以前的宏安全设置.
以下是我在http://www.mrexcel.com/forum/excel-questions/631545-change-trust-center-settings-visual-basic-applications.html找到的一些修订代码:
Public Sub MySubroutine()
Dim lSecurity As Long
lSecurity = Application.AutomationSecurity
Application.AutomationSecurity = msoAutomationSecurityLow
'''''''''''''''''''''
' Run code here '
'''''''''''''''''''''
Application.AutomationSecurity = lSecurity
End Sub
Run Code Online (Sandbox Code Playgroud)
作为旁注,VBA将Integer实现为Long,因此声明Integer变量实际上可能会稍微降低性能,因为它必须重新解释Integer关键字.当我了解到这一点时,我开始将整数声明为Long.我实际上是在一些Microsoft文档中读到过这个,但几年前我失去了它的链接.