简体   繁体   English

无法使用vba将Windows Pictures文件夹中的图像加载到Excel文件中

[英]Cannot load image from the Windows Pictures folder into Excel file using vba

I have a vba macro to add an image into the Excel file. 我有一个vba宏,可以将图像添加到Excel文件中。 The main code to add the file is this... 添加文件的主要代码是这样...

Dim s As Shape

Set s = Application.ActiveSheet.Shapes.AddPicture(strFilename, msoFalse, msoCTrue, lngLeft, lngTop, -1, -1)

With s
    .Height = 60
    .Locked = True
End With

lngLeft and lngTop are coordinates set earlier in the code. lngLeft和lngTop是代码前面设置的坐标。 The target image file is selected using Application.FileDialog(msoFileDialogFilePicker) 使用Application.FileDialog(msoFileDialogFilePicker)选择目标图像文件

strFilename is filled using the following code... strFilename使用以下代码填充...

Dim fd As FileDialog
Dim objFile As Variant

Set fd = Application.FileDialog(msoFileDialogFilePicker)

With fd
    .ButtonName = "Select"
    .AllowMultiSelect = False
    .Title = "Choose a logo graphic ..."
    .Filters.Add "Images", "*.gif; *.jpg; *.png; *.jpeg", 1
    .FilterIndex = 2
    .InitialView = msoFileDialogViewDetails
    .Show

For Each objFile In .SelectedItems
    strFilename = Dir(objFile, vbNormal)
Next objFile

Problem: Everything works perfectly on my system and my colleagues and another random system. 问题:在我的系统,我的同事和另一个随机系统上,一切都正常运行。 However, when our client, for whom it is meant, uses it, he cannot select an image from the Windows' Pictures folder. 但是,当我们的客户(其含义是)使用它时,他无法从Windows的“图片”文件夹中选择图像。 He can select it from any other folder though. 他可以从其他任何文件夹中选择它。 He gets "Run-time error '1004': The specified file wasn't found." 他得到“运行时错误'1004':找不到指定的文件”。

All systems used in development, and testing are Windows 10 and the client is using Windows 7. Also, we are using the Office 2016 while he is using 2010. 开发和测试中使用的所有系统都是Windows 10,客户端使用Windows7。此外,我们使用Office 2016时使用的是Office 2016。

Can anyone help with what could be the reason for this problem and a workaround? 任何人都可以帮助解决此问题的原因和解决方法吗?

I found a workaround that seems to do the job. 我发现一种解决方法似乎可以完成这项工作。 The problem was with this piece of code 问题出在这段代码上

For Each objFile In .SelectedItems
    strFilename = Dir(objFile, vbNormal)
Next objFile

When I added a messagebox, I found that strFilename has only file name (eg: myimage.jpg) without the full path. 当我添加一个消息框时,我发现strFilename仅具有文件名(例如:myimage.jpg),而没有完整路径。 This doesn't seem to affect its functioning in Office 2016 on Windows 10. But for some reason, either Windows 7 or Office 2010 cannot work with it. 这似乎并不影响其在Windows 10上的Office 2016中的功能。但是由于某些原因,Windows 7或Office 2010均无法使用它。 So, I changed the way file path+file name string is generated to this and it works fine. 因此,我更改了为此生成文件路径+文件名字符串的方式,并且效果很好。

strFilename = .SelectedItems(1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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