Macro for insert oval shape in table cell (word 2007)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Macro for insert oval shape in table cell (word 2007)

  1. #1
    Join Date
    Apr 2013
    Posts
    4

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,958

    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.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Apr 2013
    Posts
    4

    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

  4. #4
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,958

    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
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Apr 2013
    Posts
    4

    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.

  6. #6
    Join Date
    Apr 2013
    Posts
    4

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center