【问题标题】:Outlook Not Running Visual Basic After Restart重新启动后 Outlook 未运行 Visual Basic
【发布时间】:2020-06-23 08:44:58
【问题描述】:

所以我在 Outlook 中创建了一个 Visual Basic 脚本,它通过从 Git 中提取来创建随机签名。

脚本运行正常,但每当我重新启动机器时,脚本根本不会运行。

我通过去

解决了这个问题
"File"->"Options"->"Trust Center"->"Trust Center Settings..."->"Macro Settings"->"Enable all macros"

这让 VBA 代码在我打开和关闭 Outlook 时工作,但有没有更好的方法让代码在我重新打开 Outlook 或重新启动机器时工作。

我尝试过使用

Private Sub Application_Startup() 

    MsgBox "Hi"

End Sub

虽然该代码在我第一次放入时确实有效,但每当我重新启动 Outlook 时,它都说它无法运行,因为“宏已禁用”

这是我的随机签名代码,无论如何,只要我重新启动 Outlook 或我的机器,就可以进行这项工作吗?还是我编辑的宏设置正确?

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

【问题讨论】:

  • 您是否尝试过创建自签名证书?如果您需要说明,请告诉我。
  • 我其实不知道什么是自签名证书
  • 只是跟进看看你是否能够找到SelfCert.exe更新的答案

标签: vba outlook


【解决方案1】:

强烈建议您将宏安全设置保留为仅允许 self-sign certificate 宏,

不要使用低选项或全部运行

创建自签名证书

  1. 转到开始 > 所有程序 > Microsoft Office > Microsoft Office 工具,然后单击 VBA 项目的数字证书。

  2. 在您的证书名称框中,输入证书的名称。

  3. 单击“确定”。然后将出现 SelfCert Success 消息,单击 OK。

  1. 转到开发人员选项卡>单击 Visual Basic。或 ALT+F11

  2. 在 Visual Basic 编辑器中,转到工具 > 数字签名。

  3. 出现数字签名对话框并单击选择,您将看到一个选择证书的屏幕。现在您可以选择刚刚创建的证书了。


编辑

定位 SelfCert.exe

进入开始菜单并输入 VBA 应该会弹出SelfCert.exe

另一种定位SelfCert.exe的方法

如果您在“开始”菜单中找不到它?那么默认情况下你可以在以下位置找到SelfCert.exe

Windows 32 位

C:\Program Files\Microsoft Office\Office &lt;version number&gt;

Windows 64 位和 Office 32 位

C:\Program Files (x86)\Microsoft Office\Office &lt;version number&gt;

Windows 64 位和 Office 64 位

C:\Program Files\Microsoft Office\Office &lt;version number&gt;

Office 365(Office 2013 的订阅版或即点即用版)

C:\Program Files\Microsoft Office 15\root\office15

如果SelfCert.exe没有安装

然后运行 ​​Office 安装程序并选择 Add or Remove Features

对于旧版 Office,您需要选择 Custom installation,然后选择 Advanced customization

展开Office Shared Features 部分并选择Digital Certificate for VBA Projects 以从您的计算机运行。

找到SelfCert.exe后,只需运行它。

【讨论】:

  • 太棒了,谢谢,可惜我工作的公司没有数字证书,有没有办法下载数字证书?
  • 为我解决了问题。
  • @appl3r 更多有用信息在这里stackoverflow.com/documentation/vba/7733/…
  • @0m3r 谢谢!
猜你喜欢
  • 1970-01-01
  • 2015-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多