重新启动后 Outlook 未运行 Visual Basic [英] Outlook Not Running Visual Basic After Restart
问题描述
所以我在 Outlook 中创建了一个可视化的基本脚本,它通过从 Git 中提取来创建一个随机签名.
脚本可以正常工作,但是每当我重新启动机器时,脚本根本不会运行.
我通过转到
解决了这个问题文件"->选项"->信任中心"->信任中心设置..."->宏设置"->启用所有宏"
这让 VBA 代码在我打开和关闭 Outlook 时都可以工作,但有没有更好的方法让我在重新打开 Outlook 或重新启动机器时运行代码.
我试过使用
私有子Application_Startup()MsgBox "你好"结束子
虽然我第一次放入该代码时确实有效,但每当我重新启动 Outlook 时,它都会说它无法运行,因为宏被禁用"
这是我的随机签名代码,无论如何要在我重新启动 Outlook 或我的机器时进行这项工作?还是我编辑的宏设置方法正确?
Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)' 验证发送的项目是电子邮件.如果 Item.Class <>olMail 然后退出子'这些第一个变量是找到在 AppData 文件夹中创建的 .bat 文件的文件'将环境设置为 %APPDATA%昏暗的环境作为字符串环境 = CStr(环境(APPDATA"))'创建一个新变量来设置 RepoDir.txt 的文件路径RepoPath = 环境 &"RepoDir.txt"'创建一个新变量来抓取 RepoDir.txt 中的文本行Dim RepoFilePath 作为字符串Dim strFirstLine 作为字符串'新变量调用 RepoPath 变量,打开它并读取文件的第一行并将其复制到一个变量中RepoFilePath = RepoPath打开 RepoFilePath 作为输入 #1行输入 #1,strFirstLine关闭 #1'该脚本运行一个打开命令行的 Shell 命令,cds 到 str 变量中的 Repo 路径,执行 git pull,并将错误级别输出到临时目录中的文件Shell ("cmd/c cd" & strFirstLine & " & git pull RandomSig & echo %ERRORLEVEL% > %TEMP%gitPull.txt 2>&1")'这些第二组变量是在TEMP文件夹中找到Shell命令创建的文件'将环境设置为 %TEMP%Dim Gitenviro 作为字符串Gitenviro = CStr(Environ("TEMP"))'创建一个新变量来设置 RepoDir.txt 的文件路径PullResult = Gitenviro &gitPull.txt"'创建一个新变量来抓取 RepoDir.txt 中的文本行Dim GitFilePath As StringDim GitFirstLine 作为字符串'新变量调用PullResult变量,打开它并读取文件的第一行并将其复制到一个变量中GitFilePath = PullResult打开 GitFilePath 作为输入 #2行输入 #2,GitFirstLine关闭 #2'MsgBox (GitFirstLine)'检查变量是否不等于0,如果不等于则取消消息如果 GitFirstLine <>0 那么MsgBox "尝试执行 Git Pull 时出错,正在取消消息"取消 = 真万一Const SearchString = "%Random_Line%"Dim QuotesFile 作为字符串QuotesFile = strFirstLine &引用.txt"如果 InStr(Item.Body, SearchString) 那么如果 FileOrDirExists(QuotesFile) = False 那么MsgBox ("未找到报价文件!取消消息")取消 = 真别的Dim lines() 作为字符串Dim numLines 作为整数行数 = 0'打开文件进行读取打开 QuotesFile 作为输入 #1' 遍历文件中的每一行并将其保存在数组中 + 计数做直到 EOF(1)ReDim 保留行(numLines + 1)行输入 #1,行(numLines)numLines = numLines + 1环形关闭 #1' 获取随机行号Dim randLine 作为整数randLine = Int(numLines * Rnd()) + 1' 插入随机引号Item.HTMLBody = Replace(Item.HTMLBody, SearchString,lines(randLine))Item.HTMLBody = Replace(Item.HTMLBody, "%Random_Num%", randLine)万一万一结束子函数 FileOrDirExists(PathName As String)将 iTemp 调暗为整数出错时继续下一步iTemp = GetAttr(PathName)选择案例错误号案例是 = 0FileOrDirExists = True其他情况FileOrDirExists = False结束选择出错时转到 0结束函数
强烈建议您将宏安全设置保留为仅允许
转到开发人员"选项卡>,单击Visual Basic".或 ALT+F11
在 Visual Basic 编辑器中,转到工具 > 数字签名.
数字签名"对话框出现并单击选择",您将看到一个用于选择证书的屏幕.现在您可以选择刚刚创建的证书.
<小时>
编辑
定位 SelfCert.exe
转到开始"菜单并输入 VBA
应该会显示 SelfCert.exe
.
定位SelfCert.exe
如果您在开始"菜单中找不到它?那么默认情况下你可以在以下位置找到SelfCert.exe
Windows 32 位
C:Program FilesMicrosoft OfficeOffice <版本号>
Windows 64 位与 Office 32 位
C:Program Files (x86)Microsoft OfficeOffice <版本号>
Windows 64 位与 Office 64 位
C:Program FilesMicrosoft OfficeOffice <版本号>
Office 365(基于订阅或即点即用的 Office 2013 版本)
C:Program FilesMicrosoft Office 15
ootoffice15
如果 SelfCert.exe
没有安装
然后运行 Office 安装程序并选择添加或删除功能
.
对于旧版本的 Office,您需要选择自定义安装
,然后选择高级自定义
.
展开 Office 共享功能
部分并选择 VBA 项目的数字证书
以从您的计算机运行.
找到后直接运行SelfCert.exe
即可.
So I have created a visual basic script in outlook that creates a random signature by pulling from Git.
The script works correctly but whenever I restart my machine the script doesn't run at all.
I fixed the issue by going to
"File"->"Options"->"Trust Center"->"Trust Center Settings..."->"Macro Settings"->"Enable all macros"
This let the VBA code work whenever I opened and closed Outlook but is there a better way to have the code work whenever I reopen Outlook or restart my machine.
I have tried to use
Private Sub Application_Startup()
MsgBox "Hi"
End Sub
While that code did work when I first put it in, whenever I restarted outlook it said it couldn't run because "Macros were disabled"
Here is my code for the random signature, anyway to have this work whenever I restart outlook or my machine? Or is the macro setting I edited the correct way to go?
Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
' Validate that the item sent is an email.
If Item.Class <> olMail Then Exit Sub
'These first variables is to find the file the .bat file created within the AppData folder
'Set enviro to %APPDATA%
Dim enviro As String
enviro = CStr(Environ("APPDATA"))
'Create a new variable that sets the file path for the RepoDir.txt
RepoPath = enviro & "RepoDir.txt"
'Create a new variable to grab the line of text in RepoDir.txt
Dim RepoFilePath As String
Dim strFirstLine As String
'The new variable calls the RepoPath Variable, opens it and reads the first line of the file and copies it into a variable
RepoFilePath = RepoPath
Open RepoFilePath For Input As #1
Line Input #1, strFirstLine
Close #1
'The script runs a Shell command that opens the command line, cds to the Repo path within the str variable, does a git pull, and outputs the error level to a file in the temp directory
Shell ("cmd /c cd " & strFirstLine & " & git pull RandomSig & echo %ERRORLEVEL% > %TEMP%gitPull.txt 2>&1")
'These second set of variables is to find the file the Shell command created within the TEMP folder
'Set enviro to %TEMP%
Dim Gitenviro As String
Gitenviro = CStr(Environ("TEMP"))
'Create a new variable that sets the file path for the RepoDir.txt
PullResult = Gitenviro & "gitPull.txt"
'Create a new variable to grab the line of text in RepoDir.txt
Dim GitFilePath As String
Dim GitFirstLine As String
'The new variable calls the PullResult Variable, opens it and reads the first line of the file and copies it into a variable
GitFilePath = PullResult
Open GitFilePath For Input As #2
Line Input #2, GitFirstLine
Close #2
'MsgBox (GitFirstLine)
'The variable is checked to see if it does not equal 0, and if it doesn't the message is cancelled
If GitFirstLine <> 0 Then
MsgBox "There was an error when attempting to do the Git Pull, cancelling message"
Cancel = True
End If
Const SearchString = "%Random_Line%"
Dim QuotesFile As String
QuotesFile = strFirstLine & "quotes.txt"
If InStr(Item.Body, SearchString) Then
If FileOrDirExists(QuotesFile) = False Then
MsgBox ("Quotes file wasn't found! Canceling message")
Cancel = True
Else
Dim lines() As String
Dim numLines As Integer
numLines = 0
' Open the file for reading
Open QuotesFile For Input As #1
' Go over each line in the file and save it in the array + count it
Do Until EOF(1)
ReDim Preserve lines(numLines + 1)
Line Input #1, lines(numLines)
numLines = numLines + 1
Loop
Close #1
' Get the random line number
Dim randLine As Integer
randLine = Int(numLines * Rnd()) + 1
' Insert the random quote
Item.HTMLBody = Replace(Item.HTMLBody, SearchString, lines(randLine))
Item.HTMLBody = Replace(Item.HTMLBody, "%Random_Num%", randLine)
End If
End If
End Sub
Function FileOrDirExists(PathName As String)
Dim iTemp As Integer
On Error Resume Next
iTemp = GetAttr(PathName)
Select Case Err.Number
Case Is = 0
FileOrDirExists = True
Case Else
FileOrDirExists = False
End Select
On Error GoTo 0
End Function
It’s highly recommended to leave your macro security setting to only allow self-sign certificate Macros,
Do not use the Low option or run all
Create a self-signing certificate
Go to Start > All Programs > Microsoft Office > Microsoft Office Tools, and then click Digital Certificate for VBA Projects.
In the Your certificate's name box, type in name for the certificate.
Click OK. then SelfCert Success message will appears, click OK.
Go to Developer tab > click Visual Basic. or ALT+F11
In Visual Basic Editor, go to Tools > Digital Signature.
Digital Signature dialog appears and click on Choose and you’ll get a screen to select a certificate. Now you can choose the certificate you just created.
Edit
locating SelfCert.exe
Go to Start menu and typing VBA
should bring up the SelfCert.exe
.
Alternative method of locating SelfCert.exe
if you Can’t find it in the Start Menu? then By default you can find SelfCert.exe
in the following location
Windows 32-bit
C:Program FilesMicrosoft OfficeOffice <version number>
Windows 64-bit with Office 32-bit
C:Program Files (x86)Microsoft OfficeOffice <version number>
Windows 64-bit with Office 64-bit
C:Program FilesMicrosoft OfficeOffice <version number>
Office 365 (Subscription based or Click-to-Run version of Office 2013)
C:Program FilesMicrosoft Office 15
ootoffice15
If SelfCert.exe
is not installed
Then run Office setup and choose Add or Remove Features
.
With older versions of Office you’ll need to choose Custom installation
and then Advanced customization
.
Expand the Office Shared Features
section and select Digital Certificate for VBA Projects
to run from your computer.
Simply run SelfCert.exe
after locating it.
这篇关于重新启动后 Outlook 未运行 Visual Basic的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!