7

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?

Community
  • 1
  • 1
HCL
  • 36,053
  • 27
  • 163
  • 213
  • What do you mean point and underscore? – paparazzo Nov 29 '12 at 15:16
  • @Blam: "." and "_". For example "My_Document_2012_11_29" or "My_Document_2012.11.29". Both of these documents will be named "My.doc". You can test the behaviour also without Automation: Create a new blank document, set the documents title (in the documents property dialog) to one of the above titles and then hit ctrl-S. – HCL Nov 29 '12 at 15:19
  • 2
    This is built-in Word behavior, you cannot hack it. Note that any punctuation character will cut it short, including a dash, slash or comma. But not a space. You are just seeing Word trying to synthesize a short filename from a long title. Also covered here: http://www.microsoft-word-answers.com/microsoft/Word-VBA/33441907/default-suggested-filename.aspx – Hans Passant Dec 03 '12 at 14:32
  • @Hans Passant: This is sad news. However, I thought it would be like that, but hope dies last... – HCL Dec 03 '12 at 14:38
  • Also note that if you don't set the title then it tries to synthesize a filename from the first paragraph in the document. Exact same behavior though. – Hans Passant Dec 03 '12 at 14:41

1 Answers1

5

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.

whunmr
  • 2,435
  • 2
  • 22
  • 35
  • +1 Thank you for your creative solution. However if I understand the way of your solution right, the name will be changed after the client saves the file (clicks ok in the save file dialog). This means that in the save file dialog, the short title (My.doc) is shown, not the title? However it is important, that the filename is shown correctly in the save file dialog. But again: nice solution - thanks! – HCL Dec 06 '12 at 16:41
  • @HCL, yes, you are right. maybe we need hook the SetWindowText method here. – whunmr Dec 07 '12 at 00:54