Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get subject & title from a Word document (without opening it)?

I would like to read the title and subject fields from a Word document, but would rather not have the overhead of firing up Word to do it.

If, in Windows Explorer, I display the title and subject columns, and then navigate to a folder that has Word documents in it, then this information is displayed. What mechanism is being used to do this (aside from Shell extensions) because its fast (but I don't know if you actually need Word installed for this to work), so I'm guessing its not firing up Word and opening each document.

I've found a link to Dsofile.dll, which I presume I could use, but does this work for .doc and .docx files and is it the only way ?

like image 987
Black Light Avatar asked Nov 16 '25 05:11

Black Light


1 Answers

Well... as one might assume that the time of the ".doc" file is passing, here is one way to get the subject and title from a ".docx" file (or ".xlsx" file for that matter).

using System;
using System.IO;
using System.IO.Packaging; // Assembly WindowsBase.dll

namespace ConsoleApplication16
{
  class Program
  {
     static void Main(string[] args)
     {
        String path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
        String file = Path.Combine(path, "Doc1.docx");

        Package docx = Package.Open(file, FileMode.Open, FileAccess.Read);
        String subject = docx.PackageProperties.Subject;
        String title = docx.PackageProperties.Title;
        docx.Close();
     }
  }
}

I hope this is useful to someone.

like image 154
Black Light Avatar answered Nov 18 '25 18:11

Black Light



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!