如何在Excel模板中定义图像文件的路径

时间:2018-11-06 19:20:25

标签: excel vb.net path

我有一个Excel 2016宏,该宏将图片放置在工作簿所在的文件夹的子文件夹中。它使用此代码选择位于子文件夹\ images中的特定文件名:

imageFilename = "Double_Full_Float_Horizontal" ' Insert the filename of the image file to use (without extension)
                                               ' Code will use the file with that name in the image subfolder folder
                                               ' The extension is assumed to be png
                                               ' For a different extension, modify the code or change it so that you write the name with the extensinon
Set pic_one = .Pictures.Insert(Application.ThisWorkbook.Path & "\images\" & imageFilename & ".png")

在工作簿中运行宏时,此方法工作正常,但我需要将工作簿转换为xltm模板。当我这样做时,我从模板中打开了一个新的工作簿,此时宏将失败,并显示以下错误:

  

运行时错误'1004':无法获取Picture类的Insert属性

即使图像文件夹是模板存储位置的子文件夹,也是如此。我想我可以通过某种方式定义图像文件夹所在的绝对路径来解决此问题,但这会带来问题,因为我不能保证此位置将保持不变。感谢您解决问题的任何提示。

0 个答案:

没有答案