我终于得到了所有代码。感谢所有人的一些很棒的链接。现在是最后一个障碍。
我将此文件发送到多个电台。并非所有PC上都有Office,可能只有Excel Viewer。我在工作表中做了一个说明来解释他们需要在带Office的后台办公机器上运行它。
所以我遇到的问题是,当他们点击电子邮件按钮或其他按钮时,我收到错误“1004程序化访问Visual Basics。项目不受信任。”
所以我知道我可以要求最终用户通过设置/宏部分并启用对VBA项目的访问。
这是一种将其编码到工作表中的方法,因此当他们打开文件时,它只启用该选项吗?或者我可以添加另一个按钮来启用它吗?下面是VB SCript。
由于
克里斯
Private Sub EmailWorkbook_Click()
'This subroutine uses ...
'Public Function EmailThisWorkbook(Optional ByVal TO_Recipients As String = "", Optional ByVal CC_Recipients As String = "", Optional ByVal BCC_Recipients As String = "", Optional ByVal Message_SUBJECT As String = "", Optional ByVal Message_BODY As String = "", Optional ByVal FROM_Sender As String = "")
On Error Resume Next
Dim StationCode As String
StationCode = Left(Environ$("computername"), 3)
'Grabs the first 3 characters, starting from left side, of the environmental variable 'computername' where the email is being sent from
Dim SendTO As String
SendTO = "me@somwhere.net"
'or use... SendTO = "" ... to prompt user for an email address
Dim BSOSubject As String
BSOSubject = "BSO Scanner Inventory Report for " & StationCode
Dim BSOBody As String
BSOBody = "BSO Scanner Inventory Report for " & StationCode
Message_Result = ThisWorkbook.EmailThisWorkbook(SendTO, "", "", BSOSubject, BSOBody, "")
If Message_Result = -2 Then
MsgBox "The Send function is disabled while workbook contains unsaved changes."
End If
If Message_Result = -1 Then
MsgBox "Email Cancelled"
End If
If Message_Results = 1 Then
MsgBox "Email Sent Succesfully"
End If
If Message_Result = 0 Then
MsgBox "Email Failed To Send"
End If
End Sub
下一个子程序:
Private Sub AddDevice_Click()
Dim TargetWorkSheet_Wks As Worksheet
Set TargetWorkSheet_Wks = ActiveSheet
TargetWorkSheet_Wks.Unprotect ("chris")
Dim Button_GUI_Obj As OLEObject
Dim InsertionRow_Str As String
InsertionRow_Str = "C" & TargetWorkSheet_Wks.Range("B25").Value
Dim NewRowNumber_Int
NewRowNumber_Int = TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(-1).Value + 1
Dim NewButtonID_Str As String
TargetWorkSheet_Wks.Range(InsertionRow_Str).EntireRow.Insert shift:=xlDown
TargetWorkSheet_Wks.Range(InsertionRow_Str).EntireRow.RowHeight = 30
'Column -1
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, -1).HorizontalAlignment = xlCenter
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, -1).VerticalAlignment = xlCenter
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, -1).WrapText = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, -1).Interior.Color = RGB(0, 0, 0)
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, -1).Borders.Weight = xlThick
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, -1).Font.Name = "Calibri"
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, -1).Font.Size = 18
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, -1).Font.Color = RGB(0, 0, 0)
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, -1).Font.Bold = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, -1).Font.Italic = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, -1).Font.Underline = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, -1).Font.Strikethrough = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, -1).Font.Subscript = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, -1).Font.Superscript = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, -1) = ""
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, -1).Locked = True
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 0).HorizontalAlignment = xlCenter
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 0).VerticalAlignment = xlCenter
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 0).WrapText = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 0).Interior.Color = RGB(150, 54, 52)
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 0).Borders.LineStyle = xlNone
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 0).Font.Name = "Calibri"
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 0).Font.Size = 18
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 0).Font.Color = RGB(255, 255, 255)
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 0).Font.Bold = True
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 0).Font.Italic = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 0).Font.Underline = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 0).Font.Strikethrough = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 0).Font.Subscript = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 0).Font.Superscript = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 0) = NewRowNumber_Int
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 0).Locked = True
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 1).HorizontalAlignment = xlCenter
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 1).VerticalAlignment = xlCenter
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 1).WrapText = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 1).Interior.Color = RGB(255, 204, 52)
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 1).Borders.Weight = xlThick
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 1).Font.Name = "Calibri"
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 1).Font.Size = 18
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 1).Font.Color = RGB(0, 0, 0)
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 1).Font.Bold = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 1).Font.Italic = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 1).Font.Underline = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 1).Font.Strikethrough = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 1).Font.Subscript = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 1).Font.Superscript = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 1) = TargetWorkSheet_Wks.Range("D18").Value
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 1).Locked = True
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 2).HorizontalAlignment = xlCenter
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 2).VerticalAlignment = xlCenter
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 2).WrapText = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 2).Interior.Color = RGB(204, 51, 0)
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 2).Borders.Weight = xlThick
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 2).Font.Name = "Calibri"
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 2).Font.Size = 18
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 2).Font.Color = RGB(255, 255, 255)
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 2).Font.Bold = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 2).Font.Italic = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 2).Font.Underline = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 2).Font.Strikethrough = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 2).Font.Subscript = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 2).Font.Superscript = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 2) = TargetWorkSheet_Wks.Range("E18").Value
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 2).Locked = True
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 3).HorizontalAlignment = xlCenter
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 3).VerticalAlignment = xlCenter
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 3).WrapText = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 3).Interior.Color = RGB(0, 112, 192)
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 3).Borders.Weight = xlThick
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 3).Font.Name = "Calibri"
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 3).Font.Size = 18
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 3).Font.Color = RGB(255, 255, 255)
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 3).Font.Bold = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 3).Font.Italic = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 3).Font.Underline = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 3).Font.Strikethrough = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 3).Font.Subscript = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 3).Font.Superscript = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 3) = TargetWorkSheet_Wks.Range("F18").Value
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 3).Locked = True
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 4).HorizontalAlignment = xlCenter
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 4).VerticalAlignment = xlCenter
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 4).WrapText = True
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 4).Interior.Color = RGB(230, 230, 230)
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 4).Borders.Weight = xlThick
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 4).Font.Name = "Calibri"
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 4).Font.Size = 10
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 4).Font.Color = RGB(0, 0, 0)
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 4).Font.Bold = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 4).Font.Italic = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 4).Font.Underline = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 4).Font.Strikethrough = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 4).Font.Subscript = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 4).Font.Superscript = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 4) = TargetWorkSheet_Wks.Range("G18").Value
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 4).Locked = True
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 5).HorizontalAlignment = xlCenter
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 5).VerticalAlignment = xlCenter
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 5).WrapText = True
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 5).Interior.Color = RGB(230, 230, 230)
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 5).Borders.Weight = xlThick
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 5).Font.Name = "Calibri"
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 5).Font.Size = 10
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 5).Font.Color = RGB(0, 0, 0)
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 5).Font.Bold = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 5).Font.Italic = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 5).Font.Underline = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 5).Font.Strikethrough = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 5).Font.Subscript = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 5).Font.Superscript = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 5) = TargetWorkSheet_Wks.Range("H18").Value
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 5).Locked = True
NewButtonID_Str = Year(Now()) & Month(Now()) & Day(Now()) & Hour(Now()) & Minute(Now()) & Second(Now())
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 6).HorizontalAlignment = xlCenter
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 6).VerticalAlignment = xlCenter
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 6).WrapText = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 6).Interior.Color = RGB(150, 54, 52)
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 6).Borders.LineStyle = xlNone
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 6).Borders(xlEdgeLeft).Weight = xlThick
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 6).Font.Name = "Calibri"
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 6).Font.Size = 18
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 6).Font.Color = RGB(150, 54, 52)
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 6).Font.Bold = True
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 6).Font.Italic = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 6).Font.Underline = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 6).Font.Strikethrough = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 6).Font.Subscript = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 6).Font.Superscript = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 6) = NewButtonID_Str
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 6).Locked = True
Call Add_CommandButton(1, "Save_" & NewButtonID_Str, "Save", InsertionRow_Str, 30, 23.25, 20, 3, "SaveOrUndoDevice")
Set Button_GUI_Obj = TargetWorkSheet_Wks.OLEObjects("Save_" & NewButtonID_Str)
Button_GUI_Obj.Visible = False
Call Add_CommandButton(1, "Undo_" & NewButtonID_Str, "Undo", InsertionRow_Str, 33, 23.25, 51, 3, "SaveOrUndoDevice")
Set Button_GUI_Obj = TargetWorkSheet_Wks.OLEObjects("Undo_" & NewButtonID_Str)
Button_GUI_Obj.Visible = False
Call Add_CommandButton(1, "Edit_" & NewButtonID_Str, "Edit", InsertionRow_Str, 30, 23.25, 20, 3, "EditDevice")
Call Add_CommandButton(1, "Up_" & NewButtonID_Str, "U", InsertionRow_Str, 18, 23.25, 51, 3, "MoveDevice")
Call Add_CommandButton(1, "Down_" & NewButtonID_Str, "D", InsertionRow_Str, 15, 23.25, 70, 3, "MoveDevice")
If TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(-1, 0).Value = 0 Then
Set Button_GUI_Obj = TargetWorkSheet_Wks.OLEObjects("Up_" & NewButtonID_Str)
Button_GUI_Obj.Enabled = False
End If
If TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(1, 0).Value = "x" Then
Set Button_GUI_Obj = TargetWorkSheet_Wks.OLEObjects("Down_" & NewButtonID_Str)
Button_GUI_Obj.Enabled = False
End If
If NewRowNumber_Int > 1 Then
PreviousRecord = TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(-1, 6).Value
Set Button_GUI_Obj = TargetWorkSheet_Wks.OLEObjects("Down_" & PreviousRecord)
Button_GUI_Obj.Enabled = True
End If
Call Add_CommandButton(1, "Delete_" & NewButtonID_Str, "X", InsertionRow_Str, 15, 23.25, 85, 3, "RemoveDevice")
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 7).HorizontalAlignment = xlCenter
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 7).VerticalAlignment = xlCenter
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 7).WrapText = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 7).Interior.Color = RGB(0, 0, 0)
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 7).Borders.Weight = xlThick
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 7).Font.Name = "Calibri"
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 7).Font.Size = 18
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 7).Font.Color = RGB(0, 0, 0)
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 7).Font.Bold = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 7).Font.Italic = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 7).Font.Underline = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 7).Font.Strikethrough = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 7).Font.Subscript = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 7).Font.Superscript = False
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 7) = ""
TargetWorkSheet_Wks.Range(InsertionRow_Str).Offset(0, 7).Locked = True
TargetWorkSheet_Wks.Range("B25") = TargetWorkSheet_Wks.Range("B25").Value + 1
TargetWorkSheet_Wks.Protect ("chris")
End Sub
答案 0 :(得分:1)
VBA无法关闭VBA安全性,以帮助阻止macro viruses。软件无法轻易禁用病毒扫描程序,这是一件好事。但是,可能还有其他解决方案。 (从技术上讲,您可以使用VBA更改注册表,除非更改在重新启动Excel之后才会生效,重新启动Excel将重置与安全相关的设置。)
这些用户是否都在同一个网络上?同一工作组的一部分?如果是这样,您可以使用组{ - 3}}或类似选项this等组策略编辑器启用宏。
您可以将Excel文件所在的文件夹设置为here。
您可以向用户发送.REG
文件以更新启用宏信任的注册表。这把钥匙:
HKEY_CURRENT_USER\Software\Microsoft\Office\_{version}_\Excel\Security\VBAWarnings
可以设置为:
1 =启用所有宏(不推荐)
2 =禁用所有通知
3 =禁用除数字签名宏之外的所有宏 4 =禁用所有通知
更多信息Trusted Location或here。
例如,如果用户在Windows 2000+上运行Excel 16,您可以创建一个名为EnableMacros.reg
的文件,如下所示:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Excel\Security]
"VBAWarnings"=dword:1
...并将其分发给您的用户,并在打开Excel之前执行该操作。他们会得到一个警告,即Windows可能会停止正常工作"虽然可以通过静默打开.REG
文件来避免,也许可以使用批处理文件中的命令:
regedit.exe /s enablemacros.reg
...虽然如果用户的here受到限制(并且registry permissions也无法regedit.exe
运行),则更改可能不会生效