Discussion:
Some Basics about Word Automation
(too old to reply)
Joda-oda
2009-03-04 20:03:03 UTC
Permalink
hi @ all,

i'm new in Automation...so i'm sry if my question aren't so professional.
What i want to do is, is to add an item in my right click context menu. When
i click on it, there shout be just appear a message box.

ok, my code:


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

Public Class Form1

Dim oWord As Word.Application
Dim oButton As Office.CommandBarControl

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

oWord = New Word.Application

oWord.Visible = True

oWord.Documents.Add()


End Sub

Private Sub Loading()

oButton = oWord.CommandBars("Text").Controls.Add

With oButton

.Caption = "TEST"
.FaceId =
.OnAction =
.BeginGroup = True

End With

oButton = Nothing

End Sub

Private Sub Unloading()

oWord.CommandBars("Text").Controls("TEST").Delete()

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Call Loading()

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click

Call Unloading()

End Sub


ok, this is a very simple Application, but enough for me right now.

my problems:

1) in my version, my programm opens everytime Word new. But now i want to
change the context menu of an opened Word document.

2) is there a way, that you can load a picture instead of using the faceid?

3) What do i have to wright after .OnAction? I tried it with the name of
an other Private Sub, but then word gets an error....

I, now these are a lot of question, but i really would appreciate it. THX
David Horowitz
2009-03-10 22:08:45 UTC
Permalink
Joda,
I have a couple answers, but not all.
Post by Joda-oda
1) in my version, my programm opens everytime Word new. But now i want to
change the context menu of an opened Word document.
When you write
oWord = New Word.Application
you are creating new instance of Word. I think you need to use something
like
oWord = GetObject(, "Word.Application")
to hook up to an existing instance of Word. I don't hav exact details.
If Word is not running, then the line above will fail, in which case you
could use yor oWord = New Word.Application.
Post by Joda-oda
2) is there a way, that you can load a picture instead of using the faceid?
You can set the Picture property instead of the FaceId. You should first
load a picture something like this:
Dim iPic As IPictureDisp
iPic = LoadPicture("c:\folder\pic.bmp")
then set it like oButton.Picture = iPic
You are supposed to use a transparency mask also, as in
Dim iPicMask As IPictureDisp
iPicMask = LoadPicture("c:\folder\mask.bmp")
then set it like oButton.Mask = iPicMask
I don't know how it will work if you don't use the mask.
Post by Joda-oda
3) What do i have to wright after .OnAction? I tried it with the name of
an other Private Sub, but then word gets an error....
You should enclose the name of the sub in quotes. OnAction is a string.
--
David Horowitz
Lead Technologist
Soundside Inc.
www.soundside.biz
Post by Joda-oda
i'm new in Automation...so i'm sry if my question aren't so professional.
What i want to do is, is to add an item in my right click context menu. When
i click on it, there shout be just appear a message box.
Imports Office = Microsoft.Office.Core
Imports Word = Microsoft.Office.Interop.Word
Public Class Form1
Dim oWord As Word.Application
Dim oButton As Office.CommandBarControl
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
oWord = New Word.Application
oWord.Visible = True
oWord.Documents.Add()
End Sub
Private Sub Loading()
oButton = oWord.CommandBars("Text").Controls.Add
With oButton
.Caption = "TEST"
.FaceId =
.OnAction =
.BeginGroup = True
End With
oButton = Nothing
End Sub
Private Sub Unloading()
oWord.CommandBars("Text").Controls("TEST").Delete()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Call Loading()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Call Unloading()
End Sub
ok, this is a very simple Application, but enough for me right now.
1) in my version, my programm opens everytime Word new. But now i want to
change the context menu of an opened Word document.
2) is there a way, that you can load a picture instead of using the faceid?
3) What do i have to wright after .OnAction? I tried it with the name of
an other Private Sub, but then word gets an error....
I, now these are a lot of question, but i really would appreciate it. THX
Joda-oda
2009-03-11 09:26:01 UTC
Permalink
thx for help;
right now my code is:

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

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

Private Sub Loading_Context_Menu()

Dim Pic As stdole.StdPicture
Pic = New stdole.StdPicture

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"
'.Picture = Pic
End With

End If

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

End Sub

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

TextBox1.Text = oWord.Selection.Text

End Sub

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

On Error Resume Next
oWord.CommandBars("TEXT").Reset()

End Sub

End Class

-----------

But i still can't load the picture....
iPic = LoadPicture("c:\folder\pic.bmp")
doesn't work, it says that the name "LoadPicture" isn't declared...
Do i have to add an other reference or other imports?

also the text in my textbox doesn't appear...
if i whrite: MsgBox(oWord.Selection.Text)
the selected word appears...but my application can't send it to my textbox

you know why?
David Horowitz
2009-03-12 18:56:00 UTC
Permalink
I think you need:
stdole.StdFunctions.LoadPicture
As to why your textbox doesn't work, if you write
TextBox1.Text = "This is some text" ' instead of oWord.Selection.Text
in MyButton_Click does it work?
--
David Horowitz
Lead Technologist
Soundside Inc.
www.soundside.biz
Post by Joda-oda
thx for help;
Imports Office = Microsoft.Office.Core
Imports Word = Microsoft.Office.Interop.Word
Imports System.Reflection
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
Private Sub Loading_Context_Menu()
Dim Pic As stdole.StdPicture
Pic = New stdole.StdPicture
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"
'.Picture = Pic
End With
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub myButton_Click(ByVal Ctrl As
Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean)
Handles myButton.Click
TextBox1.Text = oWord.Selection.Text
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
On Error Resume Next
oWord.CommandBars("TEXT").Reset()
End Sub
End Class
-----------
But i still can't load the picture....
iPic = LoadPicture("c:\folder\pic.bmp")
doesn't work, it says that the name "LoadPicture" isn't declared...
Do i have to add an other reference or other imports?
also the text in my textbox doesn't appear...
if i whrite: MsgBox(oWord.Selection.Text)
the selected word appears...but my application can't send it to my textbox
you know why?
Joda-oda
2009-03-13 11:15:01 UTC
Permalink
Pic = stdole.StdFunctions.LoadPicture("")

doesn't work either....no it says that StdFunctions is not a member of
stdole...and i don't know why :(
Maybe i need an other refrence...

And TextBox1.Text = "This is some text" doesn't work either... i don't know
why my function doesn't send it to may application

You have an idea? And also thx for helping me
Joda-oda
2009-03-18 15:13:14 UTC
Permalink
Help pls :(
i can't find a solution
Santy
2009-05-03 05:19:01 UTC
Permalink
try this if this works...loading the image to Clipboard from Resource file
and then on to the button...

button.Style = msoButtonIcon
Clipboard.SetData LoadResPicture(240, 0)
button.PasteFace
Post by Joda-oda
Help pls :(
i can't find a solution
Loading...