Excel VBA 中的 ExportAsFixedFormat 错误的 PDF 输出大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15084462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:02:40  来源:igfitidea点击:

wrong PDF output size with ExportAsFixedFormat in Excel VBA

vbapdfexcel-vbapdf-generationexcel-2010

提问by Chapo

I use Excel 2010 with VBA.

我将 Excel 2010 与 VBA 一起使用。

I have the following code :

我有以下代码:

mySh.PageSetup.PaperSize = xlPaperLetter
mySh.ExportAsFixedFormat Type:=xlTypePDF, _
    Filename:=filePathTmp, _
    Quality:=xlQualityStandard, IncludeDocProperties:=False, _
    IgnorePrintAreas:=False, OpenAfterPublish:=False

It generates a PDF without error but the size is not correct. I get a 22.26 x 28.80 cm instead of the 21.59 x 27.94 cm that I would like to obtain.

它生成一个没有错误的 PDF,但大小不正确。我得到的是 22.26 x 28.80 厘米,而不是我想要的 21.59 x 27.94 厘米。

Any ideas ?

有任何想法吗 ?

Thanks for your help

谢谢你的帮助

回答by Crystal

I use Excel 2010 and had this problem. I fixed it by changing the printer in Excel's dropdown list on the Print section of the File tab. My default printer is a Konica Minolta copy machine. I changed it to Local Printer (which doesn't actually exist) and it worked! I now have the VBA code returning an 8.5x11 PDF! Now I don't have to use our clunky old laptop with an old Office to get the right size.

我使用 Excel 2010 并遇到了这个问题。我通过在“文件”选项卡的“打印”部分的 Excel 下拉列表中更改打印机来修复它。我的默认打印机是柯尼卡美能达复印机。我将其更改为本地打印机(实际上并不存在)并且它起作用了!我现在有返回 8.5x11 PDF 的 VBA 代码!现在,我不必将笨重的旧笔记本电脑与旧 Office 一起使用来获得合适的尺寸。

Note that picking the Adobe PDF printer did not fix the problem. Just try messing with the different printer choices, or make a fake printer in your computer's Devices and Printers and use that. I hope it works for others with the same issue!

请注意,选择 Adob​​e PDF 打印机并不能解决问题。只需尝试弄乱不同的打印机选择,或者在计算机的设备和打印机中制作一个假打印机并使用它。我希望它适用于有同样问题的其他人!

回答by user11059146

I set the print area where I wanted it: View-Page Break Preview. Then File-Print-Scaling = Fit Sheet on One Page Or File-Print-Page Setup-Fit to: 1 pages wide by 1 tall.

我将打印区域设置在我想要的位置:查看-分页预览。然后 File-Print-Scaling = 适合一页纸或 File-Print-Page Setup-Fit to:1 页宽 x 1 高。

Hope it helps someone.

希望它可以帮助某人。

回答by Ewen

I also use Excel 2010 and had the same problem. When using ExportAsFixedFormat, the page won't scale. From what I understand, it is the same as using the Save As with file type pdf. This means that you need to need to scale your workbook to fit the page that you require.

我也使用 Excel 2010 并遇到了同样的问题。使用 ExportAsFixedFormat 时,页面不会缩放。据我了解,它与使用文件类型为 pdf 的另存为相同。这意味着您需要缩放工作簿以适合您需要的页面。

If you:

如果你:

-Open printer settings and select 'No Scaling'

- 打开打印机设置并选择“无缩放”

-Page Layout > Print Area > Clear Print Area

- 页面布局 > 打印区域 > 清除打印区域

-Select View > Page Break View

- 选择视图 > 分页视图

It will show you the page area. If you scale your worksheet to this it will save to pdf using ExportAsFixedFormat properly.

它将显示页面区域。如果您将工作表缩放到此,它将正确使用 ExportAsFixedFormat 保存为 pdf。

回答by Oshura

We had the same problema and soved it changing at devices and printers the default printing options:

我们遇到了同样的问题,并解决了在设备和打印机上更改默认打印选项的问题:

-Control Panel->Devices and Printers->Select the printer by clicking once to it.

-控制面板->设备和打印机->点击一次选择打印机。

-Second mouse button->Printer properties->Advanced->Printing defaults->Advanced.

-第二个鼠标按钮->打印机属性->高级->打印默认值->高级。

-Choose paper size and click OK.

- 选择纸张尺寸并单击确定。

Why not to change it at excel? Because somthing related to permissions between Excel and the service running spool or...

为什么不在excel上改呢?因为有些与 Excel 和运行假脱机的服务之间的权限有关,或者...

That default setting makes the macro to export to the paper size correctly.

该默认设置使宏正确导出到纸张尺寸。

If it Works for you, is thanks to our IT department, not to me :P

如果它对您有用,这要归功于我们的 IT 部门,而不是我 :P