1

I'm still not much experienced with C#, and I'm having trouble reading a docx file. It contains a music file embedded and I don't know how to open the .docx file and play that music. I want to play this track. I don't have other options because there are like 360 similar files like this which I need to process I've tried to search but couldn't find anything. Here is the picture

EDIT: I've managed to find the audio file here, its an oleobject type. Now how do I save this to my disk?

public class docParser
{
    public static void openDocx(string filepath)
    {
        String docText = readDocx(@filepath);
        Console.WriteLine(docText);
    }

    public static string readDocx(string filepath)
    {
       using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(filepath, false))
       {
            OpenXmlPart oleobject; 
            oleobject = wordDocument.MainDocumentPart.GetPartById("docRId2"); //I need to save this
       }
    }
}

And while using it, I write

    public Form1()
    {
        docParser.openDocx("d:\\sample.docx");
    }
StrikerZ
  • 21
  • 4
  • Does this answer your question? [Parse Word (.doc /.docx) document](https://stackoverflow.com/questions/31376177/parse-word-doc-docx-document) – Matt Evans Jul 31 '20 at 14:17
  • Reverse the process done here: https://stackoverflow.com/a/3322259/578411 I expect the used classes and overall approach are the same for reading. – rene Jul 31 '20 at 14:20
  • How are you reading the docx file? OpenXML SDK? Something else? *(Please tag your question with whatever it is)* What code have you written so far (that you are having trouble with)? If you are using OpenXML SDK, here's a hint. Create a Docx file that doesn't have the embedded music file in it. Copy the docx file, and add the embedded music to the copy. Now download the OpenXML SDK Productivity Tool from the Microsoft site. Open the tool and use it to Diff the two files. You should see how the embedding is stored. – Flydog57 Jul 31 '20 at 14:21
  • I haven't written any code yet because I've no idea what to do, this situation is kinda new to me and I'm not that experienced with file management yet. I've a set of 100 files and I need to process the data in it, text and all seem to be okay to process but the problem is embeds. I'm trying to read the article mentioned above by rene to find the solution to it. – StrikerZ Jul 31 '20 at 14:24
  • You mention that "text and all seem to be okay." By what measure? This sort of implies that you have attempted some of the solution which, if that is the case, you would do well to share your attempt. Nobody knows what libraries or anything you are using, and if you have literally done NOTHING, then nobody can or should help you. There is a whole lot of internet out there for "getting started." – BryanOfEarth Jul 31 '20 at 15:07
  • An alternative is to rename the docx file to .zip, open it with your favorite Zip tool and then copy the contenr out of the `word\embeddings` folder or use a word macro. https://superuser.com/questions/767095/how-can-i-search-for-inserted-files-in-a-microsoft-word-document/767125#767125 – rene Jul 31 '20 at 15:35
  • Sorry for not replying soon, I slept. I updated my post with the code I've written so far since yesterday, I'm using OpenXML SDK like suggested. The code is in the post. – StrikerZ Aug 01 '20 at 03:21
  • Update: I managed to find the audio file as oleobject, and I added the code above, please guide me further on how to save it. – StrikerZ Aug 01 '20 at 04:51

1 Answers1

0

Answering my own question, this code helped me do the thing I wanted.

public static void extractObject(string filepath)
    {
       using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(filepath, false))
       {
            Aspose.Words.Document doc = new Aspose.Words.Document();
            doc = new Aspose.Words.Document(filepath);
            NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);
            int i = 0;
            foreach (Shape shape in shapes)
            {
                if (shape.OleFormat != null)
                {
                    shape.OleFormat.Save(String.Format("D:\\" + "/out_{0}.{1}", i, "wav"));
                    i++;
                }
            }
        }
    }
StrikerZ
  • 21
  • 4