Shapes.AddPicture仅添加最后一个图像 - 适用于Mac的Excel VBA宏

时间:2014-09-17 18:52:25

标签: excel macos vba excel-vba

我有一个excel宏,它在服务器的excel表中填充图像。 虽然这对于Windows来说是直截了当的,但对于Mac,我使用" curl"来获取图像。命令,将其复制到本地文件夹(如果imaage存在),并使用Shapes.AddPicture插入图像,然后删除本地图像。对于单张图片,这很有效。但是当我必须选择几列并添加多个图像时,它只会添加最后一个图像。有人能告诉我为什么以及如何解决这个问题? 以下是相关代码    =====================================

  For Each objCell In objSelectedRange

           If objCell.Value <> "" Then
                UserPath = UserPath & "/" & objCell.Value & ImageType  ' Add prefix and filename
                ImagePath = ImageFolder & objCell.Value & ImageType
                FTPpathtofile = ImagePath
                scriptToCheck = "tell application " & Chr(34) & "Finder" & Chr(34) & " to do shell script " & Chr(34) & "curl  " & FTPpathtofile & Chr(34)
                retCode = MacScript(scriptToCheck)
                str = (InStr(retCode, "404"))
                If (str = 0) Then
                    scriptToRun = "tell application " & Chr(34) & "Finder" & Chr(34) & " to do shell script " & Chr(34) & "curl  " & FTPpathtofile & " >" & UserPath & Chr(34)
                    MacScript (scriptToRun)
                    With ActiveSheet
                         PathtoHD = MacScript("path to startup disk as string")
                         localImagePath = Replace(UserPath, "/", ":")
                         localImagePath = PathtoHD & localImagePath
                         Set objImgMac = .Shapes.AddPicture(localImagePath, True, True, objCell.Left, objCell.Top, 30, 60)

                        If adjustCell Then
                            objCell.RowHeight = 78
                            If objCell.ColumnWidth < 12 Then
                                objCell.ColumnWidth = 12
                            End If
                        End If

                        If vAlign = "Top" Then
                            objCell.VerticalAlignment = xlVAlignTop
                            objImgMac.Top = objCell.Top + 15
                            objImgMac.Left = objCell.Left + 5
                        Else
                            If vAlign = "Bottom" Then
                                objCell.VerticalAlignment = xlVAlignBottom
                                objImgMac.Top = objCell.Top + 5
                                objImgMac.Left = objCell.Left + 5
                            End If
                        End If
                    End With

            'delete file from the folder
            DeleteFileOnMac (localImagePath)

            End If
            End If

  Next objCell

=============================

1 个答案:

答案 0 :(得分:0)

你有

 UserPath = UserPath & "/" & objCell.Value & ImageType 
在您的循环中

,但您似乎无法在迭代之间重置UserPath的值,因此它只会累积内容。