VBA学习笔记(11)-经典代码之 (Visio中升级代码)

Option Explicit

Private Function add()
On Error GoTo ErrHandler
Dim filePath As String, fileName As String

  filePath = Application.ActiveDocument.Path
  fileName = filePath + "hello.bas"

  Visio.Application.Vbe.ActiveVBproject.VBComponents.Import (fileName)
  
  'Shell "cmd /c del " & fileName, vbHide

  Exit Function
ErrHandler:
  MsgBox Err.Description
End Function

Private Function del()
  On Error GoTo ErrHandler
  Dim modName As String
  
  modName = "hello"
  If (check(modName) = 1) Then
     Visio.Application.Vbe.ActiveVBproject.VBComponents.Remove Visio.Application.Vbe.ActiveVBproject.VBComponents(modName)
  End If
  Exit Function
ErrHandler:
  MsgBox Err.Description
End Function

Public Function call_ext()
   Call hello.hello
End Function

Function check(ByVal modName As String) As Long
  On Error GoTo ErrHandler
  
  Dim i
  check = 0
  'MsgBox Visio.Application.Vbe.ActiveVBproject.VBComponents.Count
  For i = 1 To Visio.Application.Vbe.ActiveVBproject.VBComponents.Count
     'MsgBox Visio.Application.Vbe.ActiveVBproject.VBComponents(i).Name
     If (Visio.Application.Vbe.ActiveVBproject.VBComponents(i).Name = modName) Then
        check = 1
     End If
  Next i
  
  Exit Function
ErrHandler:
  MsgBox Err.Description
End Function

Function AddCode(ByVal blockName As String, ByVal codeLine As Integer, ByVal codeString As String)
  On Error GoTo ErrHandler
  
  'insert
  'With Visio.Application.Vbe.ActiveVBproject.VBComponents("update").CodeModule
  '  .InsertLines codeLine, codeString
  'End With
  
  'replace
  Visio.Application.Vbe.ActiveVBproject.VBComponents(blockName).CodeModule.ReplaceLine codeLine, codeString
  
  Exit Function
ErrHandler:
  MsgBox Err.Description
End Function



'Sub test()
'   Call AddCode("update", 2, "modName = ""hello""")
'End Sub

Public Sub update()

  Call del
  Call add
  'Call call_ext
  
  MsgBox "Update Done"
End Sub

包括删除和增加模块;

 

当然也可以备份:

 

Option Explicit

Public Sub backup()
On Error GoTo ErrHandler
Dim filePath As String, fileName As String

filePath = Application.ActiveDocument.Path
fileName = filePath + "hello.bas"

Visio.Application.Vbe.ActiveVBproject.VBComponents("hello").Export (fileName)
MsgBox "Backup done"
Exit Sub
ErrHandler:
  MsgBox Err.Description
End Sub

 

转载于:https://www.cnblogs.com/xiyoulhj/p/4417556.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值