简体   繁体   English

使用 VBA 宏打开图片链接

[英]Using VBA Macro To open Link to picture

My goal of this program is to open a picture in a folder by selecting a cells value in the active row which im using the line to do.我的这个程序的目标是通过在活动行中选择一个单元格值来打开文件夹中的图片,我使用该行来做。

picture = Cells(ActiveRow, 6).Value

I am getting a Run time error '1004' Application-defined or object defined error我收到运行时错误“1004”应用程序定义或对象定义错误

Here is my full code这是我的完整代码

Sub Picture_Click()

   Sheets("Master").Unprotect Password:="Conti1"
   Sheets("Records").Unprotect Password:="Conti1"

   Dim picture As String
   Dim ActiveRow As Long


   Worksheets("Master").Activate

        ActiveRow = Rows(ActiveCell.Row).Select
        picture = Cells(ActiveRow, 6).Value

ChDir"P:\926_TM\03_LocalExchange\Tracking_and_Labeling\LabEquipment\pictures"

    Workbooks.Open picture

End Sub

I am trying to use the value from the selected cell to be the name of the picture file in the selected folder, the picture I would like to open.我正在尝试使用所选单元格中的值作为所选文件夹中图片文件的名称,即我要打开的图片。

also I am getting a value of -1 for ActiveRow我也得到了 ActiveRow 的值 -1

Any help would be great!任何帮助都会很棒!

ActiveRow is a Long, so you cannot select a row and say that is a number. ActiveRow是一个Long,因此您不能选择一行并说这是一个数字。 You would simply write 你只要写

ActiveRow = ActiveCell.Row

In the immediate window, if you write ?Rows(ActiveCell.Row).Select, it will return a Boolean Value, not a number. 在立即窗口中,如果您编写?Rows(ActiveCell.Row).Select,它将返回一个布尔值,而不是数字。

Also to open a file you should use the FollowHyperlink method: 同样要打开文件,您应该使用FollowHyperlink方法:

from msdn: 来自msdn:

ActiveWorkbook.FollowHyperlink Address:="http://example.microsoft.com"

Your link would replace the one in the example of course. 当然,您的链接将替换示例中的链接。

picture = "P:\926_TM\03_LocalExchange\Tracking_and_Labeling\LabEquipment\pictures\" & Cells(ActiveRow, 6).Value
ActiveWorkbook.FollowHyperlink Address:=picture

You can't open a picture with the Workbooks.Open method, as that method is exclusively for opening workbooks. 您无法使用Workbooks.Open方法打开图片,因为该方法专门用于打开工作簿。

One option is to use a Shell command to open the picture. 一种选择是使用Shell命令打开图片。 In this example, I will open a picture located on the the Pictures folder named Test.png 在此示例中,我将打开名为Test.png的Pictures文件夹中的图片。

Private Sub OpenPic()
        Shell "mspaint.exe C:\Users\USERNAME\Pictures\Test.png"
End Sub

This will open the picture in paint. 这将打开涂料中的图片。

Adapting this method to your code: 使此方法适应您的代码:

Sub Picture_Click()

   Sheets("Master").Unprotect Password:="Conti1"
   Sheets("Records").Unprotect Password:="Conti1"

   Dim picture As String
   Dim ActiveRow As Long


   Worksheets("Master").Activate

   ActiveRow = ActiveCell.Row
   picture = Cells(ActiveRow, 6).Value

   Shell "mspaint.exe P:\926_TM\03_LocalExchange\Tracking_and_Labeling\LabEquipment\pictures\" & picture

End Sub

This assumes that the value in Activerow, 6 includes the file extension. 假定Activerow, 6中的值包含文件扩展名。

As an aside: I would add that you don't need to define ActiveRow at all and could just use it as-is. 顺便说一句:我要补充一点,您根本不需要定义ActiveRow,而可以按原样使用它。

So this: 所以这:

ActiveRow = ActiveCell.Row
picture = Cells(ActiveRow, 6).Value

Can be rewritten as: 可以改写为:

picture = Cells(ActiveCell.Row, 6).Value

And you can omit the ActiveRow variable altogether. 您可以完全省略ActiveRow变量。

This answer will help you to open in default photo viewer .这个答案将帮助您在默认照片查看器中打开 I tried,我试过了,

Sub image_opener()
    Dim photo_path As String
    photo_path = Worksheets("Sheet1").Range(Selection.Address).Value
    PID = Shell(photo_path, vbNormalFocus)
End Sub

but the above code didn't work for me.但上面的代码对我不起作用。 I think the McAfee in my PC is preventing the excel from running that shell command.我认为我 PC 中的 McAfee 阻止了 excel 运行该 shell 命令。

So, I created a openImg.bat file containing所以,我创建了一个 openImg.bat 文件,其中包含

%1

%1 basically takes the input from the user. %1 基本上接受用户的输入。 Normally we can open image using this openImg.bat by,通常我们可以使用这个 openImg.bat 打开图像,

openImg.bat path_of_image/image.jpg

So, I edited the VBA accordingly所以,我相应地编辑了 VBA

Sub image_opener()
    Dim photo_path As String
    photo_path = Worksheets("Sheet1").Range(Selection.Address).Value
    PID = Shell("bat_file_path\openImg.bat " & photo_path, vbNormalFocus)
End Sub

photo_path = "image_path/image.jpg" . photo_path = "image_path/image.jpg" . Finally I setup a shortcut key for the macro from Developer>Macros>Options>Shortcut Key .最后,我从Developer>Macros>Options>Shortcut Key为宏设置了一个快捷键。 The above worked for me.以上对我有用。

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

相关问题 在Excel中使用VBA时将图片另存为图片而不是图片链接 - Save picture as a picture instead of picture link when using VBA in Excel 如何使用VBA宏在Powerpoint幻灯片中“重置图片”? - How to “Reset Picture” in Power point Slide using VBA macro? 使用 VBA 更改图片 - Using VBA to change Picture 无法使用链接在电子表格中插入图片 - Can't insert picture in a spreadsheet using a link 保存图片并使用Java和Tomcat获取链接 - Save picture and get a link using Java and Tomcat 使用PIL打开图片但颜色错误 - Using PIL open a picture but got wrong color 在单元格中插入图片的宏会创建一个链接,导致“无法显示链接的图像”错误 - Macro to insert picture in cell creates a link which results in 'The Linked Image cannot be displayed' error 使用 Word VBA 将图片重新着色为黑白 75% - Recolor picture to black and white 75% using Word VBA 如何使用VBA将边框应用于在Excel工作表中作为图片元素(Dim var as Picture)插入的图像? - How to apply border to images inserted as Picture elements (Dim var as Picture) in Excel sheet using VBA? 图片的特定部分可以用作使用jQuery或Javascript的链接吗? - Can a specific part of a picture be used as a link using jQuery or Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM