1

我正在使用 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

4

3 回答 3

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
于 2012-05-05T20:51:47.267 回答
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;
    }
}

在创建或打开文档时添加 locic 以设置纸张大小取决于您,我猜您需要确定正在打开的文档是否故意以非 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 > 创建新项目 > Office > Word 2010(或 2007)加载项并粘贴上面的代码。这是一个屏幕截图,显示它适用于 A4 和 Letter:

在此处输入图像描述

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

于 2012-05-07T00:22:55.760 回答
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;

                }

            }
于 2013-09-20T16:10:55.643 回答