none
print RRS feed

  • Question

  • hello,

     

    how can I print a word document?
    I've seen that I can use Microsoft.Office.Interop.Word._Document.PrintOut, and   System.Windows.Controls.PrintDialog.ShowDialog but how to link these 2?

    Thanks.

    Monday, January 10, 2011 1:51 PM

All replies

  • Hi Jenny

    Can you expand a bit on the solution that should do this? For example, is this a Console App, a Windows Form or something else? Locally or on a network? Version of Word?


    Cindy Meister, VSTO/Word MVP
    Monday, January 10, 2011 4:01 PM
    Moderator
  • well it is a gui (hence the showdialog part, where i ask the user which options to print with before actually printing), I'm using wpf/C#, it is locally. as for the version of word I'm not sure, I mean I want it to work for every version...
    Monday, January 10, 2011 4:10 PM
  • oh and I'd also like to be able to save the printing settings to use them later on on the code, if it is possible.
    Monday, January 10, 2011 4:32 PM
  • well it is a gui (hence the showdialog part, where i ask the user which options to print with before actually printing), I'm using wpf/C#, it is locally. as for the version of word I'm not sure, I mean I want it to work for every version...


    Hi Jenny

    So, in general terms what you need to do is:

    1. Store the information the user selects in the .NET dialog box in variables/objects.
    2. Analyze how this corresponds to the parameters of the PrintOut command
    3. Issue that command on the document object in the Word application, inserting the stored information

    I'm not sure exactly where the "hang up" is on your side in all of this. It's also not clear why you wouldn't be using Word's built-in Print dialog box?

    Perhaps the "hang-up" is that you're thinking there's a way to print the Word document without using the Word application?

    As this is C#, if you want to support "any version" of Word, then you either need to use late-binding or .NET 4.0 and embed the iterop types. You also need to know the exact method signature of the oldest version you want to support so that you don't try to use any features that were introduced in a later version (and thus wouldn't be supported by the older version).

    Does this give you a better idea where to start and where you're going?


    Cindy Meister, VSTO/Word MVP
    Monday, January 10, 2011 4:55 PM
    Moderator
  • I didn't say I can't use Word's built-in Print dialog box, I didn't know it existed that's all.

    All I want is to be able to print a .doc without opening Word, but with a dialog box that appears and asks the user what options to print with.
    So how would you do that?

    Thanks.

    Monday, January 10, 2011 5:08 PM
  • All I want is to be able to print a .doc without opening Word,...

    So how would you do that?


    Hi Jenny

    You can't. You MUST open Word in order to print a Word document. Even Windows (when you right-click and choose "Print" on a Word document) has to start Word in order to print a Word document.


    Cindy Meister, VSTO/Word MVP
    Monday, January 10, 2011 5:21 PM
    Moderator
  • We just probably have different definitions of what opening word means. What I mean is that I don't want it to show on the screen for the user to be bothered by it opening and closing. My question stands.

     

    Monday, January 10, 2011 5:44 PM
  • Hi Jenny

    Something like this (in pseudocode)? The Word application should not be visible unless you explicitly set its Visible property to true.

    using Word = Microsoft.Office.Interop.Word;

    //in a procedure
    Word.Application wdApp = new Word.Application();
    Word.Document wdDoc = wdApp.Documents.Open(//the document to open and other params);
    wdDoc.PrintOut(//params);
    wdDoc.Close();
    wdDoc = null;
    wdApp.Quit();
    wdApp = null;

    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.Collect();
    GC.WaitForPendingFinalizers();

     


    Cindy Meister, VSTO/Word MVP
    Monday, January 10, 2011 5:59 PM
    Moderator
  • Yes, that works but unfortunately it wasn't my question, I had actually already done that. Sorry for the mislead.

    My question is how to add to this code a dialog box to ask the user which options to print with.

     

    Thanks.

    Tuesday, January 11, 2011 2:23 AM
  • Can I get an answer to that then please? And again, sorry for having posed the wrong question.
    Tuesday, January 11, 2011 5:34 AM
  • Something like

    using Word = Microsoft.Office.Interop.Word;

    //in a procedure
    Word.Application wdApp = new Word.Application();
    Word.Document wdDoc = wdApp.Documents.Open(//the document to open and other params);
    wdApp.Dialogs(wdDialogFilePrint).Show
    wdDoc.Close();
    wdDoc = null;
    wdApp.Quit();
    wdApp = null;

    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.Collect();
    GC.WaitForPendingFinalizers();


    Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "Jenny00" wrote in message news:fddea430-8445-455a-b902-73b1f2695c02@communitybridge.codeplex.com...

    Can I get an answer to that then please? And again, sorry for having posed the wrong question.


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    Tuesday, January 11, 2011 5:58 AM
  • Yes this works thanks. I didn't think it would end up this way though, and this raises one more question.
    See the thing is I'd like to have 3 possibilities: depending on whether "choice" is set to "0", "1" or "2", I would respectively either print with the default options, or print with the options that were given earlier on and caught with a print dialog, or just show the dialog and let the user choose.

    How do you think I could achieve that?

    Thanks.
    Tuesday, January 11, 2011 6:28 AM
  • How is the user making their choice?

    It's very hard to make suggestions with you having provided so little information on exactly what it is that you have already.


    Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "Jenny00" wrote in message news:2a580686-9288-4ae1-8cb3-922850c7995a@communitybridge.codeplex.com...

    Yes this works thanks. I didn't think it would end up this way though, and this raises one more question.
    See the thing is I'd like to have 3 possibilities: depending on whether "choice" is set to "0", "1" or "2", I would respectively either print with the default options, or print with the options that were given earlier on and caught with a print dialog, or just show the dialog and let the user choose.

    How do you think I could achieve that?

    Thanks.


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    Tuesday, January 11, 2011 7:04 AM
  • The user is providing this information with a drop down list, but ultimately it's just a variable named "choice" in the code, that can be set to 0, 1 or 2. If 0 is set when the program reaches the part where the document has to be printed, then the default options will be used and there will be no dialog asking for printing options. If 1 is set, then the user will be asked for the printing options only the first time, and all the later prints will be printed using the options of the first print. If 2 is set, then the dialog should appear everytime.

    Sorry about the lack of information, it's just I didn't want to bother you with the details. If you have any more questions please let me know.

    Tuesday, January 11, 2011 7:21 AM
  • Use something like:

    using Word = Microsoft.Office.Interop.Word;

    //in a procedure
    Dim firsttime As Boolean
    Word.Application wdApp = new Word.Application();
    Word.Document wdDoc = wdApp.Documents.Open(//the document to open and other params);
    On Error Resume Next
    firsttime = wdDoc.Variables("FirstTime").Value
    If Error Then
       firsttime = True
    End If
    Select Case Choice
       Case 0
           wdDoc.PrintOut
       Case 1
           If firsttime = False Then
               wdDoc.PrintOut
           Else
               wdApp.Dialogs(wdDialogFilePrint).Show
               wdDoc.Variables("FirstTime").Value = False
           End If
       Case 2
           wdApp.Dialogs(wdDialogFilePrint).Show
    End Select
    wdDoc.Close();
    wdDoc = null;
    wdApp.Quit();
    wdApp = null;

    GC.Collect();
    GC.WaitForPendingFinalizers();
    GC.Collect();
    GC.WaitForPendingFinalizers();
     -- Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "Jenny00" wrote in message news:33d06de0-29e3-437f-a674-e68bec99ef23@communitybridge.codeplex.com...

    The user is providing this information with a drop down list, but ultimately it's just a variable named "choice" in the code, that can be set to 0, 1 or 2. If 0 is set when the program reaches the part where the document has to be printed, then the default options will be used and there will be no dialog asking for printing options. If 1 is set, then the user will be asked for the printing options only the first time, and all the later prints will be printed using the options of the first print. If 2 is set, then the dialog should appear everytime.

    Sorry about the lack of information, it's just I didn't want to bother you with the details. If you have any more questions please let me know.


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    • Marked as answer by Bessie Zhao Monday, January 17, 2011 11:03 AM
    • Unmarked as answer by Jenny00 Thursday, January 27, 2011 12:46 PM
    Tuesday, January 11, 2011 9:04 AM
  • Hello,

    thanks that looks very interesting. Is there any chance you could translate this to c# though? I'm having a little trouble to distinguish between the words I don't know because they are vb, and those that are just new to me and needed for this task.

    Thanks

    Tuesday, January 11, 2011 9:50 AM
  • Sorry I no nought about C#.


    Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "Jenny00" wrote in message news:fc8b20ef-8ae4-45d6-94f0-084a9fcded9e@communitybridge.codeplex.com...

    Hello,

    thanks that looks very interesting. Is there any chance you could translate this to c# though? I'm having a little trouble to distinguish between the words I don't know because they are vb, and those that are just new to me and needed for this task.

    Thanks


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    Tuesday, January 11, 2011 11:51 AM
  • Well pseudo code then?
    Can you please just try to explain what you're doing ?

    I espacially don't understand this:

    On Error Resume Next

    and this:

    wdDoc.Variables("FirstTime").Value

    .

    It would appear to me that you're always printing the same document, which is not what I want (there would be an easier way to do this: just choose to print multiple copies in the print options, no need to show the print dialog several times for the same document). I want to print other documents, just with the same options (such as which printer to print with or the quality of the printing, for instance).

     

    Tuesday, January 11, 2011 12:02 PM
  • If you are only printing a document once, then the On Error Resume Next and wdDoc.Variables will not be applicable, but what it was doing was setting a variable in the document that could be used to determine if it was the first time that it was being printed.

    It seems however that you want to do this on a session basis and once again, without really knowing all about what you are doing, it is hard to say what the best way of going about that would be.

    I had just picked up on the pseudo code that Cindy had posted (while she would have been sleeping)


    Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "Jenny00" wrote in message news:82b5624b-0e09-4a45-ba0f-9dcab1e4dbbd@communitybridge.codeplex.com...

    Well pseudo code then?
    Can you please just try to explain what you're doing ?

    I espacially don't understand this:

    On Error Resume Next

    and this:

    wdDoc.Variables("FirstTime").Value

    .

    It would appear to me that you're always printing the same document, which is not what I want (there would be an easier way to do this: just choose to print multiple copies in the print options, no need to show the print dialog several times for the same document). I want to print other documents, just with the same options (such as which printer to print with or the quality of the printing, for instance).


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    Tuesday, January 11, 2011 2:46 PM
  • Let me be clearer about what I want to do then, and maybe you'll be able to help me:

    what I'm trying to do is a small (I'm really just a beginner as you may have guessed) UI that lets the user choose a bunch of Word files to print, and then prints them all at once, without opening Word (in the taskbar for the user to see, although it can be opened in the background), as this feature (printing multiple files without opening each one of them and creating a mess in the taskbar and in the screen) seems not to have been implemented by Word itself.

    So basically what I have done with my interface is that I have given the ability to the user to select some files from his computer.

    Now what I'm focusing on is the printing options, and I think it would be fair to let the user choose only once the options if he wants to, so as not to be bothered for the potentially hundreds of files he has to print.

    So I have 3 different options which can be set by the user and that, as I said before, us ultimately just a variable named "choice" in the code, that can be set to 0, 1 or 2. If 0 is set when the program reaches the part where the document has to be printed, then the default options will be used and there will be no dialog asking for printing options. If 1 is set, then the user will be asked for the printing options only the first time, and all the later prints will be printed using the options of the first print. If 2 is set, then the dialog should appear everytime.

    PS: it seems you are using a software so I don't know how much control you have over your post, but from my perspective I daresay that it is uncalled for to repeat the previous message in your answer. For those like me who have a small screen it just makes it more difficult to read the whole topic, and it is obvious that you're answering to the last message, unless specified otherwise. Thus there's no need to have double messages. Plus it somehow disables the abillity to edit properly as the posters can't edit your post. Not that this really matters for the task at hand.

     

    Tuesday, January 11, 2011 2:59 PM
  • Hi Jenny

    <<Now what I'm focusing on is the printing options, and I think it would be fair to let the user choose only once the options if he wants to, so as not to be bothered for the potentially hundreds of files he has to print.

    So I have 3 different options which can be set by the user and that, as I said before, us ultimately just a variable named "choice" in the code, that can be set to 0, 1 or 2. If 0 is set when the program reaches the part where the document has to be printed, then the default options will be used and there will be no dialog asking for printing options. If 1 is set, then the user will be asked for the printing options only the first time, and all the later prints will be printed using the options of the first print. If 2 is set, then the dialog should appear everytime.>>

    Is this your real question, then? You're trying to find a way to store the user's choice of 0, 1 or 2 for your application?

    <<Can I get an answer to that then please? And again, sorry for having posed the wrong question.>>

    These forums appear globally, and no one sits here 24/7, answering questions :-) Doug's in Australia; I'm in Europe; others are in China or the US. But not everyone can respond to every question. That's the way it is for non-paid support channels, so sometimes you may just have to wait...


    Cindy Meister, VSTO/Word MVP
    Tuesday, January 11, 2011 5:33 PM
    Moderator
  • No, the part where the user chooses 0 or 1 or 2 has already been implemented, it's just a widget. From your point of view, you can assume it's just a variable in the code named choice.

    My question is: how can I code something in C# that would print a word document with the default printing options, or with options that have been set at a prior date by the user after a print dialog has been showed to him, or with the printing options that he chooses in a print dialog, all of these 3 choices depending on the state of the variable 'choice'.

    The answer from Doug was probably close to the answer, there is probably a need for a variable firstTime to check whether it is the first time the print dialog is showed or not, so that it doesn't show more than once if choice is set to "1".

    Yet I think he thought I was printing the same document over and over, which I am not, it's a list of documents that have to be printed (List<FileInfo>), and this list has to be printed either without user intervention (default printing options will be used), or with user intervention only once (to set the printing settings which will be used for all the files), or with user intervention between every file printing.

    I feel like I've repeated this at least 3 or 4 times already so I hope you have understood what I need now, but nonetheless feel free to ask for more details if it is still unclear.

    My original question was how to link the print dialog and the printOut method, because I have already done the one without the dialog.
    Here is what it will look like (I think):

     

    private void printdocs(List<FileInfo> fileList){
     Word.Application wdApp = new Word.Application();
     wdApp.Visible = false;
     bool firstTime = true;
     foreach(FileInfo in fileList){
     Word.Document wdDoc = wdApp.Documents.Open(file.FullName);
     switch (this.choice){
     case 0: // ask everytime for the printing settings
     wdApp.Dialogs(wdDialogFilePrint).Show // does this work?
     case 1: // ask only for the first file, then use the same printing settings as for the first file
     if (firstTime) {
     // display print dialog and remember the settings for all the later printings.
     firstTime = false;
     }
     else {
     // print in the background with the printing settings of the first file
     }
     case 2:
     wdDoc.printOut();
     }
     wdDoc.Close();
     }
    wdApp.Quit();
    }
    Tuesday, January 11, 2011 6:39 PM
  • Hi Jenny

    <<My question is: how can I code something in C# that would print a word document with the default printing options, or with options that have been set at a prior date by the user after a print dialog has been showed to him, or with the printing options that he chooses in a print dialog, all of these 3 choices depending on the state of the variable 'choice'.>>

    And that takes us right back to my original response:

    "So, in general terms what you need to do is:

    1. Store the information the user selects in the .NET dialog box in variables/objects.
    2. Analyze how this corresponds to the parameters of the PrintOut command
    3. Issue that command on the document object in the Word application, inserting the stored information"

    So what you now need to do is figure out what those options are that are coming from the Print dialog. This is a Word forum, don't assume anyone here is familiar that .NET dialog - you have to provide that information. What are the options that come in question? Number of copies? Page range?

    List all of those, then look at the list of parameters for the PrintOut method. Look for matches. Then ask about things for which you can't find a match.

    Note that, if you don't want to show the Word application window then you also cannot use Word's built-in dialog box.


    Cindy Meister, VSTO/Word MVP
    Tuesday, January 11, 2011 6:58 PM
    Moderator
  • I see, I thought there was a built in way of doing that, without having to do it by hand. Can you help me to make the correspondance then please? As for the options that have to be linked, well for the option where the dialog is shown everytime, I need to connect as many options as possible, and for the second choice, where the dialog is shown only the first time, the options that are connected must not be specific to one single document (options such as page range should be excluded).

    Thanks.

    Wednesday, January 12, 2011 11:35 AM
  • Hi Jenny

    You need to provide us with the options from the dialog box you'll be using. You tell us what kinds of things you need to stipulate for printing, and we can help you match those up with what Word's object model supports.

    <<for the second choice, where the dialog is shown only the first time, the options that are connected must not be specific to one single document (options such as page range should be excluded).>>

    Are you saying you need to figure out where to store this information?

    How are you storing the 0, 1, 2 choices?


    Cindy Meister, VSTO/Word MVP
    Wednesday, January 12, 2011 12:52 PM
    Moderator
  • The choice option (0 or 1 or 2) is stored in a member of the current class, and can be accessed with this.choice, as showed in my previous post.

    I don't know exactly what options I'll be using, I would just like to provide my user with as many options as possible. The problem is that I'm just a beginner both in programming and in word automation, and I'm sure I'd get less help with that matter if I tried in the wpf forum, as it is after all quite Word related. From what I can see in the help files, I could use the PrintDialog.ShowDialog method to display the dialog with the options to choose. After google-imaging this dialog, I can see that it has mainly 4 features, one for choosing the printer, one for choosing the printer options (which will include whether to print in color or b&w, the quality of the printing, etc), one for choosing the number of copies, and one for choosing which pages to print (the latter should not be used for choice=1, because choice=1 means that the same options will be used for all the subsequent printings, and the other documents might not have the same number of pages).

    But now that I came to study this dialog box carefully, I noticed that it doesn't have all the features that the Word dialog has (such as the number of pages per paper). Are you sure there is no way to show this Word dialog without showing the Word background (with the menu and everything), whether the way to hide this would be through a Word related command or through a wpf/C# command, and even if I don't care whether it is opened as a process as long as it is not visible?
    Wednesday, January 12, 2011 1:40 PM
  • Hi Jenny

    <<Are you sure there is no way to show this Word dialog without showing the Word background (with the menu and everything), whether the way to hide this would be through a Word related command or through a wpf/C# command, and even if I don't care whether it is opened as a process as long as it is not visible?>>

    Yes, I'm sure. The Word window will flash up if you use Word's built-in dialog box. I just tested again to be doubly sure.

    <<The choice option (0 or 1 or 2) is stored in a member of the current class, and can be accessed with this.choice, as showed in my previous post.>>

    So you could save the options the user picks up from the dialog box the same way, couldn't you? Or do you need to save these between sessions? In that case you should ask in the WPF forum how they recommend to do that, given the kind of project you have. There are many different ways to persist data, such as writing it to the Registry or to a file in the user profile, in your projects folders or to Isolated Storage...

    <<But now that I came to study this dialog box carefully, I noticed that it doesn't have all the features that the Word dialog has (such as the number of pages per paper). >>

    You can ask in the WPF forum whether it's possible for you to expand this dialog box with additional controls, so that you can provide options for things Word offers that it does not.

    You should also look carefully at the PrintOut method to see what parameters it offers, and read the information about those parameters in the object model Help. Here's the method signature:

    PrintOut([Background], [Append], [Range], [OutputFileName], [From], [To], [Item], [Copies], [Pages], [PageType], [PrintToFile], [Collate], [FileName], [ActivePrinterMacGX], [ManualDuplexPrint], [PrintZoomColumn], [PrintZoomRow], [PrintZoomPaperWidth], [PrintZoomPaperHeight])

    Just looking at a graphical representation of Word's Print dialog box won't give you a true idea of its capabilities. You should actually start Word as an end-user and look at the Print dialog box in the Word application. Hover your mouse over the controls to see context-sensitive information. Test printing longer documents with the various settings. You need to understand what it is you want to use. This dialog box provides extremely complex functionality and if you want to enable all of this for your users you have to understand it. Doing this kind of research is also part of being a developer - it's not just writing code :-)


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bessie Zhao Monday, January 17, 2011 11:02 AM
    • Unmarked as answer by Jenny00 Thursday, January 27, 2011 12:42 PM
    Wednesday, January 12, 2011 5:06 PM
    Moderator
  • okay, thanks for this instructive comment.
    So let's forget about advanced options for now.
    How could I just use the options from the dialog box and forward them to the printOut function?

    The basic options, from what I can see from the dialog and as stated before, are:

    one for choosing the printer, one for choosing the printer options (which will include whether to print in color or b&w, the quality of the printing, etc), one for choosing the number of copies, and one for choosing which pages to print (the latter should not be used for choice=1, because choice=1 means that the same options will be used for all the subsequent printings, and the other documents might not have the same number of pages).

    I have no idea how to access them though, so please help me.

    Thanks for your help.

    Thursday, January 13, 2011 9:24 AM
  • I realise that you are not using VBA, but that is all that I know.

    However, you may find information that is of assistance to you in the following series of articles:

    Controlling the Printer from Word VBA
    Part 1: Using VBA to Select the Paper Tray
    http://pubs.logicalexpressions.com/Pub0009/LPMArticle.asp?ID=101

    Controlling the Printer from Word VBA
    Part 2: Using VBA to control Duplex, Color Mode and Print Quality
    http://pubs.logicalexpressions.com/Pub0009/LPMArticle.asp?ID=116

    Controlling the Printer from Word VBA
    Part 3: Dealing with Different Paper Sizes
    http://pubs.logicalexpressions.com/Pub0009/LPMArticle.asp?ID=132

    Controlling the Printer from Word VBA
    Part 4: Getting printer driver details
    http://pubs.logicalexpressions.com/Pub0009/LPMArticle.asp?ID=183


    Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "Jenny00" wrote in message news:c4628897-3848-4d28-bf56-49f36ddf9f07@communitybridge.codeplex.com...

    okay, thanks for this instructive comment.
    So let's forget about advanced options for now.
    How could I just use the options from the dialog box and forward them to the printOut function?

    The basic options, from what I can see from the dialog and as stated before, are:

    one for choosing the printer, one for choosing the printer options (which will include whether to print in color or b&w, the quality of the printing, etc), one for choosing the number of copies, and one for choosing which pages to print (the latter should not be used for choice=1, because choice=1 means that the same options will be used for all the subsequent printings, and the other documents might not have the same number of pages).

    I have no idea how to access them though, so please help me.

    Thanks for your help.


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    • Marked as answer by Bessie Zhao Monday, January 17, 2011 11:03 AM
    • Unmarked as answer by Jenny00 Thursday, January 27, 2011 12:42 PM
    Thursday, January 13, 2011 10:37 AM
  • Hi Jenny

    <<one for choosing the printer, one for choosing the printer options (which will include whether to print in color or b&w, the quality of the printing, etc)>>

    These two aren't controlled over the PrintOut method. The first Word doesn't handle well, and the second it can't handle at all; you'll need to go over the Windows API for that. The reason is that the "Pinters" button in the Print dialog box calls up an interface and options that are specific to the printer. This isn't part of Word and Word can't "hook into it".

    The following article describes how to handle setting the printer for Word, via the object model:

    http://word.mvps.org/FAQs/MacrosVBA/ChangeCurPrinter.htm

    As it says, you can set the printer using Application.ActivePrinter, but that will change the Windows default printer. If you don't want to do that, you'd need the approach described. This is a VBA sample and would work fine for VB.NET, but you'll have to attack it from a different angle for C#. The reason is that the properties listed for the dialog box are actually late-bound into Word - C# won't recognize them. You'll need to use GetType().PInvoke(). I was trying to avoid this, since you're a beginner, but apparently we have no choice...

       Word.Dialog dlg = wdApp.Dialogs[Microsoft.Office.Interop.Word.WdWordDialog.wdDialogFilePrintSetup];
       object oDlg = dlg;
       object[] oArgs = { "EPSON Stylus DX4000 Series" };
       dlg.GetType().InvokeMember("Printer", BindingFlags.SetProperty, null, oDlg, oArgs);
       oArgs[0] = true;
       dlg.GetType().InvokeMember("DoNotSetAsSysDefault", BindingFlags.SetProperty, null, oDlg, oArgs);
       dlg.GetType().InvokeMember("Execute", BindingFlags.InvokeMethod | BindingFlags.IgnoreReturn, null, oDlg, null);
    
    

    <<one for choosing the number of copies, and one for choosing which pages to print>>

    Look at the PrintOut method signature. The parameter Copies is for the number of copies.

    There are two ways to tell Word what pages to print. The most flexible corresponds to typing into the "Pages" box of the Print dialog box. This lets you print non-contiguous ranges of pages, such as the example you'll see in Word 2007's dialog box, below the "Pages" box.

    In order to specify this in the PrintOut method, you need to set the Range parameter to the Enum  Word.WdPrintOutRange.wdPrintRangeOfPages AND the Pages parameter to the string that would appear in the Pages box.

    Now, since you're using C# and all of these parameters are optional variants as far as the Word object model is concerned, you must first declare variables of data type object and assign the values to those, then use those in the PrintOut method. All other required parameters should be passed as System.Type.Missing. In pseudo-code that would be something like

    object missing = System.Type.Missing;
    object oPrintRange = Word.WdPrintOutRange.wdPrintRangeOfPages;
    object oPages = "1,3";
    object oCopies = 2;
    wdDoc.PrintOut(ref missing, ref missing, ref oPrintRange, ref missing, ref missing....until you reach Copies..., ref oCopies, ref oPages, ref missing....to the end);


    Cindy Meister, VSTO/Word MVP
    • Edited by Cindy Meister MVPModerator Thursday, January 13, 2011 10:56 AM added copies to pseudocode
    • Marked as answer by Bessie Zhao Monday, January 17, 2011 11:03 AM
    • Unmarked as answer by Jenny00 Thursday, January 27, 2011 12:42 PM
    Thursday, January 13, 2011 10:55 AM
    Moderator
  • hello,

     

    sorry to come back after so long, I had other projects to work on. Once again I'd like to thank all of you who tried to solve my issue.
    I'm sorry but I still can't get this to work. I don't understand all this vba stuff, which doesn't help. Is there any chance you could try to translate this to C# so that I could try if it works as I planned it to?

    Thanks a lot.

    Tuesday, January 25, 2011 11:58 PM
  • I'm temporarily marking the posts as unanswered so that the whole topic is marked with a '?' because I still didn't get this code to work.

    Thanks for helping me.
    Thursday, January 27, 2011 12:48 PM
  • Can you please help me?
    I tried posting in the C# forum but I got no answers, so this is the only forum that seems to know what my problem is.

    I really need help to make this work.

    thanks
    Friday, February 4, 2011 2:50 PM
  • I have been struggling with this issue for a month now, I really need help please.
    Thursday, February 10, 2011 4:44 AM
  • Do you have to use C#?


    Hope this helps.

    Doug Robbins - Word MVP,
    dkr[atsymbol]mvps[dot]org
    Posted via the Community Bridge

    "Jenny00" wrote in message news:95b09874-e2c8-4cdc-9262-597992cc881b@communitybridge.codeplex.com...

    I have been struggling with this issue for a month now, I really need help please.


    Doug Robbins - Word MVP dkr[atsymbol]mvps[dot]org
    Saturday, February 12, 2011 5:16 AM
  • Yes I do, the whole rest of my application is in C#, and it's quite huge.
    This is just the part about printing a word document, it should be pretty easy but it's been a month now and I still didn't find out how to do it...

    all I want is for the user to have the choice whether to be asked for the printing options or not (or only the first time), and then print it without flashing the Word window which would be quite unesthetic. Plus I'm sure it would be less performant since it would require graphical ressources, etc.

    I really need help with that, I'm sure it's just a few lines of code but I guess I'm not good enough to do it. There should be some people here with enough knowledge though I hope, so please help me.

    Thanks a lot.

    ps: if you need yet one more explanation of what I want, with more details, I can state it again, but as you can see from the size of this thread I think I've already done it more than once.
    Tuesday, February 15, 2011 3:36 AM