none
Document be printed out of printed area in Office 2010 with VSTO RRS feed

  • Question

  • Hi,

    I wrote a program to control word 2003 to do open a HTML file, add an page footer and then print it out to printer. It works fine in XP + Office 2003 machine. When we switch to use Windows 7 and Office 2010, then problem is found in the print out.

    The content of the HTML in print out goes outside the printed area, it seems the word 2010 does not do correct line break.

    I have no idea which part goes wrong, does anyone can help?

    For the VSTO dll, I have switched to use this

    C:\Program Files\Common Files\microsoft shared\VSTO\10.0\VSTOLoader.dll

    But I am not sure the new machine is installed which version of VSTO runtime

    (I remembered I did it before explicitly before C# program can do control on office 2003)

    Please kindly help.

    Thanks in advance,

    Raymond

    -------------------------------

    Object conversionDialogShow = false;
    Object read_only = true;
    Object background = true;       
    Object append = Type.Missing;
    Object range = Microsoft.Office.Interop.Word.WdPrintOutRange.wdPrintAllDocument;
    Object outputFileName = Type.Missing;
    Object from = Type.Missing;
    Object to = Type.Missing;
    Object item = Microsoft.Office.Interop.Word.WdPrintOutItem.wdPrintDocumentContent;
    Object copies = no_of_copies;
    Object pages = "";
    Object pageType = Microsoft.Office.Interop.Word.WdPrintOutPages.wdPrintAllPages;
    Object printToFile = false;
    Object collate = true;
    Object fileName = "";
    Object activePrinterMacGX = Type.Missing;
    Object manualDuplexPrint = Type.Missing;
    Object printZoomColumn = Type.Missing;
    Object printZoomRow = Type.Missing;
    Object printZoomPaperWidth = Type.Missing;
    Object printZoomPaperHeight = Type.Missing;

    ////object fileformat = WdOpenFormat.wdOpenFormatAllWord;
    object fileformat = WdOpenFormat.wdOpenFormatAuto;
    ////object fileformat = WdOpenFormat.wdOpenFormatXML;

    // this option is no longer work
    //if (duplex)
    //    manualDuplexPrint = true;

    Object word_filename = filename;

    Word.Document theDocument = theApplication.Documents.Open(ref word_filename,
                                                ref conversionDialogShow, ref read_only, ref missing, ref missing,
                                                ref missing, ref missing, ref missing, ref missing,
                                                ref fileformat, ref missing, ref missing, ref missing,
                                                ref missing, ref missing, ref missing);
                    
    theDocument.Application.ActivePrinter = printer_name;                

    theDocument.PageSetup.Orientation = WdOrientation.wdOrientPortrait;
    theDocument.PageSetup.TopMargin = 20;
    theDocument.PageSetup.BottomMargin = 20;
    theDocument.PageSetup.LeftMargin = 36;
    theDocument.PageSetup.RightMargin = 36;                                

    theDocument.PrintOut(ref background, ref append,
                         ref range, ref outputFileName, ref from, ref to,
                         ref item, ref copies, ref pages, ref pageType,
                         ref printToFile, ref collate, ref activePrinterMacGX,
                         ref manualDuplexPrint, ref printZoomColumn,
                         ref printZoomRow, ref printZoomPaperWidth, ref printZoomPaperHeight);


    Wednesday, September 5, 2012 4:29 PM

Answers

  • Hi Raymond

    Word has the Range.Find property that lets you search for content in the Word document.

    Range.Find.Replacement allows you to replace "hits" with something else. There's also the option in Find.Execute to replace all. So you should be able to replace all tags with a pagebreaks in one step.

    There are a number of code examples for using Find in old discussions in this forum and in http://social.msdn.microsoft.com/Forums/en-US/worddev/threads.

    Edit: The Replacement.Text for inserting a page break is: "^m"


    Cindy Meister, VSTO/Word MVP, my blog


    Thursday, September 13, 2012 12:49 PM
    Moderator

All replies

  • Hi Raymond,

    Thanks for posting in the MSDN Forum.

    I would recommend you recreate your project under Visual Studio 2010 to see whether it will work. If it doesn't or you doesn't have environment to do that, please feel free to let me know.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, September 6, 2012 2:30 AM
    Moderator
  • Hi Raymund

    <<The content of the HTML in print out goes outside the printed area, it seems the word 2010 does not do correct line break.>>

    Do you mean the document runs off the page, rather than breaking over to a new line when it reaches the margin? Or that you're getting an error message from Word complaining that the margins are wrong?

    If you start Word as an end-user and open this HTML document do you still see the same problem, or is it different than when your code runs?


    Cindy Meister, VSTO/Word MVP

    Thursday, September 6, 2012 9:34 AM
    Moderator
  • Hi Raymund

    <<The content of the HTML in print out goes outside the printed area, it seems the word 2010 does not do correct line break.>>

    Do you mean the document runs off the page, rather than breaking over to a new line when it reaches the margin? Or that you're getting an error message from Word complaining that the margins are wrong?

    If you start Word as an end-user and open this HTML document do you still see the same problem, or is it different than when your code runs?


    Cindy Meister, VSTO/Word MVP

    Hi Cindy,

    Thanks for your reply first. Yes, rather than breaking over to a new line, it printed outside...same as the word 2010 regards the page width is larger than A4.

    No any error message encountered, I also did not figure the error, user reported this in his testing.

    I remembered I tried to open the HTML in word 2010 but did not print it manually.

    What do you think should I do the manual print first?

    Please help.

    Thanks,

    Ray

    Thursday, September 6, 2012 3:45 PM
  • Hi Ray

    If this is happening only with another user, then the problem may be local to that machine. I'd ask the user to open the HTML document (instead of running your code) and ask him to look at it first in Word (in the Print Layout view). When he looks at it, does it appear correct, or is the text also running off the edge of the page on the screen?

    Also have him check the paper size, to check your theory about the different paper size.

    If it appears correct on the screen AND the paper size is what you expect (A4) ask him to print it out and then compare that result with the result from running your code.

    I also notice that you suppress the display of the conversion dialog box. It would be interesting to know what Word thinks it's seeing when it tries to open the document - what it thinks it's converting from. You should compare what the user sees to what you see when you try on your machine.

    Did both you and the user test on Word 2010?

    My suspicison is that there may be a difference in how the converters are interpreting and converting the HTML document...


    Cindy Meister, VSTO/Word MVP

    Thursday, September 6, 2012 4:13 PM
    Moderator
  • Hi Cindy,

    Thanks for your help in advance. You helped me a lot before in VSTO with Word 2003.

    I tried to use to debug the problem and stopped when the C# opens a HTML file and saves it in a doc file. Then I use Word 2010 to open the word file and do a manual print operation. In the preview print window, I notice that one interesting problem, that is the document is bilingual, I notice one table (formerly is HTML table) in english part, the line break is fine, in the lower chinese part, the table line print is failed, one long statement does not link break and print outside the right margin and does not return.

    I really do not know how to handle it now.

    Thanks,
    Ray

    Monday, September 10, 2012 1:20 PM
  • The two lines from HTML like this

    English part:

    <tr>   <td width=20 valign=top>   <p style='font-size:8.0pt;font-family:Arial;color:black'>3.</span></p>   </td>   <td width=720 valign=top>   <p style='font-size:8.0pt;font-family:Arial;color:black'>     XXXXXXXXXXXXXXXXXX   </span></p>   </td>  </tr>

    Chinese part:

    <tr>   <td width=20 valign=top>   <p lang=ZH-TW style='font-size:9.0pt;font-family:SimHei;color:black'>3.</span></p>   </td>   <td width=720 valign=top>   <p lang=ZH-TW style='font-size:9.0pt;font-family:SimHei;color:black'>    XXXXXXXXXXXXXXXXXXX   </span></p>   </td>  </tr>

    ------------------------------------

    No special difference but do not know why the line break in chinese part does not work.

    Monday, September 10, 2012 1:31 PM
  • Hi Raymond

    bilingual with Chinese, eh? I'm afraid that my experience doesn't extend into this area; I've absolutely no idea what may be happening with the rendering of Chinese (and the switch in text direction) that could be causing this.

    My inclination is to advise you to ask on Answers as the problem really falls more in the region of "how does Word work" rather than "how do I use VSTO or the Word APIs to control Word".
    http://answers.microsoft.com/en-us/office/forum/word

    There are people on that forum with experience in RTL and mixed languages.

    OTOH the support staff here on the MSDN Office forums is Chinese and they may have practical experience with this question. So you might also answer this reply with "I still need help" then wait for someone from the "Contingent Staff" to pick it up.


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, September 10, 2012 1:41 PM
    Moderator
  • I still need help, which forum I should post my question to? T-T
    Monday, September 10, 2012 3:56 PM
  • Hi Cindy,

    In case in Word 2010, is there any easy way to insert "a page break" within a doc document?

    I just need some codes so that I can save the document into docx format and then insert a page break on a paragraph.

    I hope saving the file into 2010 format can fix this problem to me.

    THanks


    Monday, September 10, 2012 4:01 PM
  • Hi Raymond

    Here's the basic code to insert a page break. Note that you must first define the Range object (tell Word where you want the page break):

      Range.InsertBreak(Word.WdBreakType.wdPageBreak)


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, September 10, 2012 5:12 PM
    Moderator
  • Thanks Cindy, but I need one more hint, I need to define a tag, for example, like <!-- PAGEBREAK -->

    in the word file and then do a search to replace this tag with page break.

    Would you advise me how to do a search on the tag and then I can execute the above Range statement.

    Many thanks to your help,

    Raymond

    Wednesday, September 12, 2012 4:53 PM
  • Hi Raymond,

    Thanks for posting in the MSDN Forum.

    What about Text Regular Expression? I think you can replace the tag via Text Regular Expression. It will let you save a lot of labor on handle string issue.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, September 13, 2012 6:56 AM
    Moderator
  • Hi Raymond

    Word has the Range.Find property that lets you search for content in the Word document.

    Range.Find.Replacement allows you to replace "hits" with something else. There's also the option in Find.Execute to replace all. So you should be able to replace all tags with a pagebreaks in one step.

    There are a number of code examples for using Find in old discussions in this forum and in http://social.msdn.microsoft.com/Forums/en-US/worddev/threads.

    Edit: The Replacement.Text for inserting a page break is: "^m"


    Cindy Meister, VSTO/Word MVP, my blog


    Thursday, September 13, 2012 12:49 PM
    Moderator
  • Hi Raymond,

    I will mark Cindy's reply as answer, please feel free to unmark it if you don't think so.

    Have a good day,

    Tom


    Tom Xu [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, September 19, 2012 6:43 AM
    Moderator