-
April 19th, 2013, 10:32 AM
#1
Macro for insert oval shape in table cell (word 2007)
Hi everyone!
I'm building a macro for inserting a oval shape in the current selected cell of a table. That is: the cell isn't selected but the cursor is at the cell where I want to insert the shape.
The thing is that i want it to be placed at an exact point of that cell but the macro doesn't recognize it. It recognizes the page and places the shape in the point I determine but relatively to the page instead of the current cell.
So I have the following code. What's wrong with it?
Thanks to anyone who can help me!
Ricardo
Sub InsWhiteTurn()
On Error GoTo ErrorHandler
Set sh = ActiveDocument.Shapes.AddShape(Type:=msoShapeOval, _
Left:=0, Top:=0, Width:=CentimetersToPoints(0.4), Height:=CentimetersToPoints(0.4))
With sh
.Line.Weight = 0.75
.Line.ForeColor = vbBlack
.Fill.ForeColor = vbWhite
.WrapFormat.Type = wdWrapNone
.LayoutInCell = True
.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
.Left = CentimetersToPoints(8)
.RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
.Top = CentimetersToPoints(6.15)
End With
Exit Sub
ErrorHandler:
End Sub
PS: I'm using open explicit to determine my variables
-
April 19th, 2013, 11:34 AM
#2
Re: Macro for insert oval shape in table cell (word 2007)
You can't HARD-CODE the TOP or LEFT properties, until you calculate WHERE that spot is supposed to be. You are in the FIRST statement.
-
April 19th, 2013, 03:16 PM
#3
Re: Macro for insert oval shape in table cell (word 2007)
Thank you very much for your quick reply dglienna!
Unfortunately, I'm not a VBA expert (I'm a newbie, actually ) so I don't think I understand completely what you meant.
Can you tell me how to correct my code so that the shape is inserted where I want - 8cm from left and 6.15 cm from the top of the cell where the cursor is, when I run the macro? Or are you saying it's impossible?
What do you mean by "You are in the FIRST statement."?
Thanks for your help,
Ricardo
-
April 19th, 2013, 07:43 PM
#4
Re: Macro for insert oval shape in table cell (word 2007)
Left:=0, Top:=0 is relative to the screen. Upper left. I think there is a way to convert a cell location, but you'd have to convert it to the same screen scale that you're using. Look up ABSOLUTE COORDINATES in EXCEL.
Also, this is a VB6 forum, not VBA
-
April 20th, 2013, 04:38 AM
#5
Re: Macro for insert oval shape in table cell (word 2007)
Thanks again David!
Where should I post my question then? Is there a forum for VBA here at codeguru?
I see what you mean and that's exactly my problem. I want to refer to where the cursor is (not the mouse cursor, the typing cursor). Which is in a cell in a table in word 2007.
Thank you once again.
-
April 20th, 2013, 06:58 AM
#6
Re: Macro for insert oval shape in table cell (word 2007)
I've managed to find in another forum something that could help me.
Here goes the code for anyone who may need it:
Sub InsTurnoBrancas()
'
'Inserir indicador de turno das brancas
'
On Error GoTo ErrorHandler
Set IndTurnBranc = ActiveDocument.Shapes.AddShape(Type:=msoShapeOval, _
Left:=fcnXCoord, Top:=fcnYCoord, Width:=CentimetersToPoints(0.4), Height:=CentimetersToPoints(0.4))
With IndTurnBranc
.Line.Weight = 0.75
.Line.ForeColor = vbBlack
.Fill.ForeColor = vbWhite
.WrapFormat.Type = wdWrapNone
.LockAnchor = True
.LayoutInCell = True
.RelativeHorizontalPosition = wdRelativeHorizontalPositionColumn
.RelativeVerticalPosition = wdRelativeVerticalPositionParagraph
End With
Exit Sub
ErrorHandler:
End Sub
Function fcnXCoord() As Double
'I've to sum the 8 centimeters to the point where the cursor is.
fcnXCoord = Selection.Information(wdHorizontalPositionRelative ToPage) + CentimetersToPoints(8)
End Function
Function fcnYCoord() As Double
'I've to subtract the 1.5 centimeters to the point where the cursor is because I've a picture in the cell so the cursor is not on the upper left of the cell, it's after the picture.
fcnYCoord = Selection.Information(wdVerticalPositionRelativeTo Page) - CentimetersToPoints(1.5)
End Function
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|