Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-20-2020, 05:07 PM
ydmq ydmq is offline How to add an event on a shape in document? Windows 10 How to add an event on a shape in document? Office 2013
Novice
How to add an event on a shape in document?
 
Join Date: Jun 2020
Posts: 6
ydmq is on a distinguished road
Question 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.
Reply With Quote
  #2  
Old 06-21-2020, 03:18 PM
macropod's Avatar
macropod macropod is offline How to add an event on a shape in document? Windows 7 64bit How to add an event on a shape in document? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 06-22-2020, 07:50 AM
ydmq ydmq is offline How to add an event on a shape in document? Windows 10 How to add an event on a shape in document? Office 2013
Novice
How to add an event on a shape in document?
 
Join Date: Jun 2020
Posts: 6
ydmq is on a distinguished road
Default

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!
Reply With Quote
  #4  
Old 06-22-2020, 11:31 AM
gmaxey gmaxey is offline How to add an event on a shape in document? Windows 10 How to add an event on a shape in document? Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

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.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 06-22-2020, 03:16 PM
macropod's Avatar
macropod macropod is offline How to add an event on a shape in document? Windows 7 64bit How to add an event on a shape in document? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by ydmq View Post
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 ?
Your attachment suggests you should be using a Word table rather than pictures. See, for example: https://www.msofficeforums.com/word-...html#post47254
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]
Reply With Quote
  #6  
Old 06-23-2020, 01:17 AM
ydmq ydmq is offline How to add an event on a shape in document? Windows 10 How to add an event on a shape in document? Office 2013
Novice
How to add an event on a shape in document?
 
Join Date: Jun 2020
Posts: 6
ydmq is on a distinguished road
Question

Quote:
Originally Posted by gmaxey View Post
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.

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 ~
Reply With Quote
  #7  
Old 06-28-2020, 05:40 AM
ydmq ydmq is offline How to add an event on a shape in document? Windows 10 How to add an event on a shape in document? Office 2013
Novice
How to add an event on a shape in document?
 
Join Date: Jun 2020
Posts: 6
ydmq is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
Your attachment suggests you should be using a Word table rather than pictures. See, for example: https://www.msofficeforums.com/word-...html#post47254
In any event, you can also put the shapes into a table so they're positioned as per your layout.
Thank you for your reply.
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!
Reply With Quote
  #8  
Old 06-28-2020, 03:47 PM
macropod's Avatar
macropod macropod is offline How to add an event on a shape in document? Windows 7 64bit How to add an event on a shape in document? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by ydmq View Post
Quote:
Originally Posted by macropod View Post
Your attachment suggests you should be using a Word table rather than pictures. See, for example: https://www.msofficeforums.com/word-...html#post47254
In any event, you can also put the shapes into a table so they're positioned as per your layout.
Do you have any suggestion?
One suggestion would be to actually read the material in the link. Your reply suggests you've ignored that entirely...
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 06-28-2020, 07:08 PM
ydmq ydmq is offline How to add an event on a shape in document? Windows 10 How to add an event on a shape in document? Office 2013
Novice
How to add an event on a shape in document?
 
Join Date: Jun 2020
Posts: 6
ydmq is on a distinguished road
Default

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 ! ^^
Reply With Quote
  #10  
Old 06-28-2020, 07:36 PM
macropod's Avatar
macropod macropod is offline How to add an event on a shape in document? Windows 7 64bit How to add an event on a shape in document? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #11  
Old 07-05-2020, 07:33 AM
ydmq ydmq is offline How to add an event on a shape in document? Windows 10 How to add an event on a shape in document? Office 2013
Novice
How to add an event on a shape in document?
 
Join Date: Jun 2020
Posts: 6
ydmq is on a distinguished road
Default

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
Reply With Quote
Reply

Tags
shape

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to add an event on a shape in document? 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

Other Forums: Access Forums

All times are GMT -7. The time now is 02:17 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft