none
opening/printing options RRS feed

  • Question

  • Hello,

    I'm trying to print a word document with C#.

    I think what I need to use is

    using Word = Microsoft.Office.Interop.Word;

    Word.Application wdApp = new Word.Application();
    wdApp.Visible = false;
    Word.Document docToPrint = wdApp.Documents.Open(file, ConfirmConversions: true, readOnly: true, AddToRecentFiles:false, Revert:true);
    docToPrint.PrintOut();
    docToPrint.Close();
    wdApp.Quit();

    I was wondering what are the default for Documents.Open, I can't find them on google.


    Also, regarding the PrintOut method, I would have wanted to display the usual print dialog, so that the user can choose the options himself (and then pass them to the method).
    How can I do that ?

    It seems that I need to write the path of the 'file' variable with slashes and not backslashes as usually done with Windows. Why is that ?

    Finally, I have a warning that says that there is an ambiguity between the method 'Microsoft.Office.Interop.Word._Document.Close(ref object, ref object, ref object)' and the element 'Microsoft.Office.Interop.Word.DocumentEvents2_Event.Close' which is not a method. (same warning for quit). How can I fix that ?

    Thanks

     

    Friday, November 26, 2010 3:47 PM

Answers

  • Hi Jenny

    I'm not sure that ALL the defaults for the options are documented anywhere. Some of them are listed in the VBA Help for Word (Alt+F11 in the Word UI to open the VBA environment, then F1 to open the Help).

    FWIW if you pass System.Type.Missing for all optional arguments Word will use the defaults - no need to define any options you won't use. Unless you need something special, that's the best practice.

    wdApp.Dialogs is a collection of available built-in dialog boxes you can display/show. For example: wdApp.Dialogs[Word.WdWordDialog.wdDialogFilePrint].Show();

    If you use the Show method, Word will execute the printing itself as long as the user doesn't cancel the dialog box. The Display method will show the dialog box and the user can set the options, but it won't execute any action (in this case, won't print). Then you'd have to take care of printing yourself. As these dialog boxes are all modal, code execution will be synchronous (it will wait until the dialog box has been dismissed).

    You may certainly use backslashes to define the file path. In some respects, Word will accept both forward and backslashes. The option for forward slashes was introduced to make Word more "web compatible".

    You can ignore the ambiguity warning for the Close method, as it takes precedence over the event. If you wanted to use the event, you'd have to fully qualify the "namespace" it comes from. That there's a conflict has to do with how the COM type libraries are bult on which the PIAs base. They actually combine multiple classes into a single interface, and some of the methods in those classes have the same name. But if you don't want to see it:

    ((Microsoft.Office.Interop.Word.

    _Document) app.ActiveDocument).Close(ref missing, ref missing, ref missing);


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bessie Zhao Friday, December 3, 2010 11:12 AM
    Saturday, November 27, 2010 9:10 AM
    Moderator
  • I was chasing the defaults and saw Cindy's post. I agree about the documentation, and also about focussing on the parameters that you need.

    Part of the problem is that in some cases the official documentation is wrong and has been wrong for a long time - I don't have a full replacement text but here are a few things that are not spelled out. I do not know how much of this is relevant if you are working from C# / via Interop.

     a. It may be significant that the called method can detect whether or not each optional parameter is present, and can also detect the incoming data type. That means that in theory, passing no value could have a different effect from passing one of the allowed values, even the default value.

     b. PasswordDocument is described as an optional Variant, but AFAICS if you supply the parameter in VBA, it has to have a specific data type - e.g. if you provide a string, or a variant set to a string, it is OK. If you supply an unassigned Variant, an error is generated. I would guess that applies to all four password parameters. I think the "effective default" is "" for all of them, but am not certain.

     c. Revert can be True or False as the documentation states. But it can also be 0, 1 or 2. If it is 0 or 1 it /appears/ to behave the same way as if it was False. If it is 2, a prompt is displayed asking the user whether or not to Revert. Even though in VBA, "True" is normally equivalent to -1, if you actually pass an integer -1 as the value, an error is generated. I think the default is False.

     d. AddToRecentFiles can be True or False as the documentation states. It can also be -1, 0 or 1. These behave like True, False and True as you might expect. If you do not specify the parameter at all, Word uses "False" (I think). However, if you provide an unassigned Variant, Word behaves as if the value were "True".

    ...and so on.


    Peter Jamieson
    • Marked as answer by Bessie Zhao Friday, December 3, 2010 11:12 AM
    Saturday, November 27, 2010 10:27 AM
  • > But if I want to leave the default value, I must know what it is, otherwise it's stupid to leave it to the default value.

    Well, that is your point of view. But another one is that what you have is akin to a C# method with a (large) number of different signatures. If you pick the signature where you just provide a file name, because that's all you need, you don't actually need to look through all the other versions of the method and understand them all, because you are in effect relying on the author of the called routine to do sensible things.

    I certainly agree that it would be better if all this stuff had been more comprehensively documented in the first place, and it would be pretty helpful if all the stuff was - for example - on a wiki where it could be progressively improved by anyone willing to contribute. Perhaps there is one already (cf. pInvoke). Other than that, I really do not know how you would get detailed information on these matters from the developers. When I really need to know, I do the necessary experiments. It's tedious and potentially error-prone, but it does leave you feeling that you probably haven't missed anything important.

     


    Peter Jamieson
    • Proposed as answer by Amy LiModerator Thursday, December 2, 2010 8:20 AM
    • Marked as answer by Bessie Zhao Friday, December 3, 2010 11:12 AM
    Saturday, November 27, 2010 5:02 PM

All replies

  • Hi Jenny

    I'm not sure that ALL the defaults for the options are documented anywhere. Some of them are listed in the VBA Help for Word (Alt+F11 in the Word UI to open the VBA environment, then F1 to open the Help).

    FWIW if you pass System.Type.Missing for all optional arguments Word will use the defaults - no need to define any options you won't use. Unless you need something special, that's the best practice.

    wdApp.Dialogs is a collection of available built-in dialog boxes you can display/show. For example: wdApp.Dialogs[Word.WdWordDialog.wdDialogFilePrint].Show();

    If you use the Show method, Word will execute the printing itself as long as the user doesn't cancel the dialog box. The Display method will show the dialog box and the user can set the options, but it won't execute any action (in this case, won't print). Then you'd have to take care of printing yourself. As these dialog boxes are all modal, code execution will be synchronous (it will wait until the dialog box has been dismissed).

    You may certainly use backslashes to define the file path. In some respects, Word will accept both forward and backslashes. The option for forward slashes was introduced to make Word more "web compatible".

    You can ignore the ambiguity warning for the Close method, as it takes precedence over the event. If you wanted to use the event, you'd have to fully qualify the "namespace" it comes from. That there's a conflict has to do with how the COM type libraries are bult on which the PIAs base. They actually combine multiple classes into a single interface, and some of the methods in those classes have the same name. But if you don't want to see it:

    ((Microsoft.Office.Interop.Word.

    _Document) app.ActiveDocument).Close(ref missing, ref missing, ref missing);


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bessie Zhao Friday, December 3, 2010 11:12 AM
    Saturday, November 27, 2010 9:10 AM
    Moderator
  • I was chasing the defaults and saw Cindy's post. I agree about the documentation, and also about focussing on the parameters that you need.

    Part of the problem is that in some cases the official documentation is wrong and has been wrong for a long time - I don't have a full replacement text but here are a few things that are not spelled out. I do not know how much of this is relevant if you are working from C# / via Interop.

     a. It may be significant that the called method can detect whether or not each optional parameter is present, and can also detect the incoming data type. That means that in theory, passing no value could have a different effect from passing one of the allowed values, even the default value.

     b. PasswordDocument is described as an optional Variant, but AFAICS if you supply the parameter in VBA, it has to have a specific data type - e.g. if you provide a string, or a variant set to a string, it is OK. If you supply an unassigned Variant, an error is generated. I would guess that applies to all four password parameters. I think the "effective default" is "" for all of them, but am not certain.

     c. Revert can be True or False as the documentation states. But it can also be 0, 1 or 2. If it is 0 or 1 it /appears/ to behave the same way as if it was False. If it is 2, a prompt is displayed asking the user whether or not to Revert. Even though in VBA, "True" is normally equivalent to -1, if you actually pass an integer -1 as the value, an error is generated. I think the default is False.

     d. AddToRecentFiles can be True or False as the documentation states. It can also be -1, 0 or 1. These behave like True, False and True as you might expect. If you do not specify the parameter at all, Word uses "False" (I think). However, if you provide an unassigned Variant, Word behaves as if the value were "True".

    ...and so on.


    Peter Jamieson
    • Marked as answer by Bessie Zhao Friday, December 3, 2010 11:12 AM
    Saturday, November 27, 2010 10:27 AM
  • Hi Peter

    <<a. It may be significant that the called method can detect whether or not each optional parameter is present, and can also detect the incoming data type. That means that in theory, passing no value could have a different effect from passing one of the allowed values, even the default value.>>

    With C# (pre-.NET 4.0), there's no such thing as an optional parameter. Something must be passed in the C# code. As you say, it can make a difference as to whether something is passed, or not. That's why it's very important the C# programmer knows about passing something as "type missing" instead of passing an object value to each optional parameter. In the PIA/COM interface, the PIA knows to interpret this as the VBA equivalent of not specifying the parameter at all.


    Cindy Meister, VSTO/Word MVP
    Saturday, November 27, 2010 12:39 PM
    Moderator
  • Yes I agree.

    As for the backslashes, I wonder whether the OP's problem is that \ is used as an escape in C# string literals and needs to be doubled up.


    Peter Jamieson
    Saturday, November 27, 2010 2:46 PM
  • Thanks for the answers to the other questions.
    Regarding the default values, I'm kinda lost. It seems that you both say that I should only focus on the options that I need, and either leave the others (.NET4) or give them type.Missing. But if I want to leave the default value, I must know what it is, otherwise it's stupid to leave it to the default value. Why wouldn't this be documented ? On the worst case scenario, someone could test all the possibilities for each parameter and make his own documentation, but this should have been done by Microsoft. Isn't there a way to contact the developers and ask them to read their code so as to see the default values ?
    Saturday, November 27, 2010 4:07 PM
  • > But if I want to leave the default value, I must know what it is, otherwise it's stupid to leave it to the default value.

    Well, that is your point of view. But another one is that what you have is akin to a C# method with a (large) number of different signatures. If you pick the signature where you just provide a file name, because that's all you need, you don't actually need to look through all the other versions of the method and understand them all, because you are in effect relying on the author of the called routine to do sensible things.

    I certainly agree that it would be better if all this stuff had been more comprehensively documented in the first place, and it would be pretty helpful if all the stuff was - for example - on a wiki where it could be progressively improved by anyone willing to contribute. Perhaps there is one already (cf. pInvoke). Other than that, I really do not know how you would get detailed information on these matters from the developers. When I really need to know, I do the necessary experiments. It's tedious and potentially error-prone, but it does leave you feeling that you probably haven't missed anything important.

     


    Peter Jamieson
    • Proposed as answer by Amy LiModerator Thursday, December 2, 2010 8:20 AM
    • Marked as answer by Bessie Zhao Friday, December 3, 2010 11:12 AM
    Saturday, November 27, 2010 5:02 PM
  • Hi Jenny,

    I'm writing to check the status of the thread. Could you please let me know if you have solved the problem?

    Best Regards,
    Amy Li
    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Thursday, December 2, 2010 8:20 AM
    Moderator