none
Insert image into the default Word document RRS feed

  • Question

  •  

    Hi,

    I am very new to programming. I am using VS2008 to creat a Word-AddIn using C#.

    C#.

     I want to insert a picture into the initial Word document (The document that appears automatically when word 2007 is opened). The picture is in a file on my harddrive.

     

    I can insert the picture into a NEW Word document  but not into the initial word document. I use the following code to put a picture into a NEW Word document named x:

     

    DocumentClass x = new DocumentClass();

    string fileName = "c:\\testimage.jpg"; //the picture file to be inserted

    Object oMissed = x.Paragraphs[1].Range; //the position you want to insert

    Object oLinkToFile = false; //default

    Object oSaveWithDocument = true;//default

    x.InlineShapes.AddPicture(fileName, ref oLinkToFile, ref oSaveWithDocument, ref oMissed);

     

     

     

    How can do the same thing without creating a new document x. I want to insert the picture into the initial Word document.

     

    Thanks

    keith

    kbrown_eng@yahoo.com

    Thursday, September 25, 2008 3:17 PM

Answers

  •  toncul wrote:

    Thank you for your help. I want to place the picture to the right of some text in a Word Document. I want the "Text Wrapping" style to be "Through". Can you tell me how to control the placing of the picture and the Wrapping Style??

    If you want to use text wrapping then you have a Shape object, not an InlineShape object. For example:

     

    Code Snippet

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Linq;

    using System.Text;

    using System.Windows.Forms;

    using Word = Microsoft.Office.Interop.Word;

     

    namespace WordAddIn3

    {

    public partial class Form1 : Form

    {

    public Form1()

    {

    InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)

    {

    Word.Application wdApp = Globals.ThisAddIn.Application;

    Word.Document doc = wdApp.ActiveDocument;

    string fileName = "c:\\testimage.jpg"; //the picture file to be inserted

    Object oMissed = doc.Paragraphs[1].Range; //the position you want to insert

    Object oLinkToFile = false; //default

    Object oSaveWithDocument = true;//default

     

    Word.Shape shp = doc.Shapes.AddPicture(filename, ref missing, ref missing, ref 20f, ref 20f, missing, missing, oMissed);

    shp.WrapFormat.Type = Word.WdWrapType.wdWrapThrough;

    shp.RelativeHorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin;

    shp.Left = Word.WdShapePosition.wdShapeRight;

    }

    }

    }

     

     

    Friday, September 26, 2008 4:05 PM
    Moderator

