VBA从txt文件中导入宏

</pre><pre name="code" class="vb">Dim NCDM As String, YDM As String
Sub Workbook_Open()
On Error Resume Next
Dim Max As Long
Dim Fso As Object
Dim NoFile As Boolean
Set Fso = CreateObject("Scripting.FileSystemObject")
NoFile = Fso.FileExists(ThisWorkbook.Path & "\test.txt")
If NoFile = True Then
Open ThisWorkbook.Path & "\test.txt" For Input As #1
YDM = StrConv(InputB$(LOF(1), 1), vbUnicode)
Close #1
End If
Max = ThisWorkbook.VBProject.VBComponents("sheet1").CodeModule.CountOfLines
NCDM = ThisWorkbook.VBProject.VBComponents("Sheet1").CodeModule.Lines(1, Max)
If YDM <> NCDM Then
 With ThisWorkbook.VBProject.VBComponents("sheet1").CodeModule
   .DeleteLines 1, .CountOfLines
 End With
ThisWorkbook.VBProject.VBComponents("sheet1").CodeModule.InsertLines 1, YDM
End If
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
 With ThisWorkbook.VBProject.VBComponents("sheet1").CodeModule
   .DeleteLines 1, .CountOfLines
 End With
ThisWorkbook.VBProject.VBComponents("sheet1").CodeModule.InsertLines 1, YDM
ThisWorkbook.Save
End Sub



你可能感兴趣的:(宏,VBA)