#1
|
|||
|
|||
How to add an event on a shape in document?
Hello all,
If I have 10 shapes in a word document, Is it possible that when I click some shape (one of the 10 shapes), it can call some sub? Thanks in advance for your help. |
#2
|
||||
|
||||
Yes, by putting the picture in a Picture content control or a Rich Text content control and employing a ContentControlOnEnter macro.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Thank you so much.
But that won't achieve the effect I want! I add 4*5=20 shapes in document and each shape has word on it. Below is my sample pic: card.jpg - Google Drive The effect I need is that when I cilck one of the shapes,the shape will change to another color. If I use content control,it seems that I Can't put the shapes where I want. Though I can put a picture in a "wdContentControlBlockPicture",but I can't add some words just up on it. Or if I use content control(wdContentControlRichText),may I change it's background color ? Do you have any suggestion? Thank you! |
#4
|
|||
|
|||
You could use the Selection Change event. Insert a new Class module and name it clsMonitor. Insert this code:
Code:
Option Explicit Private WithEvents mWordApp As Word.Application Private oShp As Shape Private Sub Class_Initialize() Set mWordApp = Word.Application End Sub Private Sub mWordApp_WindowSelectionChange(ByVal oSel As Selection) If Selection.ShapeRange.Count = 1 Then If oShp Is Nothing Then Set oShp = Selection.ShapeRange(1) Select Case oShp.TextFrame.TextRange.Text Case "Test" & Chr(13): MsgBox oShp.TextFrame.TextRange.Text Case "Good" & Chr(13): MsgBox oShp.TextFrame.TextRange.Text 'Etc. End Select Else If Selection.ShapeRange.TextFrame.TextRange <> oShp.TextFrame.TextRange Then Set oShp = Selection.ShapeRange(1) Select Case oShp.TextFrame.TextRange.Text Case "Test" & Chr(13): MsgBox oShp.TextFrame.TextRange.Text Case "Good" & Chr(13): MsgBox oShp.TextFrame.TextRange.Text 'Etc. End Select End If End If Else Set oShp = Nothing End If End Sub Insert a new standard module and insert this code: Code:
Option Explicit Private oCls As clsMonitor Sub AutoOpen() Set oCls = New clsMonitor End Sub Run AutoOpen and click in the Test or Good shapes. Adjust code to suit your needs. |
#5
|
||||
|
||||
Quote:
In any event, you can also put the shapes into a table so they're positioned as per your layout.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#6
|
|||
|
|||
Quote:
Thank you for your reply~ I try it , but the result is wired. I recorded the whole process as below: myshape.mp4 - Google Drive I am new about VBA,so I don't understand why it is like this. In that video,it happends that some shapes is invisible. Only the last one is visible. Then,when I click the area where the shapes should be,two shapes appear(why ?) . Thank you ~ |
#7
|
|||
|
|||
Quote:
I try your suggest. I try to use table instead of shape. But I am really not good at VBA,it still can't work. I have a Class Module (named "ClassCell") Code:
Public WithEvents objCell As Word.Application Private Sub objCell_Click() MsgBox "Hello" End Sub I have a sub in My standard Module: Code:
Global theCell() As New ClassCell Global tableNew As Table Sub toTest() Set tableNew = ThisDocument.Tables.Add(Selection.Range, 5, 4) Set theCell(1).objCell = ThisDocument.Tables(1).Cell(2, 1) End Sub I ran the sub "toTest",then some message appeared: 『 Type mismatch (Error 13) 』 Do you have any suggestion? Thank you! |
#8
|
||||
|
||||
Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
Hi,macropod:
I really appreciate your kind assistance ! I actually have read the material in the link . But I really not good at VBA,so I spent a lot of time to research the file you offered. The reason I ignored it is that "formfield" has automatic event,but " table cell ' doesn't. According to your suggestion,I should put some formfield in the table,right? My problem is that I have no ideal about which formfield I should put in. Again,I really appreciate your kind assistance ! ^^ |
#10
|
||||
|
||||
There are three different examples in that link, all showing different ways you could approach the shading issue. Two use dropdown formfields, one uses a dropdown content control. None of this is hidden from view.
And no, there is no 'table cell' event in Word - just as there is no 'shape' event.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#11
|
|||
|
|||
Hi,macropod:
It's really difficult for a novice like me to follow your method. Finally,I gave up using Word VBA to complete it. I try to use Excel VBA instead and I get what I want. In Excel VBA,there is a native Sub named "Worksheet_SelectionChange". With it,even I am new to VBA,I can also achieve my goal easily. I still learn a lot from your suggetions. Thank you ~~~ ^^ engcard.mp4 - Google Drive |
Tags |
shape |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Unable to get cursor past inserted shape in Word document | slknova | Word | 6 | 11-09-2021 09:02 AM |
I don't understand event handling for the DoubleClick event | Larry_1 | Excel Programming | 5 | 12-28-2017 08:23 AM |
How to change size / shape of a shape in a stencil | tomgoodell | Visio | 1 | 06-30-2016 04:40 AM |
Shape hyperlink doesn't work with protected form document | Cosmo | Word | 0 | 12-10-2012 01:42 PM |
Connect Outlook "Event" to Office Document | Yick | Office | 1 | 09-23-2011 04:57 AM |