设置Microsoft Word的默认打印页面大小

时间:2012-05-05 18:45:56

标签: vb.net printing ms-word

我正在使用Microsoft.Office.Interop.Word使用vb.net和microsoft word,一切都很好。我唯一的问题是我无法找到一种方法将默认页面大小打印设置从“letter”更改为“A4”。 此代码正在为Crystal报表执行此任务,但不是为Word执行此操作

Dim pp As New System.Drawing.Printing.PrintDocument For i = 0 To pp.DefaultPageSettings.PrinterSettings.PaperSizes.Count - 1 If pp.DefaultPageSettings.PrinterSettings.PaperSizes.Item(i).Kind = System.Drawing.Printing.PaperKind.A4 Then pp.DefaultPageSettings.PaperSize = pp.DefaultPageSettings.PrinterSettings.PaperSizes.Item(i) Exit For End If Next

3 个答案:

答案 0 :(得分:2)

您应该更改Word文档实例上的PageSetup界面的PaperSize。

Imports Microsoft.Office.Interop.Word
....

Dim myWordApp as Application = New Application();  
Dim myWordDoc As Document = myWordApp.Documents.Open("your_file_name_here")
myWordDoc.PageSetup.PaperSize = WdPaperSize.wdPaperA4

答案 1 :(得分:1)

参考:http://social.msdn.microsoft.com/forums/en-US/vsto/thread/45152591-1f3e-4d1e-b767-ef030be9d9f2

由于页面大小因段而异,因此最好的方法是设置Document.Section对象的PageSetup属性。例如,您可以遍历文档的所有部分:

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    Application app = Globals.ThisAddIn.Application;
    Word.Document doc = app.ActiveDocument;
    foreach (Section section in doc.Sections)
    {
        section.PageSetup.PaperSize = WdPaperSize.wdPaperA4;
    }
}

在创建或打开文档时添加用于设置纸张大小的位置取决于您,我猜您需要确定打开的文档是否故意以非A4大小保存。

编辑:这确实有用,我不知道您对评论ThisAddIn isnt a member or Globals and ActiveDocument isnt a member of Application in VB.NET的意思 - 你不能跳过这两行,这里是VB.Net版本:

Private Sub ThisAddIn_Startup(sender As Object, e As System.EventArgs)
    Dim app As Application = Globals.ThisAddIn.Application
    Dim doc As Word.Document = app.ActiveDocument
    For Each section As Section In doc.Sections
        section.PageSetup.PaperSize = WdPaperSize.wdPaperA4
    Next
End Sub

您需要做的就是> Visual Studio>创建一个新的Prorject>办公室> Word 2010(或2007)加载项并粘贴在上面的代码中。这是一个截图,显示它适用于A4和Letter:

enter image description here

您可能遇到的唯一问题是当打印机没有尺寸纸张时会出现此错误:Requested PaperSize is not available on the currently selected printer.

答案 2 :(得分:0)

我正在发布此问题的另一个答案,因为已接受的答案不会刷新文档或处理页面方向。所以,万一有人需要这个,我发现这是一个更好的解决方案...

            Microsoft.Office.Interop.Word.Application app = Globals.ThisAddIn.Application;
            Microsoft.Office.Interop.Word.Document doc = app.ActiveDocument;

            foreach (Section section in doc.Sections)
            {
                if(section.PageSetup.Orientation == WdOrientation.wdOrientLandscape)
                {
                    section.PageSetup.PageWidth = 841.88976378F;
                    section.PageSetup.PageHeight = 595.275590551F;

                }
                else
                {
                    section.PageSetup.PageWidth = 595.275590551F;
                    section.PageSetup.PageHeight = 841.88976378F;

                }

            }