这是要加入的vba 函数 功能是关闭痕迹
Public Function vbaCloseRev() As String
Dim vbaCloseRevFunction As String
vbaCloseRevFunction = "Public Sub CloseRev()" + vbCrLf
vbaCloseRevFunction = vbaCloseRevFunction + "With ActiveDocument" + vbCrLf
vbaCloseRevFunction = vbaCloseRevFunction + ".TrackRevisions = False" + vbCrLf
vbaCloseRevFunction = vbaCloseRevFunction + ".PrintRevisions = False" + vbCrLf
vbaCloseRevFunction = vbaCloseRevFunction + ".ShowRevisions = False" + vbCrLf
vbaCloseRevFunction = vbaCloseRevFunction + "End With" + vbCrLf
vbaCloseRevFunction = vbaCloseRevFunction + "End Sub" + vbCrLf
vbaCloseRev = vbaCloseRevFunction
End Function
'初始化形成VBA模块
Dim MacroStr As String
MacroStr =vbaCloseRev()
im mFile As String
Dim fso As New Scripting.FileSystemObject
mFile = "c:\MacroTempFile.txt"
fso.CreateTextFile(mFile, True).Write MacroStr '保存vba代码临时文件
Dim c As Integer
Set oProject = oDoc.VBProject
Set oComponents = oProject.VBComponents
c = oComponents.Count '获取当前文档中的vba模块数目
If c < 2 Then
Set oComponent = oComponents.Add(vbext_ct_StdModule) '如果没有模块新建一个模块
Set oModule = oComponent.CodeModule
oModule.AddFromFile mFile '用文件填充新模块
Else
'如果已经存在一个模块 清除模块内的代码
'然后再重新填充代码
With oDoc.VBProject.VBComponents.Item(1).CodeModule
.DeleteLines 1, .CountOfLines
End With
oDoc.VBProject.VBComponents.Item(1).CodeModule.AddFromFile mFile
先要在EXCEL宏安全性的选项内,勾选信任VBA存取项目
Sub test1()
Dim a As Workbook
Set a = Workbooks.Open("d:\test.xls")
Dim code As String
code = code & "Private Sub Command2_Click()" & vbCrLf
code = code & "MsgBox ""有趣吧?"", 0, "":P""" & vbCrLf
code = code & "End Sub" & vbCrLf
With a.VBProject.VBComponents("Sheet1").CodeModule
NextLine = .CountOfLines + 1
.InsertLines NextLine, code
End With