Getting Selected Text to my Application

  • Thread starter Joda-oda via OfficeKB.com
  • Start date
J

Joda-oda via OfficeKB.com

Imports Office = Microsoft.Office.Core
Imports Word = Microsoft.Office.Interop.Word

Class Form1

Dim oWord As Word.Application
Dim myBar As Office.CommandBar

Public WithEvents myButton As Office.CommandBarButton

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.
EventArgs) Handles Me.Load

Call Loading_Context_Menu()

End Sub

Public Sub Loading_Context_Menu()

Try

oWord = GetObject(, "Word.Application")

oWord.CommandBars("TEXT").Reset()

myBar = oWord.CommandBars("TEXT")

If myBar IsNot Nothing Then

Dim TEXTControlCount As Integer = myBar.Controls.Count

myButton = CType(myBar.Controls.Add(Type:=Office.
MsoControlType.msoControlButton, Before:=TEXTControlCount, Temporary:=True),
Office.CommandBarButton)

With myButton
.Caption = "Send to my Application"
.Tag = "Send to my Application"
.FaceId = 100
End With

End If

Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

End Sub

Public Sub myButton_Click(ByVal Ctrl As Microsoft.Office.Core.
CommandBarButton, ByRef CancelDefault As Boolean) Handles myButton.Click


TextBox1.Text = oWord.Selection.Text '<----- Doesn't Work

End Sub

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.
Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

oWord.CommandBars("TEXT").Reset()

End Sub

End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top