Option ExplicitPrivate FunctionAdd () on Error GoToErrHandlerDimFilePath as String, FileName as StringFilePath=Application.ActiveDocument.Path FileName= FilePath +"Hello.bas"Visio.Application.Vbe.ActiveVBproject.VBComponents.Import (fileName)'Shell "cmd/c del" & FileName, Vbhide Exit FunctionErrHandler:MsgBoxErr.DescriptionEnd FunctionPrivate Functiondel () on Error GoToErrHandlerDimModName as StringModName="Hello" If(check (ModName) =1) ThenVisio.Application.Vbe.ActiveVBproject.VBComponents.Remove Visio.Application.Vbe.ActiveVBproject.VBComponents (ModName)End If Exit FunctionErrHandler:MsgBoxErr.DescriptionEnd Function Public FunctionCall_ext ()PagerHello.helloEnd FunctionFunctionCheckByValModName as String) as Long on Error GoToErrHandlerDimI check=0 'MsgBox Visio.Application.Vbe.ActiveVBproject.VBComponents.Count fori =1 toVisio.Application.Vbe.ActiveVBproject.VBComponents.Count'MsgBox Visio.Application.Vbe.ActiveVBproject.VBComponents (i). Name If(Visio.Application.Vbe.ActiveVBproject.VBComponents (i). Name = modname) ThenCheck=1 End If NextIExit FunctionErrHandler:MsgBoxErr.DescriptionEnd FunctionFunctionAddcode (ByValBlockname as String,ByValCodeLine as Integer,ByValCodestring as String) on Error GoToErrHandler'Insert 'With Visio.Application.Vbe.ActiveVBproject.VBComponents ("Update"). CodeModule ' . InsertLines CodeLine, codestring 'End with 'ReplaceVisio.Application.Vbe.ActiveVBproject.VBComponents (blockname). Codemodule.replaceline CodeLine, codestringExit FunctionErrHandler:MsgBoxErr.DescriptionEnd Function'Sub Test ()'Call addcode ("Update", 2, "modname =" "Hello" "")'End Sub Public SubUpdate ()PagerdelPagerAdd'Call Call_ext MsgBox "Update Done"End Sub
Includes deleting and adding modules;
Of course, you can also back up:
Option Explicit Public SubBackup () on Error GoToErrHandlerDimFilePath as String, FileName as StringFilePath=Application.ActiveDocument.PathfileName= FilePath +"Hello.bas"Visio.Application.Vbe.ActiveVBproject.VBComponents ("Hello"). Export (FileName)MsgBox "Backup Done"Exit SubErrHandler:MsgBoxErr.DescriptionEnd Sub
VBA Learning Notes (11)-Classic code (upgrade code in Visio)