编写【文件新建】【文件打开】【文件关闭】等简单宏代码,将自己的学号、姓名,作为内容在弹出的消息窗口上显示
时间: 2023-05-18 10:02:33 浏览: 83
单片机显示自己的学号源代码(汇编语言)
以下是一个简单的宏代码,可以实现文件新建、文件打开和文件关闭功能,并在弹出的消息窗口上显示您的学号和姓名:
Sub FileOperations()
Dim studentID As String
Dim studentName As String
studentID = "123456789"
studentName = "张三"
Dim filePath As String
filePath = "C:\Users\" & studentName & "\Documents\"
' 新建文件
Dim newFileName As String
newFileName = InputBox("请输入新建文件的名称:")
If newFileName <> "" Then
Dim newFilePath As String
newFilePath = filePath & newFileName & ".txt"
Open newFilePath For Output As #1
Print #1, "这是一个新建的文件。"
Close #1
MsgBox "文件已成功新建:" & newFilePath & vbCrLf & "学号:" & studentID & vbCrLf & "姓名:" & studentName
End If
' 打开文件
Dim openFileName As String
openFileName = InputBox("请输入要打开的文件名:")
If openFileName <> "" Then
Dim openFilePath As String
openFilePath = filePath & openFileName & ".txt"
If Dir(openFilePath) <> "" Then
Open openFilePath For Input As #1
Dim fileContent As String
fileContent = Input$(LOF(1), 1)
Close #1
MsgBox "文件内容:" & vbCrLf & fileContent & vbCrLf & "学号:" & studentID & vbCrLf & "姓名:" & studentName
Else
MsgBox "文件不存在:" & openFilePath
End If
End If
' 关闭文件
Dim closeFileName As String
closeFileName = InputBox("请输入要关闭的文件名:")
If closeFileName <> "" Then
Dim closeFilePath As String
closeFilePath = filePath & closeFileName & ".txt"
If Dir(closeFilePath) <> "" Then
Close #1
MsgBox "文件已成功关闭:" & closeFilePath & vbCrLf & "学号:" & studentID & vbCrLf & "姓名:" & studentName
Else
MsgBox "文件不存在:" & closeFilePath
End If
End If
End Sub
阅读全文