简体   繁体   中英

Excel vba emf pasted to worksheet print/pdf wrong size

I'm taking a enhanced meta file generated from another application (DPlot Jr) copying to the clipboard, and pasting it to an Excel(2007) worksheet. From there i use vba to convert that file to a pdf. This is the code i have to do that:

            ' 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

I discovered that, the image that gets created in the PDF is slightly larger than the actual size of the graphic. For example, the width of the graphic should be 2.65". The size of the graph in the PDF is 2.816, so about .166" or 1 Pica, appears to be added.

While i could, and may have to, just decrease the size of the image initially by .166" that seems kind of hacky and I'd just like to have the image's original size to come over.

I discovered that if i paste the image to a Chartsheet, the size IS maintained, but the image becomes a bitmap on teh Chartsheet page.

When i create the pdf, i have all the correct settings. I have no margins, actual size, etc.

Has anyone else seen this? Can anyone help? I need to have the image as a pdf.

Thanks for any help!

Russ

Well, This is strange, and shouldn't make a difference, but it does, so far appear to work.

In the above workflow, after pasting the EMF to a worksheet, if i select that image THEN copy that to a new Chartsheet, the original size is maintained, as well as the vector nature of the graphic. Here is the code:

            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

It is almost as though Excel determines that the paste is from another app and pastes a bitmap to the chartsheet, but if it determines that it is from itself, it pastes as a vector image. This maybe a loophole that might go away in the future, but it seems to work so far.

fwiw!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM