We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Add pictures and group ShapeRange

I have the following code in vba that I'm trying to convert using Aspose.Words, but I didn't succeed in knowing how to convert it correctly:

Public shpCol As Collection
Public shps()
Public picRg As Range

Public Type NPMapSite
SiteName As String
specIndex As Long 'point to npSpecimen
fontSize As Long 'image file element 0
left As Long 'image file element 1
Top As Long 'image file element 2
Width As Long 'image file element 3
Height As Long 'image file element 4
ItemLeft As Long 'image file element 5
ItemTop As Long 'image file element 6
ItemWidth As Long 'image file element 7
ItemHeight As Long 'image file element 8
PTI As String
Gleason As String
color As Long
cat As Long
label As String
End Type

Public Type NPMap
Name As String
HaveSites As Boolean
siteCount As Long
DiagramName As String
DiagramFileName As String
DiagramHorizontal As Double
DiagramVertical As Double
EffectType As String
indexes() As String 'index to the map parts
locations() As String 'names for the map parts
mapsite() As NPMapSite
NoPcs As Long 'point to which map
End Type

Sub DiagramDraw(mapset As NPMap, Pic_Bookmark As String, Optional Pic_Scale As Double = 1#)
On Error GoTo bug
Dim i As Integer
Dim j As Integer
Dim pic As Shape
'If Diagram Name is blank, exit sub
If mapset.DiagramFileName = Empty Then
Exit Sub
End If
'Add the Map
Set pic = docReport.Shapes.AddPicture(mapset.DiagramFileName, LinkToFile:=False, SaveWithDocument:=True, Width:=mapset.DiagramHorizontal, Height:=mapset.DiagramVertical, Anchor:=docReport.Bookmarks(Pic_Bookmark).Range)
pic.LockAnchor = True
Set shpCol = New Collection
shpCol.Add pic
Set picRg = docReport.Bookmarks(Pic_Bookmark).Range

'Add the Annotations
For i = 1 To UBound(mapset.mapsite)
'addAnnotation mapset, mapset.mapsite(i)
'addAnnotation is a function I use in my vba project.
Next i
If shpCol.Count > 1 Then
For i = 1 To shpCol.Count
ReDim Preserve shps(i - 1)
shps(i - 1) = shpCol(i).Name
Next i
Set shpRange = docReport.Shapes.Range(shps)
Set shpRange = shpCol(1).Ungroup
End If
shpRange.Width = shpRange.Width * Pic_Scale
If Not shpRange.LockAspectRatio Then shpRange.Height = shpRange.Height * Pic_Scale
End Sub

I'm using VB.net (the types are converted to Structures in VB.net)
Can you please help me in this?
Thanks in advance,

Hi Saleem,

Thanks for your inquiry. The shared VBA code inserts the shape (image) in the document and group the shapes If more than one images are inserted.

You can insert an image into document using DocumentBuilder.InsertImage method. I suggest you please read the overloaded methods of DocumentBuilder.InsertImage from here:


Please check the following code example to group the shapes. Hope this helps you.

Document doc = new

GroupShape gs = new GroupShape(doc);

double left = double.MaxValue;

double right = double.MinValue;

double top = double.MaxValue;

double bottom = double.MinValue;

Node[] shapes = doc.GetChildNodes(NodeType.Shape, true).ToArray();

foreach (Shape s in shapes)



left = s.Left < left ? s.Left : left;

right = s.Right > right ? s.Right : right;

top = s.Top < top ? s.Top : top;

bottom = s.Bottom > bottom ? s.Bottom : bottom;


double width =right - left;

double height = bottom - top;

gs.Width = width;

gs.Height = height;

gs.CoordSize = new Size((int)width, (int)height);

DocumentBuilder builder = new DocumentBuilder(doc);



Please manually create your expected Word document using Microsoft Word and attach it here for our reference. We will investigate as to how you want your final Word output be generated like. We will then provide you more information on this along with code.

Thanks a lot for your quick reply :slight_smile:
I’ll check this code out and see how ti works for me.

Hi Tahir.

I succeeded in showing the images on my document, but the grouping didn’t work as I want, I tried a debugged a lot but couldn’t fix it. Below you’ll find a simplified class similar to the one I use mainly, and I’ll attach an doc with the image I expect to see as result, I really hope that you can help me.

Imports Aspose.Words.Drawing
Imports Aspose.Words
Imports System.Drawing

Public Class Class1

<span style="color:blue;">Private</span> <span style="color:blue;">Sub</span> DiagramDraw(<span style="color:blue;">ByVal</span> mapset <span style="color:blue;">As</span> <span style="color:#2b91af;">NPMap</span>, <span style="color:blue;">ByVal</span> Pic_Bookmark <span style="color:blue;">As</span> <span style="color:blue;">String</span>, <span style="color:blue;">Optional</span> <span style="color:blue;">ByVal</span> Pic_Scale <span style="color:blue;">As</span> <span style="color:blue;">Double</span> = 1.0#)
    <span style="color:blue;">Dim</span> i <span style="color:blue;">As</span> <span style="color:blue;">Integer</span>
    <span style="color:blue;">Dim</span> pic <span style="color:blue;">As</span> System.Drawing.<span style="color:#2b91af;">Image</span>

    <span style="color:green;">'Add the Map</span>
    pic = System.Drawing.<span style="color:#2b91af;">Bitmap</span>.FromFile(mapset.DiagramFileName)

    <span style="color:blue;">If</span> gs <span style="color:blue;">Is</span> <span style="color:blue;">Nothing</span> <span style="color:blue;">Then</span> gs = <span style="color:blue;">New</span> <span style="color:#2b91af;">GroupShape</span>(docReport)

    gs.WrapType = <span style="color:#2b91af;">WrapType</span>.Tight
    gs.RelativeHorizontalPosition = <span style="color:#2b91af;">RelativeHorizontalPosition</span>.Page
    gs.RelativeVerticalPosition = <span style="color:#2b91af;">RelativeVerticalPosition</span>.Page
    gs.VerticalAlignment = <span style="color:#2b91af;">VerticalAlignment</span>.Center
    gs.HorizontalAlignment = <span style="color:#2b91af;">HorizontalAlignment</span>.Center
    gs.Left = 0
    gs.Top = 0
    gs.Height = (pic.Height / pic.VerticalResolution) * 72
    gs.Width = (pic.Width / pic.HorizontalResolution) * 72

    gs.CoordOrigin = <span style="color:blue;">New</span> <span style="color:#2b91af;">Point</span>(0, 0)
    gs.CoordSize = <span style="color:blue;">New</span> <span style="color:#2b91af;">Size</span>(gs.Width, gs.Height)

    gs.AnchorLocked = <span style="color:blue;">True</span>


    <span style="color:blue;">Dim</span> DiagramShape <span style="color:blue;">As</span> <span style="color:#2b91af;">Shape</span> = builder.InsertImage(mapset.DiagramFileName)


    <span style="color:green;">'Add the Annotations</span>
    <span style="color:blue;">For</span> i = 1 <span style="color:blue;">To</span> UBound(mapset.mapsite)
    <span style="color:blue;">Next</span> i

    gs.Height = gs.Height * Pic_Scale
    gs.Width = gs.Width * Pic_Scale

<span style="color:blue;">End</span> <span style="color:blue;">Sub</span>

<span style="color:blue;">Private</span> <span style="color:blue;">Sub</span> addAnnotation()
    <span style="color:blue;">Dim</span> EffectShape <span style="color:blue;">As</span> <span style="color:#2b91af;">Shape</span> = <span style="color:blue;">Nothing</span>
    <span style="color:blue;">Dim</span> EffectShape2 <span style="color:blue;">As</span> <span style="color:#2b91af;">Shape</span>
    <span style="color:blue;">Dim</span> TextShape <span style="color:blue;">As</span> <span style="color:#2b91af;">Shape</span>

    <span style="color:blue;">Dim</span> str <span style="color:blue;">As</span> <span style="color:blue;">String</span> = <span style="color:#a31515;">"GS: 2+3=5"</span> & ChrW(11) & <span style="color:#a31515;">"PTI: 50%"</span>

    TextShape = <span style="color:blue;">New</span> <span style="color:#2b91af;">Shape</span>(docReport, <span style="color:#2b91af;">ShapeType</span>.TextBox)
    TextShape.Left = 23
    TextShape.Top = 38.75
    TextShape.Width = 46.75
    TextShape.Height = 26.25

    <span style="color:green;">'****** I did'nt find how to do these in ASPOSE ******</span>
    <span style="color:green;">'TextShape.Fill.Transparency = 1</span>
    <span style="color:green;">'TextShape.line.Visible = msoFalse</span>

    <span style="color:blue;">Dim</span> para <span style="color:blue;">As</span> <span style="color:#2b91af;">Paragraph</span> = <span style="color:blue;">New</span> <span style="color:#2b91af;">Paragraph</span>(docReport)
    <span style="color:blue;">Dim</span> rn <span style="color:blue;">As</span> <span style="color:blue;">New</span> <span style="color:#2b91af;">Run</span>(docReport, str)

    rn.Font.Size = 6
    rn.Font.Name = <span style="color:#a31515;">"Times New Roman"</span>

    <span style="color:green;">'****** I did'nt find how to do these in ASPOSE ******</span>
    <span style="color:green;">'TextShape.TextFrame.TextRange.Font.Size = mapsite.fontSize</span>
    <span style="color:green;">'TextShape.TextFrame.TextRange.Font.Bold = False</span>
    <span style="color:green;">'TextShape.TextFrame.TextRange.ParagraphFormat.Alignment = wdAlignParagraphCenter</span>

    TextShape.ZOrder = 1
    TextShape.TextBox.FitShapeToText = <span style="color:blue;">True</span>

    EffectShape = <span style="color:blue;">New</span> <span style="color:#2b91af;">Shape</span>(docReport, <span style="color:#2b91af;">ShapeType</span>.Rectangle)
    EffectShape.Left = 33
    EffectShape.Top = 33.75
    EffectShape.Width = 35
    EffectShape.Height = 10
    DefaultShapeFormat(EffectShape, <span style="color:#a31515;">"Red"</span>)

    EffectShape2 = <span style="color:blue;">New</span> <span style="color:#2b91af;">Shape</span>(docReport, <span style="color:#2b91af;">ShapeType</span>.Rectangle)
    EffectShape2.Left = 50.5
    EffectShape2.Top = 33.75
    EffectShape2.Width = 17.5
    EffectShape2.Height = 8.75
    DefaultShapeFormat(EffectShape2, <span style="color:#a31515;">"Blue"</span>)
<span style="color:blue;">End</span> <span style="color:blue;">Sub</span>

<span style="color:blue;">Private</span> <span style="color:blue;">Sub</span> DefaultShapeFormat(shpAnno <span style="color:blue;">As</span> <span style="color:#2b91af;">Shape</span>, lngcolor <span style="color:blue;">As</span> <span style="color:blue;">String</span>)
    <span style="color:blue;">With</span> shpAnno
        .FillColor = System.Drawing.<span style="color:#2b91af;">Color</span>.FromName(lngcolor)
        .Font.Color = <span style="color:#2b91af;">Color</span>.Black
        .WrapType = <span style="color:#2b91af;">WrapType</span>.Square
        .StrokeWeight = 0.75
    <span style="color:blue;">End</span> <span style="color:blue;">With</span>
<span style="color:blue;">End</span> <span style="color:blue;">Sub</span>

End Class


Hi Saleem,

Thanks for your inquiry. It would be great if you please share following detail for investigation purposes.

  • Please attach your input Word document.
  • Please

    create a standalone/runnable simple application (for example a Console
    Application Project
    ) that demonstrates the code (Aspose.Words code) you used to generate
    your output document

  • Please attach the output Word file that shows the undesired behavior.
  • Please
    attach your target Word document showing the desired behavior. You can
    use Microsoft Word to create your target Word document. I will
    investigate as to how you are expecting your final document be generated
    like. It seems that Result.docx is your expected document.

As soon as you get these pieces of information to
us we’ll start our investigation into your issue.

Hi Tahir,

Thank you a lot for following my posts, I really appreciate your great support :slight_smile:

Now about my inquiry, I kept trying and searching for a solution and last night I achieved it :smiley:
So I will not make you waste your time on it anymore.
But expect more questions from me, since all what I’m doing in my days now is ASPOSE :wink:
Thanks again.

Hi Saleem,

Thanks for your feedback. It is nice to hear from you that you have found the solution of your issues. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.