新建From1(窗体),新建Command1(按钮CommandButton),代码:
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Const SW_SHOWNORMAL = 1
Private Sub OpenNotepadWithFile(filePath As String)
Dim returnValue As Long
' 使用 ShellExecute 打开记事本并加载指定文件
returnValue = ShellExecute(0, "open", "notepad", Chr(34) & filePath & Chr(34), "", SW_SHOWNORMAL)
' 检查是否成功执行
If returnValue <= 32 Then
MsgBox "无法打开记事本", vbExclamation
End If
End Sub
Private Sub Command1_Click()
Dim filePath As String
filePath = App.Path & "\123.txt"
Call OpenNotepadWithFile(filePath)
End Sub