Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the file name of a Word document without saving it from c# and automation II

I have asked here a question on how setting the filename of a Word document via automation without saving it. Thanks to Remou, I have received a nice way doing this via calling the FileSummaryInfo-Dialog and setting the Title-property.

However now I have the problem that the client wants to have document names with special chars in (point and underscore) and it seems to be a bug (or a feature) of word, that it cuts the title and only takes the chars before the first special char for building the file name! I have already googled a lot, however was not able to find a resolution for this problem. The problem is also noticed here (see under gotcha), however without a solution.

Has anybody another solution for setting the filename without saving, or a workaround/bugfix for the mentioned odd behavior?

like image 967
HCL Avatar asked Nov 29 '12 15:11

HCL


1 Answers

Try easyhook, since do not have Windows machine besides my hand these days. following is just the call flow (something like what i did years ago, changed a software's socket bind port to different one by Detours)

About Hook the CreateFileW:

The example in the easyhook's wiki is just what we want here.

CreateFileHook = LocalHook.Create(
                    LocalHook.GetProcAddress("kernel32.dll", "CreateFileW"),
                    new DCreateFile(CreateFile_Hooked),
                    this);

In the CreateFile_Hooked you can change the parameter InFileName, then call real CreateFileW

static IntPtr CreateFile_Hooked(
    String InFileName,
    UInt32 InDesiredAccess,
    UInt32 InShareMode,
    IntPtr InSecurityAttributes,
    UInt32 InCreationDisposition,
    UInt32 InFlagsAndAttributes,
    IntPtr InTemplateFile)
{
    // FIGURE OUT THE FILE NAME THAT YOU WANT HERE
    // IF the InFileName is not your Document name "My.doc", then call orignal CreateFile
    // with all the parameter unchanged.

    // call original API...
    return CreateFile(
        YOUR_CHANGED_FILE_NAME_HERE,
        InDesiredAccess,
        InShareMode,
        InSecurityAttributes,
        InCreationDisposition,
        InFlagsAndAttributes,
        InTemplateFile);
}

Call flow:

After you changed the title to "My_Document_2012_11_29", then hook the CreateFileW of Word process. For example when the InFileName is "My.doc", then you should change it to "My_Document_2012_11_29".

Because this is done in the Word process, so the Detoured function do not know "My.doc" is mapping to "My_Document_2012_11_29". There is lot ways to get this mapping info, one is save this mapping info to a known file in your app, and read the file in the Detoured function.

like image 87
whunmr Avatar answered Sep 30 '22 14:09

whunmr