Nasıl Yapılır: Word and .NET Framework 4'te Özel Tanıyıcılarla Akıllı Etiketler Oluşturma
.NET Framework 4 hedefleyen Word projelerinde, Word'ün ISmartTagExtension arayüzünü uygulayarak belgelerdeki akıllı etiketleri nasıl tanıdığını denetleyebilirsiniz.
Akıllı etiketi çalıştırmak için, son kullanıcılar Word ve Excel'de akıllı etiketleri etkin hale getirmeliler. Daha fazla bilgi için bkz. Nasıl Yapılır: Word ve Excel'de Akıllı Etiketleri Etkinleştirme.
Uygulama alanı: Bu konudaki bilgiler Word 2007'nin belge düzeyi projelerine ve uygulama düzeyi projelerine yöneliktir. Daha fazla bilgi için bkz. Office Uygulamalarında Kullanılabilir Özellikler ve Proje Türü.
Word belgesine özel tanıyıcı ile akıllı etiket eklemek için
Word 2007 için belge düzeyi veya uygulama düzeyi projesi oluşturun. Daha fazla bilgi için bkz. Nasıl Yapılır: Visual Studio'da Office Projeleri Oluşturma.
Başvuru Ekle iletişim kutusunun .NET sekmesinden Microsoft.Office.Interop.SmartTag derlemesine (12.0.0.0 sürümü) bir başvuru ekleyin.
Projeye sınıf dosyası ekleyin ve ISmartTagExtension arayüzünü uygulayan sınıf oluşturun.
Yeni sınıfta, akıllı etiketi gösteren SmartTag nesnesi oluşturun ve akıllı eitket eylemlerini gösteren bir veya daha fazla Action nesnesi oluşturun. Bu nesneleri oluşturmak için Globals.Factory.CreateSmartTag ve Globals.Factory.CreateAction yöntemlerini kullanın.
Recognize yöntemini uygulayın ve kendi özel tanıma davranışınızı yazın.
Uygulamanız, Word'un akıllı etiketi tanıması için context parametresinin PersistTag yöntemini çağırmalı.
SmartTag nesnesi dönmek için ExtensionBase özelliğini uygulayın.
Oluşturduğunuz eylemlerin Click olayını ve isteğe bağlı olarak BeforeCaptionShow olayını yanıtlaması için olay işleyicileri oluşturun.
Proje belgesi için olan kod dosyasında, (belge düzeyi projeleri için) ThisDocument sınıfının VstoSmartTags özelliğine veya (uygulama düzeyi projeleri için) ThisAddIn sınıfının VstoSmartTags özelliğine akıllı etiket örneği ekleyin.
Örnek
Aşağıdaki kod örneğinde bir Word belgesinde özel akıllı etiketlerin nasıl oluşturulduğu gösterilmektedir. Örnek, sales ve organization terimlerini tanımak için Recognize yöntemini uygular. Recognize yöntemi, akıllı etiketler için anahtarlı özellikler koleksiyonuna anahtar ve değer çifti ekler. Yöntem, daha sonra akıllı etiketi tanımak ve yeni akıllı etiket özelliğini kaydetmek için PersistTag yöntemini çağırır.
Örneği test etmek için, sales (satışlar) ve organization (organizasyon) kelimelerini belgede farklı yerlere yazın ve sonra akıllı etiket eylemlerini deneyin. Bir eylem, tanınmış terim için ilgili özellik değerini görüntülerken diğer eylem akıllı etiket isim-uzayını ve başlığını görüntüler.
Imports System
Imports System.Windows.Forms
Imports Microsoft.Office.Interop.SmartTag
Imports Microsoft.Office.Tools.Word
Public Class CustomSmartTag
Implements ISmartTagExtension
' Declare the smart tag.
Private smartTagDemo As Microsoft.Office.Tools.Word.SmartTag
' Declare actions for this smart tag.
WithEvents Action1 As Microsoft.Office.Tools.Word.Action
WithEvents Action2 As Microsoft.Office.Tools.Word.Action
Public Sub New()
Me.smartTagDemo = Globals.Factory.CreateSmartTag(
"https://www.contoso.com/Demo#DemoSmartTag", "Custom Smart Tag", Me)
Action1 = Globals.Factory.CreateAction("Display property value")
Action2 = Globals.Factory.CreateAction("Display smart tag details")
smartTagDemo.Terms.AddRange(New String() {"sales", "organization"})
smartTagDemo.Actions = New Microsoft.Office.Tools.Word.Action() {Action1, Action2}
End Sub
Private Sub Recognize(ByVal text As String,
ByVal site As ISmartTagRecognizerSite,
ByVal tokenList As ISmartTagTokenList,
ByVal context As SmartTagRecognizeContext) Implements ISmartTagExtension.Recognize
For Each term As String In smartTagDemo.Terms
' Search the text for the current smart tag term.
Dim index As Integer = text.IndexOf(term, 0)
While (index >= 0)
' Create a smart tag token and a property bag for the recognized term.
Dim propertyBag As ISmartTagProperties = site.GetNewPropertyBag()
' Write a new property value.
Dim key As String = "Key1"
propertyBag.Write(key, DateTime.Now.ToString())
' Attach the smart tag to the term in the document
context.PersistTag(index, term.Length, propertyBag)
' Increment the index and then find the next instance of the smart tag term.
index += term.Length
index = text.IndexOf(term, index)
End While
Next
End Sub
' This action displays the property value for the term.
Private Sub Action1_Click(ByVal sender As Object,
ByVal e As Microsoft.Office.Tools.Word.ActionEventArgs) Handles Action1.Click
Dim propertyBag As ISmartTagProperties = e.Properties
Dim key As String = "Key1"
MessageBox.Show(("The corresponding value of " & key & " is: ") + propertyBag.Read(key))
End Sub
' This action displays smart tag details.
Private Sub Action2_Click(ByVal sender As Object,
ByVal e As Microsoft.Office.Tools.Word.ActionEventArgs) Handles Action2.Click
MessageBox.Show(("The current smart tag caption is '" &
smartTagDemo.Caption & "'. The current smart tag type is '") &
smartTagDemo.SmartTagType & "'.")
End Sub
Public ReadOnly Property Base() As Microsoft.Office.Tools.Word.SmartTag
Get
Return (smartTagDemo)
End Get
End Property
Public ReadOnly Property ExtensionBase() As Object Implements ISmartTagExtension.ExtensionBase
Get
Return (smartTagDemo)
End Get
End Property
End Class
using System;
using System.Windows.Forms;
using Microsoft.Office.Interop.SmartTag;
using Microsoft.Office.Tools.Word;
namespace CustomSmartTagExample
{
public class CustomSmartTag : ISmartTagExtension
{
// Declare the smart tag.
Microsoft.Office.Tools.Word.SmartTag smartTagDemo;
// Declare actions for this smart tag.
private Microsoft.Office.Tools.Word.Action Action1;
private Microsoft.Office.Tools.Word.Action Action2;
public CustomSmartTag()
{
this.smartTagDemo = Globals.Factory.CreateSmartTag(
"https://www.contoso.com/Demo#DemoSmartTag", "Custom Smart Tag", this);
Action1 = Globals.Factory.CreateAction("Display property value");
Action2 = Globals.Factory.CreateAction("Display smart tag details");
smartTagDemo.Terms.AddRange(new string[] { "sales", "organization" });
smartTagDemo.Actions = new Microsoft.Office.Tools.Word.Action[] { Action1, Action2 };
Action1.Click += new ActionClickEventHandler(Action1_Click);
Action2.Click += new ActionClickEventHandler(Action2_Click);
}
void ISmartTagExtension.Recognize(string text, ISmartTagRecognizerSite site, ISmartTagTokenList tokenList,
SmartTagRecognizeContext context)
{
foreach (string term in smartTagDemo.Terms)
{
// Search the text for the current smart tag term.
int index = text.IndexOf(term, 0);
while (index >= 0)
{
// Create a smart tag token and a property bag for the recognized term.
ISmartTagProperties propertyBag = site.GetNewPropertyBag();
// Write a new property value.
string key = "Key1";
propertyBag.Write(key, DateTime.Now.ToString());
// Attach the smart tag to the term in the document
context.PersistTag(index, term.Length, propertyBag);
// Increment the index and then find the next instance of the smart tag term.
index += term.Length;
index = text.IndexOf(term, index);
}
}
}
// This action displays the property value for the term.
private void Action1_Click(object sender,
Microsoft.Office.Tools.Word.ActionEventArgs e)
{
ISmartTagProperties propertyBag = e.Properties;
string key = "Key1";
MessageBox.Show("The corresponding value of " + key + " is: " + propertyBag.get_Read(key));
}
// This action displays smart tag details.
private void Action2_Click(object sender,
Microsoft.Office.Tools.Word.ActionEventArgs e)
{
MessageBox.Show("The current smart tag caption is '" +
smartTagDemo.Caption + "'. The current smart tag type is '" + smartTagDemo.SmartTagType + "'.");
}
public Microsoft.Office.Tools.Word.SmartTag Base
{
get { return smartTagDemo; }
}
public object ExtensionBase
{
get { return smartTagDemo; }
}
}
}
Kodu Derleme
Başvuru Ekle iletişim kutusundaki COM sekmesinden Microsoft Smart Tags 2.0 Tür Kitaplığı'na projedeki başvuruyu ekleyin. Başvurunun Yereli Kopyala özelliğinin false olduğuna emin olun. Eğer true ise, başvuru doğru birincil birlikte çalışma derlemesine değildir ve Microsoft Office yükleme medyasından derlemeyi yüklemelisiniz. Daha fazla bilgi için bkz. Nasıl Yapılır: Office Birincil Birlikte Çalışma Derlemelerini Yükleme.
Örnek kodu CustomSmartTag isimli yeni bir dosyaya koyun.
C#'ta proje adıyla eşleşmesi için isim-uzayını değiştirin.
Sınıf dosyasının en üstündeki Microsoft.Office.Tools.Word ve Microsoft.Office.Interop.SmartTag isim-uzayı için Imports (Visual Basic'te) veya using (C#'ta) ifadelerini ekleyin.
Projenizdeki ThisDocument_Startup veya ThisAddIn_Startup olay işleyicisine aşağıdaki kodu ekleyin. Bu kod belgeye özel akıllı etiket ekler.
Me.VstoSmartTags.Add(New CustomSmartTag().Base)
this.VstoSmartTags.Add(new CustomSmartTag().Base);
Güvenlik
Akıllı etiketi çalıştırmak için, akıllı etiketler Word'de etkin halde olmalıdır. Daha fazla bilgi için bkz. Nasıl Yapılır: Word ve Excel'de Akıllı Etiketleri Etkinleştirme.
Ayrıca bkz.
Görevler
Nasıl Yapılır: Word ve Excel'de Akıllı Etiketleri Etkinleştirme
Nasıl Yapılır: Word Belgelerine Akıllı Etiketler Ekleme
Nasıl Yapılır: Excel Çalışma Kitaplarına Akıllı Etiketler Ekleme
İzlenecek Yol: Belge Düzeyi Özelleştirmesi Kullanarak Akıllı Etiket Oluşturma
İzlenecek Yol: Uygulama Düzeyi Eklentisini Kullanarak Akıllı Etiket Oluşturma
Kavramlar
Nasıl Yapılır: Excel and .NET Framework 3.5'te Özel Tanıyıcılarla Akıllı Etiketler Oluşturma