Link to home
Start Free TrialLog in
Avatar of skrombeen
skrombeen

asked on

Adding a function to the right click event of ms word

Good Day

I have a sql database with a list of companies and their relevant logos...
I need to code in ms word if its version dependent then 2003...

In the application i have written my users will be able to copy information relevant to the above company and paste this into word, but when they go to paste the information i need to add menu item to the right click event which will allow them to choose the company from a list, the application will then paste the information and will include the selected companies logo...

any suggestions or coding samples out thier???

Thanking you in advance!
Regards
Shaun
P.s the application will be developed in vb.net
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

I am not sure what you are asking for.  Are you asking for how to create code to write Word documents, or process all those steps?

Bob
Avatar of skrombeen
skrombeen

ASKER

Hi Bob

no that is not what im asking...

Im asking if i can write my own right click menu for ms-word?

Shaun
Shaun,

If you are looking for a Word add-in, then I am glad that I asked before I went off on a tilt.  

Good luck finding what you are looking for ;)

Bob
Thanks Bob, I'll await for the next answer!

cheers
OK, now I am confused. You say you are writing an app in VB.NET, but you want to create a right-click menu for Word. Do you mean that you are displaying a Word doc inside a VB.NET form? If that is the case, you will probably need to use Word macro tools to make the popup. See ShowPopup in the Word macro help.

If you need to right-click in a VB.NET form itself, then you need to trap for the mouse click event in the appropriate control. After you get the mouse click, you check to see if it's a right click, and then do what you need to do.

The code to make sure you have the right button is:

Private Sub RightClick (ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyControl.MouseUp
        If Not e.Button = System.Windows.Forms.MouseButtons.Right Then Exit Sub
 
        'Do your stuff here

End Sub

Add a context menu control to your form and name it mnuContext. Add code along these lines to the RightClick stuff:
       Dim strFill as String = "One.Two.Three.Four"
       Dim mnuItems() as MenuItem
       mnuContext = New ContextMenu
        arrContext = strFill.Split(".")
        Dim i As Integer
        ReDim mnuItems(arrContext.GetUpperBound(0))
        For i = 0 To arrContext.GetUpperBound(0)
            mnuItems(i) = New MenuItem
            mnuItems(i).Text = arrContext(i)
            mnuContext.MenuItems.Add(mnuItems(i))
            AddHandler mnuItems(i).Click, AddressOf DoRightClick
        Next
     
        mnuContext.Show(sender, New Point(e.X, e.Y))

DoRightClick is a subroutine that figures out which item was clicked and takes the appropriate action.

WARNING: I am sure that code is not letter perfect!

ctm5
Hi ctm5

thank you for this info, but unfortunetly it is not what i am looking for...

Below is point by point of what i need to do:
- I have written a vb.net application which captures company information the user enters aswell as their logo
- I have a sql database which stores this information

The users of the system will be compiling a research document based on this information they have captured, because the information will be peiced together from different companies in the database... and in order for them to keep track of where the information comes from within MS-Word, when they copy the information from my application copy (ctrl + C) and when they want to paste this information into MS-Word, they will need to right click or have a menu item, which will bring up a list of companies in the sql database, once they select one of these companies the information in the clipboard will be pasted in MS-Word aswell as the selected companies Logo...

Does this make more sense?

tx
I'm still not clear if the right-click menu appears in VB.NET app or in Word.

The info is in a database. You want to get it into a Word doc. Is that right?

How are you displaying the info in the VB.NET app? If you are displaying the info, what is the purpose of the right-click?

ctm5
Hi ctm5

no...
My users will browse the information in my vb.net application...
When they find the information they are looking for they will copy this information my using CTRL + C.

Then they want to paste this info into MS-Word: They will go about it by right clicking in word and selecting the company they have copied the information from, then word will know to put the relevant logo of the company...

i.e
***********My Application**************
Company Name: Company xyz; Logo: <image>; Brief Description: Company xyz started in 1980 by doing blah blah blah blah blah joe soap

***********IN MS-Word***************

Thesis shaun, this is my thesis i have written blah blah, this is my information i have found:
started in 1980 by doing <image>

***********************************

in essence: My users will browse the data in the vb.net application, once they find the data/phrase they want to use they will copy this info onto the clipboard, then in word they will be able to paste this data/phrase into the document they are compiling, and when they do this they need to select the company they have copied the data from a menu item(list of companies will come from the sql db) that will appear when they right click in MS-WORD.
Oh, I see. Sort of.

So you really just need the code to build the menu in Word itself. Word is not appearing as a control in your VB.NET app; it is running "normally."

Then you have to use Word macros. As I mentioned, ShowPopup is probably what you want. Remember that Word macros are essentially VB Lite; you can use datasources, etc. But I really think you ought to repost this question in the Word area, because, as I understand it, databases become more or less a MailMerge data source with Word.

But I would think that you might be able to do all this stuff in your VB.NET code if you change your approach slightly. Does your .NET app "know" about the Word doc? (Did it create it, for instance?) If so, you can manipulate the doc from VB.NET and paste the phrase and the logo in. Let me know if this interests you.

ctm5
Hi

My vb.net app knows nothing about the word document...
I did initially have this posted their, but ee support told me to post it to the vb.net section as i had no response it 5 days by posting it their...

You suggested i change my approach? any suggestions?

tx
Do you have any experience manipulating Word docs within a VB.NET app?

The broad view:

Include a reference to Word in your VB.NET app.
Determine if Word is running (using Processes)
If so, find the correct doc.
With the Word ActiveDocument, do the paste.

I can be more specific tomorrow, but right now I have to get cracking on something that's due today.

ctm5
SOLUTION
Avatar of cavehop
cavehop
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, this information looks sufficient!

I will give this a bash , if i have any problems I will let you guys know!