C# Range Parameter in application.Selection.InlineShapes.AddPicture

Robin 66 Reputation points
2022-04-08T11:06:35.937+00:00

Hello,

I am doing a Word MailMerge in C#. I am inserting an image using the following statement:

application.Selection.InlineShapes.AddPicture(ImageFullPath);

The problem is, the image is inserted in the middle of the document covering the entire width of the document. Can someone explain me how to use the Range parameter so that I can specify exactly where the image should go in the document?

Thanks

Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2022-04-11T09:41:24.46+00:00

    @Robin , Welcome to Microsoft Q&A, you could try the following code to insert the picture to the word file and set the size of picture in the file.

            object missing = System.Reflection.Missing.Value;  
            object name = @"C:\Users\username\Desktop\test111.docx";  
            object Range = System.Reflection.Missing.Value;  
      
            //Start Word and create a new document.  
            Word.Application oWord;  
            Word.Document oDoc;  
            oWord = new Word.Application();  
            oDoc = oWord.Documents.Open(ref name, ref missing, ref missing,  
      ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,  
      ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);  
            //bool t = oWord.Selection.Find.Execute("You Can");  
            Word.Range range = oDoc.Paragraphs[3].Range;  
            //range.SetRange(range.Start, range.End - 10);  
            Word.InlineShape autoScaledInlineShape = range.InlineShapes.AddPicture(@"C:\Users\username\Desktop\W10.jpg");  
            autoScaledInlineShape.Width = 50;  
            autoScaledInlineShape.Height = 70;  
      
            oDoc.Save();  
            oDoc.Close();  
    

    192251-image.png

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Robin 66 Reputation points
    2022-04-12T09:00:21.61+00:00

    Thanks @Jack J Jun .

    I am very happy with your solution. I found the problem why it couldn't find the 'Customer Signature' line on the template. The issue was caused by the MergeField on my code -

    application.Selection.InlineShapes.AddPicture(PngFileFullPath);

    This was overwriting the 150 x 70 signature and the Customer Signature line. I have removed this line and all sorted now.

    Thanks for your help. Much appreciated.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.