How to disable a directory entry in OLE file

Parth Gupta 100 Reputation points
2023-05-18T17:38:26.2+00:00

Hi,

I am trying to parse a .doc file (OLE file) (MS-97-2003) by reading it byte by byte in python. I am so far successful in extracting the directory structure of the file. Suppose i find a directory named "Macros" in the .doc file and i want to disable it. My idea is to overwrite the type of directory (at byteoffset 66 decimal from the start of directory entry) to '0' indicating an Unused directory. Howeer it did not seem to work for me.

Can you tell me, if i want to disable a directory entry (say Macros) without making the file corrupt, how can i do it.

Thanks.

Office Open Specifications
Office Open Specifications
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Open Specifications: Technical documents for protocols, computer languages, standards support, and data portability. The goal with Open Specifications is to help developers open new opportunities to interoperate with Windows, SQL, Office, and SharePoint.
119 questions
{count} votes

Accepted answer
  1. Tom Jebo 1,906 Reputation points Microsoft Employee
    2023-05-26T22:01:29.55+00:00

    Hi @Parth Gupta,

    I did a small test and was able to remove the Macros root storage by using the structured storage Windows API's. This appears to have removed the macros. Word will still show the names of macros that previously existed but when you attempt to edit them in the Developer window, it will report that they no longer exist.

    Here is the small snippet using the Windows API's to remove the storage:

        STGOPTIONS stgOptions = { 2, 0, 512, 0 };
        IStorage* pStg = 0;
        HRESULT hr = StgOpenStorageEx(L"<path>\test.doc",
            STGM_TRANSACTED | STGM_READWRITE | STGM_SHARE_EXCLUSIVE, STGFMT_DOCFILE, 0, & stgOptions, 0, IID_IStorage, (void **) & pStg);
    
        hr = pStg->DestroyElement(L"Macros");
    
        pStg->Commit(STGC_DEFAULT);
    
        pStg->Release();
    
    

    I don't have the equivalent code in Python to do this but I would recommend using a library that is designed to manipulate OLE compound files.

    Tom


0 additional answers

Sort by: Most helpful