Hezký den všem, pokouším se vytvořit vlastní smart tag knihovnu s třídami Action a Recognizer ve vb.net expres (2005, Net.Framework 2.0) ve snaze využít vlatní smart tags či inteligentní značky v Excelu 2003 (edice Professional). Postupuji dle magazinu viz. link: Smart Tags in Office 2003 http://www.code-magazine.com/article.asp... Závěrem linku výše je také odkaz na starší článek: Building Smart Tags in Microsoft Visual Basic .NET http://msdn.microsoft.com/en-us/library/... Založen Class Library project s referencí Microsoft Smart Tags 2.0 s třídami Action a Recognizer a modulem dle druhého linku pro zápis klíčů při registraci, pak po použití regasm není nutné klíče zapisovat manuálně do registru : HKEY_CURRENT_USER\Software\Microsoft\Office\Common\Smart Tag Zmíněné třídy Action a Recognizer:
Imports Microsoft.Office.Interop.SmartTag
Imports System.Runtime.InteropServices
<ProgId("MySmartTag.Action"), _
Guid("C1751811-6739-4614-AB28-9FB9E988A630"), _
ComVisible(True)> _
Public Class Action
Implements ISmartTagAction
Implements ISmartTagAction2
' this is requred for com interop
Public Sub New()
End Sub
Public ReadOnly Property Desc(ByVal LocaleID As Integer) _
As String Implements ISmartTagAction.Desc
Get
Return "My SmartTag Action Description"
End Get
End Property
Public Sub InvokeVerb(ByVal VerbID As Integer, _
ByVal ApplicationName As String, _
ByVal Target As Object, _
ByVal Properties As ISmartTagProperties, _
ByVal strText As String, _
ByVal Xml As String) _
Implements ISmartTagAction.InvokeVerb
' we are not using this method, see InvokeVerb2
End Sub
Public ReadOnly Property Name(ByVal LocaleID As Integer) _
As String Implements ISmartTagAction.Name
Get
Return "My SmartTag Action"
End Get
End Property
Public ReadOnly Property ProgId() As String _
Implements ISmartTagAction.ProgId
Get
Return "MySmartTag.Action"
End Get
End Property
Public ReadOnly Property SmartTagCaption _
(ByVal SmartTagID As Integer, _
ByVal LocaleID As Integer) As String _
Implements ISmartTagAction.SmartTagCaption
Get
Return "My Smart Tag Caption"
End Get
End Property
' we are only supporting 1 smart tag
Public ReadOnly Property SmartTagCount() As Integer _
Implements ISmartTagAction.SmartTagCount
Get
Return 1
End Get
End Property
' use the same smart tag name as the recognizer
Public ReadOnly Property SmartTagName _
(ByVal SmartTagID As Integer) As String _
Implements ISmartTagAction.SmartTagName
Get
Return "urn:samples-msdn-microsoft-com#MySmartTag"
End Get
End Property
Public ReadOnly Property VerbCaptionFromID _
(ByVal VerbID As Integer, _
ByVal ApplicationName As String, _
ByVal LocaleID As Integer) As String _
Implements ISmartTagAction.VerbCaptionFromID
Get
' we are not using this method, see VerbCaptionFromID2
Return vbNullString
End Get
End Property
' we are using 6 verbs in a cascading menu
' see VerbCaptionFromID2
Public ReadOnly Property VerbCount _
(ByVal SmartTagName As String) As Integer _
Implements ISmartTagAction.VerbCount
Get
Return 6
End Get
End Property
' to get the verb id, just return the VerbIndex
Public ReadOnly Property VerbID(ByVal SmartTagName As String, _
ByVal VerbIndex As Integer) As Integer _
Implements ISmartTagAction.VerbID
Get
Return VerbIndex
End Get
End Property
Public ReadOnly Property VerbNameFromID _
(ByVal VerbID As Integer) As String _
Implements ISmartTagAction.VerbNameFromID
Get
Return "ShowTag"
End Get
End Property
Public ReadOnly Property IsCaptionDynamic _
(ByVal VerbID As Integer, _
ByVal ApplicationName As String, _
ByVal LocaleID As Integer) As Boolean _
Implements ISmartTagAction2.IsCaptionDynamic
Get
Return False
End Get
End Property
' This is necessary to underline the text the smart tag
' recognized.
Public ReadOnly Property ShowSmartTagIndicator _
(ByVal VerbID As Integer, _
ByVal ApplicationName As String, _
ByVal LocaleID As Integer) As Boolean _
Implements ISmartTagAction2.ShowSmartTagIndicator
Get
Return True
End Get
End Property
' we are not implementing the initialize method
Public Sub SmartTagInitialize(ByVal ApplicationName As String) _
Implements ISmartTagAction2.SmartTagInitialize
End Sub
' build the cascading menu for the on-application menu
Public ReadOnly Property VerbCaptionFromID2 _
(ByVal VerbID As Integer, _
ByVal ApplicationName As String, _
ByVal LocaleID As Integer, _
ByVal Properties As ISmartTagProperties, _
ByVal Text As String, _
ByVal Xml As String, _
ByVal Target As Object) As String _
Implements ISmartTagAction2.VerbCaptionFromID2
Get
Dim lcString As String = ""
Select Case VerbID
Case 1
lcString = "Test 1///Show My Smart Tag Verb ID 1"
Case 2
lcString = "Test 1///Show My Smart Tag Verb ID 2"
Case 3
lcString = "Test 2///Show My Smart Tag Verb ID 3"
Case 4
lcString = "Test 2///Show My Smart Tag Verb ID 4"
Case 5
lcString = "Test 3///Show My Smart Tag Verb ID 5"
Case 6
lcString = "Test 3///Show My Smart Tag Verb ID 6"
End Select
Return lcString
End Get
End Property
' our action just displays a messagebox of information
' collection during recognition
Public Sub InvokeVerb2(ByVal VerbID As Integer, _
ByVal ApplicationName As String, _
ByVal Target As Object, _
ByVal Properties As ISmartTagProperties, _
ByVal strText As String, _
ByVal Xml As String, _
ByVal LocaleID As Integer) _
Implements ISmartTagAction2.InvokeVerb2
Dim strMsg As String = strText
Dim i As Integer
For i = 0 To Properties.Count - 1
strMsg &= vbCrLf & Properties.KeyFromIndex(i) & "=" & _
Properties.ValueFromIndex(i)
Next
' add the Verb ID and Application Name
strMsg &= vbCrLf & "Verb ID=" & VerbID
strMsg &= vbCrLf & "Application=" & ApplicationName
MsgBox(strMsg, MsgBoxStyle.Information, "My SmartTag Action")
End Sub
End Class
<ProgId("MySmartTag.Recognizer"), _
Guid("57D8FC4A-4ED8-43c9-9716-7D378D03F2FC"), _
ComVisible(True)> _
Public Class Recognizer
Implements ISmartTagRecognizer
Implements ISmartTagRecognizer2
' this is requred for com interop
Public Sub New()
End Sub
Public ReadOnly Property Desc(ByVal LocaleID As Integer) As _
String Implements ISmartTagRecognizer.Desc
Get
Return "My first smart tag description."
End Get
End Property
Public ReadOnly Property Name(ByVal LocaleID As Integer) As _
String Implements ISmartTagRecognizer.Name
Get
Return "My first smart tag name."
End Get
End Property
Public ReadOnly Property ProgId() As String _
Implements ISmartTagRecognizer.ProgId
Get
Return "MySmartTag.Recognizer"
End Get
End Property
Public Sub Recognize(ByVal strRecText As String, _
ByVal DataType As IF_TYPE, _
ByVal LocaleID As Integer, _
ByVal RecognizerSite As ISmartTagRecognizerSite) _
Implements ISmartTagRecognizer.Recognize
' we are not using this method, see Recognize2
End Sub
' we are only supporting 1 smart tag
Public ReadOnly Property SmartTagCount() As Integer _
Implements ISmartTagRecognizer.SmartTagCount
Get
Return 1
End Get
End Property
' we are not supporting a download url
Public ReadOnly Property SmartTagDownloadURL _
(ByVal SmartTagID As Integer) As String _
Implements ISmartTagRecognizer.SmartTagDownloadURL
Get
Return ""
End Get
End Property
Public ReadOnly Property SmartTagName _
(ByVal SmartTagID As Integer) As String _
Implements ISmartTagRecognizer.SmartTagName
Get
Return "urn:samples-msdn-microsoft-com#MySmartTag"
End Get
End Property
' we are not supporting a property page
Public Sub DisplayPropertyPage(ByVal SmartTagID As Integer, _
ByVal LocaleID As Integer) _
Implements ISmartTagRecognizer2.DisplayPropertyPage
End Sub
' we are not supporting a property page
Public ReadOnly Property PropertyPage _
(ByVal SmartTagID As Integer, _
ByVal LocaleID As Integer) As Boolean _
Implements ISmartTagRecognizer2.PropertyPage
Get
End Get
End Property
Public Sub Recognize2(ByVal strRecText As String, _
ByVal DataType As IF_TYPE, _
ByVal LocaleID As Integer, _
ByVal RecognizerSite2 As ISmartTagRecognizerSite2, _
ByVal ApplicationName As String, _
ByVal TokenList As ISmartTagTokenList) _
Implements ISmartTagRecognizer2.Recognize2
Dim intLocation As Integer = _
InStr(strRecText, "SmartTag", CompareMethod.Text)
If intLocation > 0 Then
Dim stPropBag As ISmartTagProperties = _
RecognizerSite2.GetNewPropertyBag()
Dim strPropType As String = ""
Select Case DataType
Case IF_TYPE.IF_TYPE_CELL
strPropType = "IF_TYPE_CELL"
Case IF_TYPE.IF_TYPE_CHAR
strPropType = "IF_TYPE_CHAR"
Case IF_TYPE.IF_TYPE_PARA
strPropType = "IF_TYPE_PARA"
Case IF_TYPE.IF_TYPE_REGEXP
strPropType = "IF_TYPE_REGEXP"
Case IF_TYPE.IF_TYPE_SINGLE_WD
strPropType = "IF_TYPE_SINGLE_WD"
End Select
stPropBag.Write("DataType", strPropType)
stPropBag.Write("LocalID", LocaleID.ToString())
RecognizerSite2.CommitSmartTag _
("urn:samples-msdn-microsoft-com#MySmartTag", _
intLocation, 8, stPropBag)
End If
End Sub
' we are not implementing the initialize method
Public Sub SmartTagInitialize(ByVal ApplicationName As String) _
Implements ISmartTagRecognizer2.SmartTagInitialize
End Sub
<ComRegisterFunction()> _
Shared Sub Register(ByVal RegKey As String)
RegSmartTag(RegKey, True)
End Sub
<ComUnregisterFunction()> _
Shared Sub Unregister(ByVal RegKey As String)
RegSmartTag(RegKey, False)
End Sub
End Class
Modul FileRegistration:
Imports Microsoft.Win32
Imports System.Runtime.InteropServices
Imports System.Reflection
Imports System
Module FileRegistration
Private fReg As Boolean = False '<-- Shared variable so this gets called only once.
Public Sub RegSmartTag(ByVal RegKey As String, ByVal Register As Boolean)
If Not fReg Then
OutputMsg("***** Beginning smart tag registration. *****")
OutputMsg(vbTab & "Register=" & Register.ToString())
OutputMsg("")
' Main smart tags registry key.
Dim rkeySmartTags As RegistryKey _
= Registry.CurrentUser.OpenSubKey _
("Software\Microsoft\Office\Common\Smart Tag", True)
' Actions subkey.
Dim rkeyActions As RegistryKey _
= rkeySmartTags.OpenSubKey("Actions", True)
' Recognizers subkey.
Dim rkeyRecognizers As RegistryKey _
= rkeySmartTags.OpenSubKey("Recognizers", True)
' Get the current assembly.
Dim reflAssembly As [Assembly] = [Assembly].GetExecutingAssembly()
' Get all public types for the assembly.
Dim reflTypes As Type() = reflAssembly.GetExportedTypes()
Dim reflType As Type
For Each reflType In reflTypes
' <-- Looping over the exported types.
' Get the interfaces to look for the smart tag interfaces.
If reflType.IsClass Then ' Make sure that it's a class.
Dim reflInterfaces As Type() = reflType.GetInterfaces()
Dim reflInterface As Type
For Each reflInterface In reflInterfaces
Select Case reflInterface.Name
Case "ISmartTagAction" ' Smart tag action interface.
HandleReg(reflType, rkeyActions, Register)
Case "ISmartTagRecognizer" 'Smart tag recognizer interface.
HandleReg(reflType, rkeyRecognizers, Register)
End Select
Next reflInterface
End If
Next reflType
'Done. Now clean up.
reflTypes = Nothing
reflAssembly = Nothing
rkeyActions.Close()
rkeyActions = Nothing
rkeyRecognizers.Close()
rkeyRecognizers = Nothing
rkeySmartTags.Close()
rkeySmartTags = Nothing
fReg = True ' <-- Set our shared variable to True.
OutputMsg("")
OutputMsg("***** Completed smart tag registration. *****")
OutputMsg("")
End If
End Sub
Sub HandleReg(ByVal sysType As Type, ByVal RegKey As RegistryKey, _
ByVal Register As Boolean)
' Code to actually do the registration of the smart tag.
' INPUTS:
' sysType: Type for the class that's getting registered.
' RegKey: Registry key to register the class in.
' Should be the actions or recognizers key.
' Register: True to register, False to unregister.
' Get the type of the GuidAttribute class.
Dim typGUIDAttr As Type = GetType(GuidAttribute)
' Check to see if the GuidAttribute attribute is defined on the class.
If Attribute.IsDefined(sysType, typGUIDAttr) Then
Dim attrValue As GuidAttribute
' Get the GuidAttribute attribute through reflection.
attrValue = CType(Attribute.GetCustomAttribute(sysType, typGUIDAttr), GuidAttribute)
' Get the string representation of the GUID.
Dim strGuidVal As String = "{" & attrValue.Value & "}"
If Register Then
Try
Dim newKey As RegistryKey _
= RegKey.CreateSubKey(strGuidVal)
newKey.SetValue("Assembly", sysType.Assembly.FullName)
newKey.SetValue("Type", sysType.FullName)
OutputMsg(sysType.Name & " registered with smart tags successfully")
Catch
OutputMsg("Failed to register " & sysType.Name & " with smart tags")
OutputMsg(Err.GetType.Name & ":" & Err.Description)
End Try
Else
Try
RegKey.DeleteSubKey(strGuidVal, False)
OutputMsg(sysType.Name & " unregistered with smart tags successfully")
Catch
OutputMsg("Failed to unregister " & sysType.Name & " with smart tags")
OutputMsg(Err.GetType.Name & ":" & Err.Description)
End Try
End If
Else
'If we don't find the GuidAttribute attribute, write to the system console.
OutputMsg("Could not register " & sysType.Name & " as smart tag.")
OutputMsg("GUID attribute not found on class.")
End If
End Sub
Private Sub OutputMsg(ByVal Msg As String)
' Use DEBUG conditional compile constant to only output
' when compiling the DEBUG version.
#If DEBUG Then
System.Console.WriteLine(Msg)
#End If
End Sub
End Module
Asembly mám podepsanou, zkompilovanou a dostávám se k registraci, zatím v tom nemám vůbec jasno, uvádí, že stačí přidat 'jen' klíče do registru,ale skutečnost bude mnohem složitější a strašně v tom tápu. Mám prosbu na správný postup registrace knihovny, má správně být vlastně umístěna do GAC pomocí gacutil /i nebo v adresáři : C:\Program Files\Common Files\Microsoft Shared\Smart Tag (kde jsou smart tag knihovny i složka Lists a nástroj SmartTagInstall.exe pro obnovu smarttags pro office bez jejich restartu) nebo libovolně jinde s použitím podklíče Filename, který pak k ní určuje cestu, něco podobného asi používá VSTO plus podklíč Managed, ale to nemám a mít nebudu, jen express... Nechci popisovat své nezdařilé pokusy, už tak je to dlouhé. Něco ohledně COM a kolem Office jsem tu zahlídla, snad zde uspěji a povede se mi příklad s Vaší pomocí zprovoznit. Ohledně nastavení excelu má být zabezpečení nastaveno na střední, zaškrtnuto důvěřovat doplňkům a šablonám, také povoleny značky v sešitu, nástroje-> možnosti automatických oprav, pak se také vlastní značky v lepším (ne mém) případě objeví mezi stávajícími a budou se zobrazovat při rozeznání textu... Snažím se 'slepit' info z různých zdrojů, bez pomoci to v žádném případě nedám dohromady, s .net se seznamuji a pro zkušené to možná nebude tak nepochopitelné, snad budu mít štěstí, třeba mi chybí v kódu nějaké atributy, v registrech podklíče a určitě vše dělám naprosto špatně... Díky za trpělivost, váš čas a případnou pomoc s registrací, tip či jen odkaz, jak se dozvědět něco bližšího. ... Další odkazy: Visual Basic .NET Tutorial http://msdn.microsoft.com/en-us/library/... Smart Tags and the Windows Registry http://msdn.microsoft.com/en-us/library/... How to create an Office XP Smart Tag DLL by using Visual Basic .NET http://support.microsoft.com/default.asp...;en-us;q306058 I tak to asi nepůjde: http://support.microsoft.com/kb/908002
|