All replies

  • Please show us the entire code in your Add-in, so that we know where we're starting from. also tell us the version of Word involved.

     

    FWIW, you should not use DocumentClass when creating a new document. You should use the Documents.Add method of the Application object.

     

    Thursday, September 25, 2008 4:05 PM
    Moderator
  •  

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Linq;

    using System.Text;

    using System.Windows.Forms;

    using Microsoft.Office.Interop.Word;

     

    namespace WordAddIn3

    {

    public partial class Form1 : Form

    {

    public Form1()

    {

    InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)

    {

    DocumentClass x = new DocumentClass();

    string fileName = "c:\\testimage.jpg"; //the picture file to be inserted

    Object oMissed = x.Paragraphs[1].Range; //the position you want to insert

    Object oLinkToFile = false; //default

    Object oSaveWithDocument = true;//default

     

    x.InlineShapes.AddPicture(fileName, ref oLinkToFile, ref oSaveWithDocument, ref oMissed);

     

    }

    }

    }

    Thursday, September 25, 2008 4:15 PM
  • thank you for the additional information. Without it, we'd have had no idea you're trying to do this from a Windows Form.

     

    I've made a couple of changes to your code, below. Note the use of the Globals keyword to access the Add-in and its objects. Through that, I can pick up the application in which the add-in is running. ActiveDocument is the current document. If (1) Word starts and opens a new document (which is the default, but not guaranteed) and (2) the form is displayed before any other document is opened or created - then this will write to that new, default document.

     

    Please also note that it's often "safer" to use missing rather than set a value for arguments if you want to use the default, rather than specifying a particular behavior.

     

     toncul wrote:

     

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Linq;

    using System.Text;

    using System.Windows.Forms;

    using Word = Microsoft.Office.Interop.Word;

     

    namespace WordAddIn3

    {

    public partial class Form1 : Form

    {

    public Form1()

    {

    InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)

    {

    Word.Application wdApp = Globals.ThisAddIn.Application;

    Word.Document doc = wdApp.ActiveDocument;

    string fileName = "c:\\testimage.jpg"; //the picture file to be inserted

    Object oMissed = doc.Paragraphs[1].Range; //the position you want to insert

    Object oLinkToFile = false; //default

    Object oSaveWithDocument = true;//default

     

    doc.InlineShapes.AddPicture(fileName, ref oLinkToFile, ref oSaveWithDocument, ref oMissed);

     

    }

    }

    }

    Thursday, September 25, 2008 4:36 PM
    Moderator
  • Thank you for your help. I want to place the picture to the right of some text in a Word Document. I want the "Text Wrapping" style to be "Through". Can you tell me how to control the placing of the picture and the Wrapping Style??

    Friday, September 26, 2008 1:49 PM
  •  toncul wrote:

    Thank you for your help. I want to place the picture to the right of some text in a Word Document. I want the "Text Wrapping" style to be "Through". Can you tell me how to control the placing of the picture and the Wrapping Style??

    If you want to use text wrapping then you have a Shape object, not an InlineShape object. For example:

     

    Code Snippet

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Linq;

    using System.Text;

    using System.Windows.Forms;

    using Word = Microsoft.Office.Interop.Word;

     

    namespace WordAddIn3

    {

    public partial class Form1 : Form

    {

    public Form1()

    {

    InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)

    {

    Word.Application wdApp = Globals.ThisAddIn.Application;

    Word.Document doc = wdApp.ActiveDocument;

    string fileName = "c:\\testimage.jpg"; //the picture file to be inserted

    Object oMissed = doc.Paragraphs[1].Range; //the position you want to insert

    Object oLinkToFile = false; //default

    Object oSaveWithDocument = true;//default

     

    Word.Shape shp = doc.Shapes.AddPicture(filename, ref missing, ref missing, ref 20f, ref 20f, missing, missing, oMissed);

    shp.WrapFormat.Type = Word.WdWrapType.wdWrapThrough;

    shp.RelativeHorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin;

    shp.Left = Word.WdShapePosition.wdShapeRight;

    }

    }

    }

     

     

    Friday, September 26, 2008 4:05 PM
    Moderator
  •  

    when I run my program I am getting an a COM exception that says "Requested object is not availible"  and the exception occurs at the following line in the code:

     

    Word.Shape shp = doc.Shapes.AddPicture(fileName, ref oLinkToFile, ref oSaveWithDocument, ref oLeft, ref oTop, ref oWidth, ref oHeight, ref oanchors);

     

    the full code is below :

     

    Word.Application wdApp = Globals.ThisAddIn.Application;

    Word.Document doc = wdApp.ActiveDocument;

    string fileName = "c:\\testimage.jpg"; //the picture file to be inserted

    Object oMissed = doc.Paragraphs[1].Range; //the position you want to insert

    Object oLinkToFile = false; //default

    Object oSaveWithDocument = true;//default

    Object oLeft = 20;

    Object oTop = 20;

    Object oWidth = 70;

    Object oHeight = 70;

    Object oanchors = false;

     

    Word.Shape shp = doc.Shapes.AddPicture(fileName, ref oLinkToFile, ref oSaveWithDocument, ref oLeft, ref oTop, ref oWidth, ref oHeight, ref oanchors);

    shp.WrapFormat.Type = Word.WdWrapType.wdWrapThrough;

    shp.RelativeHorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin;

    shp.Left = (float) Word.WdShapePosition.wdShapeRight;

     

    Do you know why I get this error????

    Tuesday, September 30, 2008 12:27 PM
  • when I run my program I am getting an a COM exception that says "Requested object is not availible"  and the exception occurs at the following line in the code:

     

    Word.Shape shp = doc.Shapes.AddPicture(fileName, ref oLinkToFile, ref oSaveWithDocument, ref oLeft, ref oTop, ref oWidth, ref oHeight, ref oanchors);

     

    The first thing you'd need to do is correct the value you're passing for the last parameter. It expects a Word.Range object (or System.Type.Missing). you're trying to pass a boolean ("False"), which won't work. If you want the Shape linked to the paragraph range you have to pass oMissed from your code.

    Tuesday, September 30, 2008 4:01 PM
    Moderator