【问题标题】:Excel vba emf pasted to worksheet print/pdf wrong sizeExcel vba emf 粘贴到工作表打印/pdf 大小错误
【发布时间】:2014-03-18 14:00:37
【问题描述】:

我正在将另一个应用程序 (DPlot Jr) 生成的增强元文件复制到剪贴板,并将其粘贴到 Excel(2007) 工作表中。从那里我使用vba将该文件转换为pdf。这是我必须这样做的代码:

            ' copy the graph eml file to the clip board

            ret = DPlot_Command(doc, "[CopyPicture()]")

            ' copy the clip board contents to a new temp worksheet (under the covers)

            'Hide the application
            Application.ScreenUpdating = False

            'Create a new temp worksheet
            Set ws = ActiveWorkbook.Sheets.Add(After:=Sheets(1))
            ws.Name = "Temp_Graph_DPJR"
            ws.Activate

            'Paste to the temp worksheet
            ActiveSheet.Paste

            'Save as pdf from excel
            ActiveSheet.ExportAsFixedFormat _
                 Type:=xlTypePDF, _
                 filename:=pdf_filename, _
                 OpenAfterPublish:=False

我发现,在 PDF 中创建的图像比图形的实际大小略大。例如,图形的宽度应为 2.65"。PDF 中图形的大小为 2.816,因此似乎添加了大约 0.166" 或 1 Pica。

虽然我可以并且可能必须将图像的大小最初减小 0.166",但这似乎有点 hacky,我只想让图像的原始大小过来。

我发现,如果我将图像粘贴到 Chartsheet,尺寸会保持不变,但图像会变成 Chartsheet 页面上的位图。

当我创建 pdf 时,我拥有所有正确的设置。我没有边距、实际大小等。

还有其他人看过吗?任何人都可以帮忙吗?我需要将图像作为 pdf 格式。

感谢您的帮助!

罗斯

【问题讨论】:

  • 是统一添加为边框像素还是底层图像“拉伸”?如果是前者,那么您可以指示 vba 裁剪图像(Shape.PictureFormat 的成员)。如果是后者,您可以指示 vba 调整图像大小(Shape 的成员)。
  • 我相信底层图像被拉伸了一点,我一直在考虑在粘贴之前或之后调整图像的大小。我想保持原始图像大小,因为我不确定调整大小是一个常数值。
  • 不幸的是,我对 vba/excel 实际如何粘贴图片来提供帮助知之甚少。祝你好运!

标签: vba pdf excel


【解决方案1】:

嗯,这很奇怪,不应该有所作为,但它确实如此,到目前为止似乎有效。

在上述工作流程中,将 EMF 粘贴到工作表后,如果我选择该图像,然后将其复制到新的图表表,则会保持原始大小以及图形的矢量性质。代码如下:

            ret = DPlot_Command(doc, "[CopyPicture()]")

            ' copy the clip board contents to a new temp worksheet (under the covers)

            'Hide the application
            Application.ScreenUpdating = False

            'Create a new temp worksheet
            Set ws = ActiveWorkbook.Sheets.Add(After:=Sheets(1))
            ws.Name = "Temp_Graph_DPJR"
            ws.Activate

            'Paste to the temp worksheet then select/copy that one
            ActiveSheet.Paste
            Selection.Copy


            'Create a new temp chart
            Set temp_chart = ActiveWorkbook.Charts.Add
            'temp_chart.Name = "Temp_Chart"

            'Set the linesytle of the border to none
            temp_chart.ChartArea.Border.LineStyle = xlNone

            'Paste the dplotjr graph to the chart sheet
            'We had to do this as this maintained the size of the graph
            'Dumb MS Excel worksheet

            temp_chart.Paste

            'Paste to the temp worksheet
            'ActiveSheet.Paste

            'Save as pdf from excel
            ActiveSheet.ExportAsFixedFormat _
                 Type:=xlTypePDF, _
                 filename:=pdf_filename, _
                 OpenAfterPublish:=False

这几乎就像 Excel 确定粘贴来自另一个应用程序并将位图粘贴到图表表,但如果它确定它来自自身,它会粘贴为矢量图像。这可能是一个可能会在未来消失的漏洞,但它似乎到目前为止有效。

哇!

【讨论】:

    猜你喜欢
    • 2018-11-15
    • 1970-01-01
    • 2017-09-03
    • 2017-11-25
    • 1970-01-01
    • 2016-10-25
    • 2023-04-10
    • 2017-11-28
    • 1970-01-01
    相关资源
    最近更新 更多