重新启动后,Outlook无法运行Visual Basic [英] Outlook Not Running Visual Basic After Restart
问题描述
因此,我在Outlook中创建了一个视觉基本脚本,该脚本通过从Git中提取来创建随机签名.
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.
我通过
"File"->"Options"->"Trust Center"->"Trust Center Settings..."->"Macro Settings"->"Enable all macros"
这使VBA代码在我每次打开和关闭Outlook时都能工作,但是有一种更好的方法让我在每次重新打开Outlook或重新启动计算机时都能工作.
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.
我尝试使用
Private Sub Application_Startup()
MsgBox "Hi"
End Sub
虽然该代码在我初次输入时确实有效,但是每当我重新启动Outlook时,它都说由于宏被禁用"而无法运行
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"
这是我的随机签名代码,无论如何,无论何时重新启动Outlook或我的机器,这项工作都可以进行?还是我编辑的宏设置是正确的操作方式?
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
创建自签名证书
-
转到开始> 所有程序> Microsoft Office > Microsoft Office工具,然后单击"VBA项目数字证书".
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.
单击确定".然后会出现SelfCert Success消息,单击OK.
Click OK. then SelfCert Success message will appears, click OK.
-
转到开发人员"选项卡> ,单击"Visual Basic".或 ALT + F11
Go to Developer tab > click Visual Basic. or ALT+F11
在Visual Basic编辑器中,转到工具" > 数字签名.
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.
修改
找到SelfCert.exe
locating SelfCert.exe
转到开始"菜单,然后输入 VBA
会弹出SelfCert.exe
.
Go to Start menu and typing VBA
should bring up the SelfCert.exe
.
定位SelfCert.exe
如果您在开始"菜单中找不到它?那么默认情况下,您可以在以下位置找到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位
Windows 32-bit
C:\Program Files\Microsoft Office\Office <version number>
带有Office 32位的Windows 64位
Windows 64-bit with Office 32-bit
C:\Program Files (x86)\Microsoft Office\Office <version number>
Windows 64位和Office 64位
Windows 64-bit with Office 64-bit
C:\Program Files\Microsoft Office\Office <version number>
Office 365(基于订阅的版本或Office 2013的即点即用版本)
Office 365 (Subscription based or Click-to-Run version of Office 2013)
C:\Program Files\Microsoft Office 15\root\office15
如果未安装SelfCert.exe
然后运行Office安装程序并选择Add or Remove Features
.
Then run Office setup and choose Add or Remove Features
.
对于较早版本的Office,您需要选择Custom installation
,然后选择Advanced customization
.
With older versions of Office you’ll need to choose Custom installation
and then Advanced customization
.
展开Office Shared Features
部分,然后选择Digital Certificate for VBA Projects
从计算机运行.
Expand the Office Shared Features
section and select Digital Certificate for VBA Projects
to run from your computer.
找到它后,只需运行SelfCert.exe
.
这篇关于重新启动后,Outlook无法运行Visual Basic的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!