none
AddOLEObject in a table in Word RRS feed

  • Question

  • Hi,

    I am calling a dll (written in C#) from an external program to insert an image object in word's table's column. I use following code:

    object image = iImageFilePath;
    object linkToFile = false;
    object displayAsIcon = false;
    table.Cell(m_RowIndex, m_ColumnIndex).Range.InlineShapes.AddOLEObject(ref MISSING, ref image, ref linkToFile, ref displayAsIcon, ref MISSING, ref MISSING, ref MISSING, ref MISSING);
    

    On calling above function, the program hangs, the word hangs. When I debug the dll, no exception is registered.

    Any idea what I am doing wrong?

    Regards

    Ricky

    • Moved by Jason Dot Wang Thursday, October 18, 2012 9:20 AM This thread is about Word for Developers (From:Visual C# General)
    Wednesday, October 17, 2012 9:09 AM

Answers

  • Hi Ricky

    Well, you can change the height/width of the image you insert if the size is the question/problem. But I notice that the code you show us does NOT insert as an icon - you've set that parameter to false.

    It may well work manually, but when you use the Word UI you have the Word UI application interface invening on your behalf. This isn't the case when you use the API. I've never had a lot of luck with this method and have always inserted the equivalent field code (which in the background invokes the Word application UI for the OLE management) instead.

    But, even if you can get it to work on your machine there's no guarantee that it's going to work when you distribute the software and it's intalled on other machines. Unless you can be absolutely certain that the software acting as the OLE Server on your machine is installed on every machine that will run your solution, and that it's also registered to be the OLE Server on those machines and not something else.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, October 19, 2012 11:49 AM
    Moderator

All replies

  • Hi ricky_CSharp,

      Welcome to MSDN Forum Support.

      We're doing research on this issue. It might take some time before we get back to you.

      Sincerely,

      Jason Wang



    Jason Wang [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, October 18, 2012 4:44 AM
  • Hi Ricky,

    Thanks for posting in the MSDN Forum.

    According to your description, I mostly cannot reproduce this issue in terms of your breif code. Could you share more snippets in the forum?

    As you post, there is exception when debuging. So it's based on my experience that it should be the application problem, even may be caused by the image reason. Please clarify more information, including the VS version, Word version. It will be beneficial for us to do further research.

    Best Regards,


    Leo_Gao [MSFT]
    MSDN Community Support | Feedback to us

    Friday, October 19, 2012 3:05 AM
    Moderator
  • Hello Leo,

    Thanks for replying.

    /*************************************************/

    The VS version I am using is: Microsoft Visual Studio 2008
    Version 9.0.21022.8 RTM
    Microsoft .NET Framework
    Version 3.5 SP1

    Installed Edition: Professional

    Microsoft Visual C# 2008   91605-270-2456972-60700
    Microsoft Visual C# 2008

    Microsoft Visual Studio 2008 Tools for Office   91605-270-2456972-60700
    Microsoft Visual Studio 2008 Tools for Office

    The word installed version is (using in compatibility mode):

    MS Word 2010 (14.0.6024.1000), SP1 MSO (14.0.6023.1000)

    /*************************************************/

    Here is the code:

    /*********Init*********************/
    Word.Document m_ReportDoc; m_ReportApp = new Word.ApplicationClass();
    m_ReportDoc = m_ReportApp.Documents.Add(ref MISSING, ref MISSING, ref MISSING, ref MISSING); 
    Word.Table table = m_ReportDoc.Tables[resultTable];
    object image = iImageFilePath; //image is .jpg or .png. Have tried both. Path is correct. InlineShape.AddPiture works with same path.
    object linkToFile = false;
    object displayAsIcon = false;
    object range = table.Cell(m_RowIndex, m_ColumnIndex).Range;
    
    try
    {
    	/*********Method 1**********/
            table.Cell(m_RowIndex,  m_ColumnIndex).InlineShapes.AddOLEObject(ref MISSING, ref image, ref linkToFile, ref displayAsIcon, ref MISSING, ref MISSING, ref MISSING, ref MISSING);
            /*********Method 2**********/
            m_ReportDoc.InlineShapes.AddOLEObject(ref MISSING, ref image, ref linkToFile, ref displayAsIcon, ref MISSING, ref MISSING, ref MISSING, ref range);
    }                
    catch (Exception e)
    {
            return e.ToString();
    }
    
    I could able to catch exception in Method 1 & 2. I must close word forcefully and then it works. I do not know the reason for exception. May be because the word doc is not available anymore:

    InteropServices.COMException (0x800706BE): Der Remoteprozeduraufruf ist fehlgeschlagen. (Ausnahme von HRESULT: 0x800706BE). ErrorCode=-2147023170. Inner Exception

    I think, in English, it should be: The remote method call is failed.


    • Edited by ricky_CSharp Friday, October 19, 2012 8:22 AM Added code snippet
    Friday, October 19, 2012 8:13 AM
  • Hi Ricky

    The method you're using inserts an embedded COM OLE object, which is usually not the preferable method for graphics. Instead, you should look at the InlineShapes.AddPicture method.

    An OLE object is a dynamic entity: the Word application acts as an "OLE client" and an "OLE server" application, appropriately entered in the Windows Registry, is required for managing the content of the embedded object. This technique is more appropriate for an Excel worksheet, for example, than a static image.

    The difference between the behavior you see and that Leo reports certainly depends from differing machine configurations. Whatever OLE-capable software registered on your machine as supporting OLE manipulation of graphics is likely different from that on Leo's machine. In your case, the application functioning as OLE Server is in conflict with the programmatic creation of the embedded object (perhaps a dialog box is waiting for user input). What's more, even if you get it to work it's not said that the machines on which your solution is eventually installed will have the same configuration, which could result in other difficulties.

    So, using the AddPicture method is preferred.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, October 19, 2012 10:43 AM
    Moderator
  • Hello Cindy,

    I would have used AddPicture but the issue is that add picture adds the image completely and not as a small icon. Since I am adding it in a table cell, it makes my cell appear too big which is not an optimal solution for us.

    Regards

    Ricky

    _______________________

    Edit: Manually it is possible to embed an image in word. I have tried with Insert --> Object, Create from file where I give path to my graphic file. It functions excactly as I desire.

    • Edited by ricky_CSharp Friday, October 19, 2012 11:27 AM Info: Manually adding object in word
    Friday, October 19, 2012 11:19 AM
  • Hi Ricky

    Well, you can change the height/width of the image you insert if the size is the question/problem. But I notice that the code you show us does NOT insert as an icon - you've set that parameter to false.

    It may well work manually, but when you use the Word UI you have the Word UI application interface invening on your behalf. This isn't the case when you use the API. I've never had a lot of luck with this method and have always inserted the equivalent field code (which in the background invokes the Word application UI for the OLE management) instead.

    But, even if you can get it to work on your machine there's no guarantee that it's going to work when you distribute the software and it's intalled on other machines. Unless you can be absolutely certain that the software acting as the OLE Server on your machine is installed on every machine that will run your solution, and that it's also registered to be the OLE Server on those machines and not something else.


    Cindy Meister, VSTO/Word MVP, my blog

    Friday, October 19, 2012 11:49 AM
    Moderator