Xóa Tên Addin ra khỏi danh sách Addin khi không sử dụng

  • Thread starter Thread starter vba_gpe
  • Ngày gửi Ngày gửi
Liên hệ QC
Bạn phải tạo chương trình bên VB6 hoặc ngôn ngữ khác, không phải chạy trong Excel. Làm đúng thứ tự như sau:

1. ExcelApp.Addins('YourAddin").Installed = False
2. Thoát Excel - ExcelApp.Quit.
3. Xóa các entries trong Registry liên quan
4. Mở lại Excel và kiểm tra nếu ExcelApp.Addins('YourAddin") is Nothing thì thông báo "Đã gỡ bỏ add-in thành công."
5. Thoát Excel - ExcelApp.Quit. Thoát chương trình.
Gửi Anh Tuân.
Em dùng vb6 có viết cái gở bỏ addin theo trình tự trên. Tham khảo ý kiến anh và mọi người:
Code khởi động:
Mã:
Sub Main()
On Error Resume next
Dim ExcelApp As Excel.Application
ExcelApp.AddIns("CSG").Installed = False
ExcelApp.Quit
'- Xoa addin trong regkey Addin Manager
If RegKeyExists("HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Add-in Manager\C:\test_addin.xla") = True Then
    RegKeyDelete ("HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Add-in Manager\C:\test_addin.xla")
End If
'- Xoa addin trong regkey Option
Dim i As Long
For i = 1 To 10
If RegKeyExists("HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Options\OPEN5" & i) = True And RegKeyRead("HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Options\OPEN5" & i) = "C:\test_addin.xla" Then
    RegKeyDelete ("HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Options\OPEN5" & i)
End If
Next
'- Mo lai Excel
ExcelApp.Workbooks.Add
ExcelApp.Visible = False         'khong hien file Excel
If ExcelApp.AddIns("test_addin.xla") Is Nothing Then MsgBox "Da go go Addin thanh cong"
ExcelApp.Quit
End Sub
Các hàm hổ trợ:
Mã:
'==========================
'==Reading from the Registry:
'===========================
'reads the value for the registry key i_RegKey
'if the key cannot be found, the return value is ""
Function RegKeyRead(i_RegKey As String) As String
Dim myWS As Object


  On Error Resume Next
  'access Windows scripting
  Set myWS = CreateObject("WScript.Shell")
  'read key from registry
  RegKeyRead = myWS.RegRead(i_RegKey)
End Function
'==========================
'==Checking if a Registry key exists:
'===========================


'returns True if the registry key i_RegKey was found
'and False if not
Function RegKeyExists(i_RegKey As String) As Boolean
Dim myWS As Object


  On Error GoTo ErrorHandler
  'access Windows scripting
  Set myWS = CreateObject("WScript.Shell")
  'try to read the registry key
  myWS.RegRead i_RegKey
  'key was found
  RegKeyExists = True
  Exit Function
  
ErrorHandler:
  'key was not found
  RegKeyExists = False
End Function
'==========================
'==Saving a Registry key:
'===========================


'sets the registry key i_RegKey to the
'value i_Value with type i_Type
'if i_Type is omitted, the value will be saved as string
'if i_RegKey wasn't found, a new registry key will be created
Sub RegKeySave(i_RegKey As String, _
               i_Value As String, _
      Optional i_Type As String = "REG_SZ")
Dim myWS As Object


  'access Windows scripting
  Set myWS = CreateObject("WScript.Shell")
  'write registry key
  myWS.RegWrite i_RegKey, i_Value, i_Type


End Sub
'==========================
'==Deleting a key from the Registry:
'===========================


'deletes i_RegKey from the registry
'returns True if the deletion was successful,
'and False if not (the key couldn't be found)
Function RegKeyDelete(i_RegKey As String) As Boolean
Dim myWS As Object


  On Error GoTo ErrorHandler
  'access Windows scripting
  Set myWS = CreateObject("WScript.Shell")
  'delete registry key
  myWS.RegDelete i_RegKey
  'deletion was successful
  RegKeyDelete = True
  Exit Function


ErrorHandler:
  'deletion wasn't successful
  RegKeyDelete = False
End Function
Cái addin muốn gở bỏ là: C:\test_addin.xla
Em có gửi file đính kèm.

Mong nhận được góp ý của mọi người
Trân trọng
 

File đính kèm

Lần chỉnh sửa cuối:
Sao bạn sài Excell 11 nhưng trong code lại là 12

Mã:
RegKeyRead("HKEY_CURRENT_USER\Software\Microsoft\Office\11.0\Excel\Options\OPEN" & i) = """C:\Documents and Settings\Thuyyeu\My Documents\Downloads\Compressed\xoa add in test_addin\test_addin.xla"""
 
Sao bạn sài Excell 11 nhưng trong code lại là 12

Mã:
RegKeyRead("HKEY_CURRENT_USER\Software\Microsoft\Office\11.0\Excel\Options\OPEN" & i) = """C:\Documents and Settings\Thuyyeu\My Documents\Downloads\Compressed\xoa add in test_addin\test_addin.xla"""
Dear Thuyyeu99
Trong Registry của mình không có 11.0 mà chỉ có 10 , 12, và 14. Cái 14 (office 2010 - chỉ có PowerPoint), còn cái 12 (Office 2007: Word, Excel, Access).
Mình sửa lại code main :
Mã:
Sub Main()
On Error Resume Next
Dim ExcelApp As Excel.Application
ExcelApp.AddIns("CSG").Installed = False
ExcelApp.Quit
'- Xoa addin trong regkey Addin Manager
If RegKeyExists("HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Add-in Manager\C:\test_addin.xla") = True Then
    RegKeyDelete ("HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Add-in Manager\C:\test_addin.xla")
End If
'- Xoa addin trong regkey Option
Dim i As Long
For i = 1 To 10
If RegKeyExists("HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Options\OPEN" & i) = True And RegKeyRead("HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Options\OPEN" & i) = "C:\test_addin.xla" Then
    RegKeyDelete ("HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Options\OPEN" & i)
End If
Next
'- Mo lai Excel
ExcelApp.Workbooks.Add
ExcelApp.Visible = False         'khong hien file Excel
If ExcelApp.AddIns("test_addin.xla") Is Nothing Then MsgBox "Da go go Addin thanh cong"
ExcelApp.Quit
End Sub

Sửa lại chổ
PHP:
Excel\Options\OPEN5" & i) thành Excel\Options\OPEN" & i)
Thân.
 
Lần chỉnh sửa cuối:
Xin nhờ anh chị giải đáp
Trong regkey: "HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Add-in Manager" có 3 Name: (Default), C:\Abc.xla, C:\Bcd.xla.
Để kiểm tra sự tồn tại của của các Name: C:\abc.xla ... tôi có thể dùng code để kiểm tra hay không, nếu được thì tôi khai báo như thế nào!
Mã:
Function RegKeyExists(i_RegKey As String) As Boolean
Dim myWS As Object


  On Error GoTo ErrorHandler
  'access Windows scripting
  Set myWS = CreateObject("WScript.Shell")
  'try to read the registry key
  myWS.RegRead i_RegKey
  'key was found
  RegKeyExists = True
  Exit Function
  
ErrorHandler:
  'key was not found
  RegKeyExists = False
End Function
Mong nhận được hồi đáp của anh chị.
Thân.
 
Web KT

Bài viết mới nhất

Back
Top Bottom