Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

just tried to embed a word document (test0.docx) in another word document (test1.docx) as linked object. So far so easy. Now I took this document (test1.docx) and embedded it in the next one (test2.docx). Issue here is, that if I change test0.docx, save it and close it test2.docx isn't updated with the new text.

Is there a chance to work it out programatically?

Thx in advance

Olli

Got an idea which should work... unfortunately it dose not... Anybody an idea?

C#
public static void DoSomething(string docPath)
{
  Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
  try
  {
    Document doc = wordApp.Documents.Open(docPath, ReadOnly: false, Visible: false, AddToRecentFiles: false);
    doc.Activate();
    for(int i = doc.InlineShapes.Count; i > 0; i--)
    {
      if (doc.InlineShapes[i].Type == WdInlineShapeType.wdInlineShapeLinkedOLEObject)
      {
        if (doc.InlineShapes[i].LinkFormat.SourceName.Split('.')[doc.InlineShapes[i].LinkFormat.SourceName.Split('.').Length - 1] == "docx")
          DoSomething(doc.InlineShapes[i].LinkFormat.SourceFullName);
        doc.InlineShapes[i].LinkFormat.Update();
        doc.Save();
      }
    }
    (doc as _Document).Close();
    (wordApp as _Application).Quit();
  }
  catch { }
  System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
}
Posted
Updated 11-Jul-12 3:22am
v2
Comments
ZurdoDev 5-Jul-12 11:15am    
Have you tried recording a macro to do what you want?
hornet_79 6-Jul-12 2:31am    
Sure! Proble is, that if you try to open te first embedded linked object office starts a new instance an everything that happens here isn't recorded by the macro...
Or so it seems to me. I'm not that crack with office automation apparently...

1 solution

Solved it. Not the solution I would like most but at least it works. Above code just works fine if you use ".doc" instead of ".docx". So much for microsofts wonderful new xml based file formats... at least in that case ;-)
It seems, that the OLEObjects in docx are not referenced. Instead they are saved as ".emf"-pictures inside the xml. Maybe possible solutions for ".docx" would be to edit the xml based ".docx" directly or open the InlineShape in the source document as word document programatically.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900