Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I have a word document that contains 'n' number of Shapes. I want to replace one of them using the Shape.Title property.

Using the below code I am able to replace it, but the problem is the following:
The new shape is placed in a different place in the document.

I alreay searched on google and also in CodeProject regarding the procedure to put the Shape in the right place, but the result is always the same.

Can anyone help me?

Thank you in advance

What I have tried:

VB
Dim myDoc As Document 'Word document

Public Function ReplaceShape(shapeTitle As String, sFileName As String)

  Const tmpTitle As String = "SHAPE_NEW_NAME"
  Dim sh As Shape

  Try
    'looking for the shape...
    For Each shp As Shape In myDoc.Shapes
      If shp.Title = shapeTitle Then
        'placing the new shape
        sh = myDoc.Shapes.AddPicture(sFileName, Anchor:=shp.Anchor)
        sh.Left = shp.Left
        sh.Top = shp.Top
        sh.Width = shp.Width
        sh.Height = shp.Height
        sh.Title = tmpTitle
        'removing the old shape
        shp.Delete()
      End If
    Next
    Return True
  Catch ex As Exception

    Return False
  End Try
End Function
Posted
Updated 12-Jun-22 10:13am
v2

Using the information you provide, I looked up "myDoc.Shapes.AddPictures" using Google. Picking the top return (microsoft.docs sounds like a winner):

Shapes.AddPicture method (Excel) | Microsoft Docs[^]

At the end of the post I see what could be the EXACT solution you're looking for: by explicitly placing the "Shape" at the coordinates one should be able to customize some default placement.

Since we don't really know what interface to VB you've got running, it's hard to describe an easier way. Typically more information is available to a user of VisualStudio, or in Excel in the Visual Basic for Applications IDE; it's probably the same interface used by Word. You can usually access positions of controls in either case by opening the Properties window. At least this has been my experience.
 
Share this answer
 
Comments
Silvano Bianchi 10-Jun-22 15:08pm    
Dear RedDk

Thank you very much for your suggestion.
I will carefully read the link you suggested and then I'll try to implement the solution.
Silvano Bianchi 12-Jun-22 16:13pm    
I looked for the solution to the problem, but from what I have read, it is not easy to obtain the .Top value of the Shape.

So I decided to use a work around.

For those who may be interested:

In place of the shapes, I insert in the word document some text boxes in which I put the shapes.

So I replace the shapes with the following code.
RedDk 12-Jun-22 18:46pm    
Always the answer: "Whatever works" (no matter for how short a time ...) ;)
VB
Dim myDoc As Document 'Word document
Public Function ReplaceInLineShape(inLineShapeTitle As String, sFileName As String)
    Const tmpTitle As String = "_SHAPE_OBJECT_TEMPORARY_NAME_"
    Dim ilsh As InlineShape
    Try
      'Loop though all shapes and looking for inlineshape
      For Each shp As Shape In myDoc.Shapes
        Try
          For Each ilshp As InlineShape In shp.TextFrame.TextRange.InlineShapes
            If ilshp.Title = inLineShapeTitle Then
              ilsh = shp.TextFrame.TextRange.InlineShapes.AddPicture(sFileName)
              ilsh.Height = ilshp.Height
              ilsh.Width = ilshp.Width
              ilsh.Title = tmpTitle
              ilshp.Delete()
            End If
          Next
        Catch ex As Exception
        End Try
      Next

    Catch ex As Exception
      Return False

    End Try

    Return True
  End Function
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900