Hi,
ich versuch grad mal ne Symbolleiste mit einem Button in der Word-Anwendung zu erstellen.
Als Vorlage nutze ich diese Seite: http://msdn.microsoft.com/en-us/library/aa289518%28v=vs.71%29.aspx
Ich habe die Codes bei mir eingefügt und ggf. angepasst. Es werden bei mir keine Fehler angezeigt und ist kompilierbar.
Fürs Testen benutze ich Word 2003. Nun gut, ich habs getestet aber muss leider sagen dass es bei mir nicht funktioniert. In Word wird überhaupt keine Symbolleiste erstellt und kann auch keine neue Leiste im Kontextmenü (rechte Maustaste auf freie Stelle oben) sehen. Es muss doch funktionieren aber vielleicht kann mir JEMAND helfen?
Private Function AddWordToolbar(ByVal word As Outlook.Application, _
ByVal toolbarName As String) As Microsoft.Office.Core.CommandBar
Dim toolBar As Microsoft.Office.Core.CommandBar = Nothing
Try
' Create a command bar for the add-in
toolBar = CType(word.CommandBars.Add(toolbarName, _
Microsoft.Office.Core.MsoBarPosition.msoBarTop, , True), _
Microsoft.Office.Core.CommandBar)
toolBar.Visible = True
Return toolBar
Catch
' Add exception handling here.
Return Nothing
End Try
End Function
Private Function MakeANewButton(ByVal commandBar As _
Microsoft.Office.Core.CommandBar, ByVal caption As String, _
ByVal faceID As Integer, ByVal clickHandler As _
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler) _
As Microsoft.Office.Core.CommandBarButton
Try
Dim newButton As Microsoft.Office.Core.CommandBarButton
newButton = CType(commandBar.Controls.Add( _
Microsoft.Office.Core.MsoControlType.msoControlButton), _
Microsoft.Office.Core.CommandBarButton)
newButton.Caption = caption
newButton.FaceId = faceID
AddHandler newButton.Click, clickHandler
Return newButton
Catch ex As System.Exception
' Add code here to handle the exception.
Return Nothing
End Try
End Function
Public Sub insertText_Click(ByVal barButton As _
Microsoft.Office.Core.CommandBarButton, ByRef someBool As Boolean)
Dim text As String = ""
Dim data As System.Windows.Forms.IDataObject = _
System.Windows.Forms.Clipboard.GetDataObject()
If data.GetDataPresent(System.Windows.Forms.DataFormats.Text) Then
text = data.GetData( _
System.Windows.Forms.DataFormats.Text).ToString()
If (Not wordApp Is Nothing) Then
Me.wordApp.ActiveWindow.Selection.InsertBefore(text)
End If
End If
End Sub
Public Sub styleText_Click(ByVal barButton As _
Microsoft.Office.Core.CommandBarButton, ByRef someBool As Boolean)
Dim code As Object = "Code"
If (Not wordApp Is Nothing) Then
Me.wordApp.ActiveWindow.Selection.Style = code
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
'Dim toolbar As Microsoft.Office.Core.CommandBar
'Dim wordApp As Microsoft.Office.Interop.Outlook.Application = Nothing
'SetApplicationFields(wordApp.Application)
'If Not wordApp Is Nothing Then
' ToolBar = AddWordToolbar(wordApp, "Some useful toolbar.")
'End If
Dim insertText As Microsoft.Office.Core.CommandBarButton
Dim styleText As Microsoft.Office.Core.CommandBarButton
' Visual Basic
' Create a toolbar.
Dim toolbar As Microsoft.Office.Core.CommandBar = Nothing
If Not wordApp Is Nothing Then
toolbar = AddWordToolbar(wordApp, "Some useful toolbar.")
End If
' Create a button to add text.
insertText = MakeANewButton(toolbar, "Insert text", 1044, _
AddressOf insertText_Click)
' Create a button to style selected text.
styleText = MakeANewButton(toolbar, "Style text", 1081, _
AddressOf styleText_Click)
End Sub Danke im Voraus. ;) |