Search code examples
c#vstoword-interop

adding image to word using word-interop


i am trying to insert an image with the hyper link the below is my code

            Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();
            WordApp.Documents.Add();

            WordApp.Visible = true;
            Microsoft.Office.Interop.Word.Document doc = WordApp.ActiveDocument;
            Microsoft.Office.Interop.Word.Range drange = doc.Range();
            Microsoft.Office.Interop.Word.InlineShape picture = 
            drange.InlineShapes.AddPicture("c:\\logo.png", Type.Missing, Type.Missing, Type.Missing);

            // noew add the hyperlink to object of inlineshape
            drange.Hyperlinks.Add(picture, "http:\\www.google.com", Type.Missing, Type.Missing, Type.Missing, Type.Missing);

but when i run the project i get an error enter image description here does any one know why this happens or how i can go about fixing it


Solution

  • Microsoft.Office.Interop.Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;                            
    Microsoft.Office.Interop.Word.Application objApplication = Globals.ThisAddIn.Application;
    Microsoft.Office.Interop.Word.Selection objSelection = objApplication.Selection;
    Microsoft.Office.Interop.Word.Paragraphs p = objSelection.Paragraphs;
    Microsoft.Office.Interop.Word.Range objRange = objSelection.Range;
    
    Microsoft.Office.Interop.Word.InlineShape ils = objRange.InlineShapes.AddPicture(@"C:\..\image.PNG");
    float scaledWidth = ils.Width;
    float scaledHeight = ils.Height;