Aracılığıyla paylaş


Nasıl Yapılır: Gizli Modda Word İletişim Kutularını Kullanma

Microsoft Office World'de yerleşik iletişim kutularını çağırarak bir yöntem çağrısıyla karmaşık işlemleri kullanıcıya görüntülemeden gerçekleştirebilirsiniz. Bunu, DialognesnesininExecute yöntemini kullanarak, Display yöntemini çağırmadan yapabilirsiniz.

Uygulama alanı: Bu konudaki bilgiler Word 2007 ve Word 2010 uygulamalarının 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ü.

Örnekler

Aşağıdaki kod örneğinde, kullanıcı girişi olmadan çoklu sayfa kurulum özelliklerini ayarlamak için gizli modda Sayfa Kurulumu iletişim kutusunun nasıl kullanılacağı gösterilir. Örnekler, özel sayfa boyutunu yapılandırmak için Dialog nesnesini kullanır. Üst kenar boşluğu, alt kenar boşluğu vb. gibi sayfa kurulumu için özel ayarlar, Dialog nesnesinin geç bağımlı özellikleri olarak kullanılabilir. Bu özellikler, Word tarafından çalışma zamanında dinamik olarak oluşturulur.

Aşağıdaki kod örneğinde, Option Strict'in kapalı olduğu Visual Basic projelerinde ve .NET Framework 4 hedefleyen Visual C# projelerinde bu görevin nasıl gerçekleştirildiği anlatılır. Bu projelerde, Visual Basic ve Visual C# derleyicilerinde geç bağımlı özellikleri kullanabilirsiniz. Bu örneği kullanmak için projenizdeki ThisDocument veya ThisAddIn sınıfından kodu çalıştırın.

Dim dialog As Word.Dialog = Application.Dialogs.Item(Word.WdWordDialog.wdDialogFilePageSetup)

' Set the properties of the dialog box.
' ControlChars.Quote() is used to represent the symbol for inches.
With dialog
    .PageWidth = 3.3 & ControlChars.Quote
    .PageHeight = 6 & ControlChars.Quote
    .TopMargin = 0.71 & ControlChars.Quote
    .BottomMargin = 0.81 & ControlChars.Quote
    .LeftMargin = 0.66 & ControlChars.Quote
    .RightMargin = 0.66 & ControlChars.Quote
    .HeaderDistance = 0.28 & ControlChars.Quote
    .Orientation = Word.WdOrientation.wdOrientPortrait
    .DifferentFirstPage = False
    .FirstPage = 0
    .OtherPages = 0

    ' Apply these settings only to the current selection with this line of code:
    .ApplyPropsTo = 3

    ' Apply the settings.
    .Execute()
End With
dynamic dialog = Application.Dialogs[Word.WdWordDialog.wdDialogFilePageSetup];
dialog.PageWidth = "3.3\"";
dialog.PageHeight = "6\"";
dialog.TopMargin = "0.71\"";
dialog.BottomMargin = "0.81\"";
dialog.LeftMargin = "0.66\"";
dialog.RightMargin = "0.66\"";
dialog.HeaderDistance = "0.28\"";
dialog.Orientation = "0";
dialog.DifferentFirstPage = "0";
dialog.FirstPage = "0";
dialog.OtherPages = "0";

// Apply these settings only to the current selection with this line of code:
dialog.ApplyPropsTo = "3";

// Apply the settings.
dialog.Execute(); 

Aşağıdaki kod örneğinde, Option Strict'in açık olduğu Visual Basic projelerinde ve .NET Framework 3.5'i hedefleyen Visual C# projelerinde bu görevin nasıl gerçekleştirildiği anlatılır. Bu projelerde, geç bağımlı özelliklere erişmek için yansımayı kullanmalısınız. Bu örneği kullanmak için projenizdeki ThisDocument veya ThisAddIn sınıfından kodu çalıştırın.

Friend Sub PageSetupDialogHidden()
    Dim dialog As Word.Dialog = Application.Dialogs.Item(Word.WdWordDialog.wdDialogFilePageSetup)

    ' Set the properties of the dialog box.
    ' ControlChars.Quote() is used to represent the symbol for inches.
    InvokeHelper(dialog, "PageWidth", "3.3" & ControlChars.Quote)
    InvokeHelper(dialog, "PageHeight", "6" & ControlChars.Quote)
    InvokeHelper(dialog, "TopMargin", "0.71" & ControlChars.Quote)
    InvokeHelper(dialog, "BottomMargin", "0.81" & ControlChars.Quote)
    InvokeHelper(dialog, "LeftMargin", "0.66" & ControlChars.Quote)
    InvokeHelper(dialog, "RightMargin", "0.66" & ControlChars.Quote)
    InvokeHelper(dialog, "HeaderDistance", "0.28" & ControlChars.Quote)
    InvokeHelper(dialog, "Orientation", "0")
    InvokeHelper(dialog, "DifferentFirstPage", "0")
    InvokeHelper(dialog, "FirstPage", "0")
    InvokeHelper(dialog, "OtherPages", "0")

    ' Apply these settings only to the current selection with this line of code:
    InvokeHelper(dialog, "ApplyPropsTo", "3")

    ' Apply the settings.
    dialog.Execute()
End Sub

Private Shared Sub InvokeHelper(ByVal dialog As Word.Dialog, ByVal member As String, ByVal value As String)
    Dim dialogType As System.Type = GetType(Word.Dialog)

    ' Set the appropriate property of the dialog box.
    dialogType.InvokeMember(member,
        System.Reflection.BindingFlags.SetProperty Or
            System.Reflection.BindingFlags.Public Or
            System.Reflection.BindingFlags.Instance,
        Nothing, dialog, New Object() {value},
        System.Globalization.CultureInfo.InvariantCulture)
End Sub
private void PageSetupDialogHidden() 
{ 
    Word.Dialog dialog = Application.Dialogs[Word.WdWordDialog.wdDialogFilePageSetup];

    InvokeHelper(dialog, "PageWidth", "3.3\"");
    InvokeHelper(dialog, "PageHeight", "6\"");
    InvokeHelper(dialog, "TopMargin", "0.71\"");
    InvokeHelper(dialog, "BottomMargin", "0.81\"");
    InvokeHelper(dialog, "LeftMargin", "0.66\"");
    InvokeHelper(dialog, "RightMargin", "0.66\"");
    InvokeHelper(dialog, "HeaderDistance", "0.28\"");
    InvokeHelper(dialog, "Orientation", "0");
    InvokeHelper(dialog, "DifferentFirstPage", "0");
    InvokeHelper(dialog, "FirstPage", "0");
    InvokeHelper(dialog, "OtherPages", "0");

    // Apply these settings only to the current selection with this line of code:
    InvokeHelper(dialog, "ApplyPropsTo", "3"); 

    // Apply the settings.
    dialog.Execute(); 
}

private static void InvokeHelper(Word.Dialog dialog, string member, string value)
{
    System.Type dialogType = typeof(Word.Dialog);

    // Set the appropriate property of the dialog box.
    dialogType.InvokeMember(member,
        System.Reflection.BindingFlags.SetProperty |
            System.Reflection.BindingFlags.Public |
            System.Reflection.BindingFlags.Instance,
        null, dialog, new object[] { value },
        System.Globalization.CultureInfo.InvariantCulture);
}

Ayrıca bkz.

Görevler

Nasıl Yapılır: Yerleşik Word İletişim Kutularını Kullanma

Office Çözümlerinde Geç Bağlama

Başvuru

Yansıma (C# ve Visual Temel)

Diğer Kaynaklar

Word Nesne Modeline Genel Bakış