none
How to insert image/Picture in Textbox using microsoft.office.interop.word RRS feed

  • Question

  • Hello,

            I want to insert image in text-box using microsoft.office.interop.word. For doing this I have created a simple console application. Through console application I am opening word document file and replacing some text inside the text box.

            So I am able to replace the text in text-box but unable to insert image in text-box. Is there any way to insert image in textbox. I am able to insert image in Table but I want to insert image in textbox.

             I have tried but unable to find the solution.

           Below is the code for same

          

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Office.Interop.Word;
    using System.Net;
    
    using System.Data.SqlClient;
    
    using System.Data;
    using System.Configuration;
    using System.Globalization;
    
    using System.Runtime.InteropServices;
    
    namespace PrintSticker
    {
    
        class Program
        {
    
            public static Microsoft.Office.Interop.Word.Document aDoc = null;
            public static Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
            public static object missing = System.Reflection.Missing.Value;
    
            private static void FindReplace(Microsoft.Office.Interop.Word.Document aDoc, Dictionary<string, string> lstPrintData)
            {
    
                try
                {
    
                    int counter = 0;
    
                    int intdctCount = 0;
                    for (int j = 0; j < lstPrintData.Count; j++)
                    {
                        // string Findtext = element.Key; ;
                        // string ReplaceText = element.Value;
                        if (intdctCount == lstPrintData.Count - 1)
                        {
                            break;
                        }
                        string Findtext = Convert.ToString(lstPrintData.Keys.ElementAt(intdctCount)); 
                        string ReplaceText = Convert.ToString(lstPrintData.Values.ElementAt(intdctCount));
    
                        var range = aDoc.Range();
                        range.Find.Execute(FindText: Findtext, Replace: WdReplace.wdReplaceAll, ReplaceWith: ReplaceText);
                        var shapes = aDoc.Shapes;
    
    
                        foreach (Microsoft.Office.Interop.Word.Shape shape in shapes)
                        {
                            string shapename = Convert.ToString(shape.Name);
                            string shapevalue = Convert.ToString(shape.TextFrame.TextRange.Text);
    
    
                            if (shapevalue != "\r")
                            {
                                var initialText = shape.TextFrame.TextRange.Text;
                                if (initialText == Findtext + "\r")
                                {
                                    var resultingText = initialText.Replace(Findtext, ReplaceText);
                                    shape.TextFrame.TextRange.Text = resultingText;
                                    if (initialText == Findtext + "\r")
                                    {
                                        intdctCount++;
                                        break;
                                    }
                                }
                                else if (initialText.Contains(Findtext))
                                {
                                ReagainReplace:
                                    var resultingText = initialText.Replace(Findtext, ReplaceText);
                                    shape.TextFrame.TextRange.Text = resultingText;
                                    if (initialText.Contains(Findtext))
                                    {
                                        if (intdctCount < lstPrintData.Count - 1)
                                        {
                                            intdctCount++;
                                            Findtext = Convert.ToString(lstPrintData.Keys.ElementAt(intdctCount)); 
                                            ReplaceText = Convert.ToString(lstPrintData.Values.ElementAt(intdctCount)); 
                                            
                                            initialText = resultingText;
                                            goto ReagainReplace;
                                            //break;
                                        }
                                        else
                                        {
                                            break;
                                        }
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                counter++;
                            }
                        }
                    }
    
    
                    Console.WriteLine(counter);
                }
                catch (Exception exc)
                {
                    Console.WriteLine(exc.Message);
    
                }
    
            }
    
            static void Main(string[] args)
            {
                try
                {
                    Console.WriteLine(DateTime.Now);
    
                    object filename = @"D:\USER DATA\Nitin\Sticker.docx";
    
                    if (File.Exists((string)filename))
                    {
    
                        object readOnly = false;
                        object isVisible = false;
                        // make visible Word application
                        wordApp.Visible = false;
                        // open word template
                        aDoc = wordApp.Documents.Open(ref filename, ref missing,
                                ref readOnly, ref missing,
                                ref missing, ref missing,
                                ref missing, ref missing,
                                ref missing, ref missing,
                                ref missing, ref isVisible,
                                ref missing, ref missing,
                                ref missing, ref missing);
                        aDoc.Activate();
    
                        Dictionary<string, string> dictionary = new Dictionary<string, string>();
    
    
                        dictionary.Add("#FullName", "Mr.Nitin Jagtap");
                        dictionary.Add("#UCID", "UCH-908890");
                        dictionary.Add("#Age", "26");
                        dictionary.Add("#Sex", "M");
                        dictionary.Add("#FullAddress ", "Matoshri Niwas,Rajmudra Housing Society");
                        dictionary.Add("#Contact", "91-9503808004");
                        dictionary.Add("#Date", "18/08/1989");
                        dictionary.Add("#DoctorName", "Dr.Shivajirao Bhosale");
                        dictionary.Add("#OPDID", "UC/OPD/1703/1367");
    
    
                        FindReplace(aDoc, dictionary);
    
                        object DocNoParam = Type.Missing;
                        object SaveToFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
                        object outputFileName;
    
                        outputFileName = @"D:\USER DATA\Nitin\Sticker.pdf";
    
                        // Save word template in pdf format
                        aDoc.SaveAs(ref outputFileName, ref SaveToFormat,
                                                ref DocNoParam, ref DocNoParam,
                                                ref DocNoParam, ref DocNoParam,
                                                ref DocNoParam, ref DocNoParam,
                                                ref DocNoParam, ref DocNoParam,
                                                ref DocNoParam, ref DocNoParam,
                                                ref DocNoParam, ref DocNoParam,
                                                ref DocNoParam, ref DocNoParam);
                    }
    
                }
                catch (Exception exc)
                {
    
                    Console.Write(exc.Message);
    
                }
                finally
                {
                    object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
                    ((_Document)aDoc).Close(ref saveChanges, ref missing, ref missing);
                    wordApp.Quit();
                    Marshal.ReleaseComObject(aDoc);
                    Marshal.ReleaseComObject(wordApp);
                    Console.WriteLine(DateTime.Now);
                }
            }
        }
    }

             I want output like below.

             Could you please help me to sort out this issue.

    Thanks,

    Nitin



    • Edited by NitinJagtap Tuesday, March 28, 2017 8:47 AM
    • Moved by CoolDadTx Tuesday, March 28, 2017 2:00 PM Office related
    Tuesday, March 28, 2017 8:42 AM

Answers

  • Hello Nitin,

    We could use FillFormat.UserPicture Method (Word) to insert a picture into a text box.

    E.g.

    shape.Fill.UserPicture("ImagePath");

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by NitinJagtap Wednesday, March 29, 2017 3:50 PM
    Wednesday, March 29, 2017 6:04 AM
    Moderator

All replies

  • Hello Nitin,

    We could use FillFormat.UserPicture Method (Word) to insert a picture into a text box.

    E.g.

    shape.Fill.UserPicture("ImagePath");

    Regards,

    Celeste


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by NitinJagtap Wednesday, March 29, 2017 3:50 PM
    Wednesday, March 29, 2017 6:04 AM
    Moderator
  • Thanks Celeste,

                          For you valuable reply.

    according to your solution

    shape.Fill.UserPicture("ImagePath");

    I am able to add image in text box perfectly.Thank you so much for your response.

    Thanks

    Nitin

    Wednesday, March 29, 2017 3:50 PM
  • Just another solution to insert image, link and formatted text to textbox. Anyone who would like to simultaneously insert rich elements in textbox and have a good layout could check the following link.

    http://www.dotnetfunda.com/articles/show/3160/csharp-insert-well-formatted-textbox-into-word


    • Edited by Ezreal93 Monday, April 17, 2017 1:59 AM
    Monday, April 17, 2017 1:58 AM