views:

275

answers:

2

Hello,

Is it possible to use the Open XML sdk to manipulate parts of document which is currently open in the ofice app (word/ppt). I know the easiest thing is to use VSTO, but its slow and would involve clipboard use to insert elements, the OXML sdk is direct and simpler.

If somebody could post some code sample that would be great.

Thanks in advance
Rakesh

A: 

Something like below:-

//include the namespace

using DocumentFormat.OpenXml.WordProcessing

//Open and manipulate temp.docx 

using (WordprocessingDocument myDoc = WordprocessingDocument.Open("temp.docx", true)) 
{
    //Access main part of document 
    MainDocumentPart mainPart = myDoc.MainDocumentPart; 

    //Add new comments part to document 
    mainPart.AddNewPart<WordprocessingCommentsPart>(); 

    //Delete Styles part within document 
    mainPart.DeletePart(mainPart.StyleDefinitionsPart); 

    //Iterate through all custom xml parts within document 
    foreach (CustomXmlPart customXmlPart in mainPart.CustomXmlParts) {
        //DO SOMETHING 
    }
}

Also, you could use LINQ to avoid foreach loops.

ydobonmai
Sorry, If I was not clear earlier. I want to manipulate the document that is currently open in Word. I would be creating a button in the ribbon which adds parts to the currently active document. This would mean the document may not be saved when running this action, hence, no file path.Is there a way to convert the activedocument object into a WordProcessingDocument objectThanks R
A: 

Apparently you cannot do this without Sharepoint.

As per Zeyad Jarabi/..

...you need a platform that understands how to take a shared lock (like SharePoint or SkyDrive). Without this concept then the application and the SDK can only take read or write locks, which prevents these two pieces of technology from accessing the same file.