gpt4 book ai didi

vba - 重新启动后 Outlook 不运行 Visual Basic

转载 作者:行者123 更新时间:2023-12-04 19:41:36 27 4
gpt4 key购买 nike

所以我在 outlook 中创建了一个可视化基本脚本,它通过从 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

最佳答案

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

Do not use the Low option or run all

创建自签名证书

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

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

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

enter image description here

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

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

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


编辑

定位 SelfCert.exe

转到“开始”菜单并键入 VBA 应该调出 SelfCert.exe .

enter image description here

定位的替代方法SelfCert.exe

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

Windows 32 位

C:\Program Files\Microsoft Office\Office <version number>

Windows 64 位与 Office 32 位

C:\Program Files (x86)\Microsoft Office\Office <version number>

Windows 64 位与 Office 64 位

C:\Program Files\Microsoft Office\Office <version number>

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找到后。

关于vba - 重新启动后 Outlook 不运行 Visual Basic,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31617344/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com