vba 在给定的excel工作表中插入pdf文件作为图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20674082/
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-12 01:01:58  来源:igfitidea点击:

insert pdf file as image in given excel worksheet

imageexcelvbapdf

提问by Adrian Gornall

I have been trying to modify this script to take the pdf file and insert it as an image into the active worksheet. but it keeps creating a new workbook and inserting into the new book.

我一直在尝试修改此脚本以获取 pdf 文件并将其作为图像插入到活动工作表中。但它不断创建一个新的工作簿并插入到新书中。

Can someone help me modify this script so it inserts the pdf as an image to the worksheet called "report".

有人可以帮我修改此脚本,以便将 pdf 作为图像插入到名为“报告”的工作表中。

Sub insert_pdf_to_report()

子 insert_pdf_to_report()

Dim Xl Dim Wb Dim Ws Dim Ol

Dim Xl Dim Wb Dim Ws Dim Ol

Sheets("Report").Activate

Set Xl = CreateObject("Excel.Application")
Set Wb = Xl.Workbooks.Add
Set Ws = Wb.Worksheets.Add
Set Ol = Ws.OLEObjects.Add(, "C:\QGC_HSSE\template.pdf", True, False)

    With Ol
        .Left = Ws.Range("A1").Left
        .Height = Ws.Range("A1").Height
        .Width = Ws.Range("A1").Width
        .Top = Ws.Range("A1").Top
    End With

    Sheets("Report").Activate
Xl.Visible = True

End Sub

结束子

采纳答案by An Cong Tran

Can you try this?

你能试试这个吗?

Sheets("Report").Activate

Set Ws = ActiveWorkbook.Worksheets("Report")
Set Ol = Ws.OLEObjects.Add(, "/Users/tcan/Farewell-dinner.jpg", True, False)

...

The problem with your code is that these statements:

您的代码的问题在于这些语句:

Set Xl = CreateObject("Excel.Application")
Set Wb = Xl.Workbooks.Add
Set Ws = Wb.Worksheets.Add

create a new workbook and a worksheet inside the new WB.

在新的 WB 中创建一个新的工作簿和一个工作表。

I do not have excel to test this but I think my code may work as your expectation by removing the commands that create new workbook.

我没有 excel 来测试这个,但我认为我的代码可以通过删除创建新工作簿的命令来按您的期望工作。

Cheers.

干杯。