Excel VBA addPicture通过图像的路径 [英] Excel VBA addPicture by path to image

查看:91
本文介绍了Excel VBA addPicture通过图像的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Windows上编写了VBA宏.此宏根据单元格的值将图片添加到我的excel工作表中,例如'image.png'.这些图像与我的Excel工作簿位于同一目录中.VBA将获取工作簿的路径,并使用它来查找单元格中指定的图像.这在Windows上可以使用,但是在Mac上则不能使用.宏返回错误(1004),表明找不到指定的文件.

I've written an VBA Macro on windows. This Macro adds pictures into my excel sheet based on the value of a cell e.g. 'image.png'. These images are located in the same directory as my Excel workbook. VBA will get the path to the workbook and use it to find the image specified in the cell. This works on windows, however it does not work on mac. The macro returns an error (1004) saying it can't find the specified file.

Sub InsertImage()
    Dim useless As Double
    Dim clTop As Double
    For Each c In ActiveSheet.Range("C3:C200").Cells
       If c.Value = "" Then
           useless = 1
       Else
           Set cl = Range(c, c.Offset(0, 1))
           clTop = cl.Top
           ActiveSheet.Shapes.AddPicture _
           Application.ActiveWorkbook.Path & "\" & c.Value, _
           True, True, 500, clTop, 140, 140
       End If
    Next
    End Sub

更新:

进行了更多测试:当我首先手动添加要添加的所有图片,然后立即将其全部删除,然后运行完全相同的Macro时,所有图片都可以完美导入.这可能是Excel for Mac中的错误吗?

Did some more testing: When I first manually add all the pictures that I want to add, then immediatly delete them all, then run exactly the same Macro, all the pictures are imported perfectly fine. Could this be a bug in Excel for Mac?

推荐答案

在此处留下答案,以防万一有人在用VBA将多个图片插入Mac版Excel时将头撞在墙上时仍能找到此页面的情况(像我一样)).我在此站点上找到了GrantAccessToMultipleFiles帮助,终于为我修复了它: https://warwick.ac.uk/fac/sci/systemsbiology/staff/dyer/software/excelvbafileopen/

Leaving an answer here in case anyone still finds this page when banging their heads against a wall trying to insert multiple pictures into Excel for Mac, using VBA (like me). I found the GrantAccessToMultipleFiles help on this site here finally fixed it for me: https://warwick.ac.uk/fac/sci/systemsbiology/staff/dyer/software/excelvbafileopen/

这篇关于Excel VBA addPicture通过图像的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