Advertisement
Guest User

批量插入图片批注

a guest
Apr 11th, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub 批量插入图片批注()
  2.  
  3. ' 【使用步骤】
  4.  
  5. ' 1. 在电子表格所在的文件夹中建立一个名为“图片目录”(注①)的文件夹(注②)
  6.  
  7. ' 2. 将待插入批注的图片(任意文件格式)放入“图片目录”
  8.  
  9. ' 3. 将各张图片的文件名(不包括扩展名)重命名为与对应的目标单元格的内容 相同 的字符串(注③)
  10.  
  11. ' 4. 选定全部的目标单元格
  12.  
  13. ' 5. 执行宏。(完成!)
  14.  
  15.  
  16. ' 【注】
  17.  
  18. ' ① 也可自定义 imgDir 的值为"图片目录"之外的其他名称:
  19.              imgDir = "图片目录"
  20.  
  21. ' ② 由于图片是内嵌地插入的,所以在执行宏之后可删除整个“图片目录”
  22.  
  23. ' ③ 若目标单元格包含不允许出现在文件名中的特殊字符怎么办?
  24. '    提示两种办法:
  25. '      1. 在执行宏之后更改单元格的内容,批注不受影响
  26. '      2. 批注属于单元格,所以能一起移动
  27.  
  28.  
  29. ' [VBA get image size]( https://social.msdn.microsoft.com/Forums/office/en-US/5f375529-a002-4312-a54b-b70d6d3eb6ae )
  30. Dim objShell As Object
  31. Dim objDir As Object
  32. Dim objFile As Object
  33. Dim objFileName, objFileMainName As String
  34.  
  35. fileDir = ThisWorkbook.Path & "\" & imgDir & "\"
  36. Set objShell = CreateObject("Shell.Application")
  37. Set objDir = objShell.Namespace(fileDir)
  38.  
  39. ' [(Rough prototype)]( www.wordlm.com/Excel/jqdq/6627.html )
  40. Dim MR As Range
  41. For Each MR In Selection
  42.   If Not IsEmpty(MR) Then
  43.     MR.Select
  44.     MR.ClearComments
  45.     MR.AddComment
  46.     MR.Comment.Visible = False
  47.     MR.Comment.Text Text:=""
  48.  
  49.     ' -------- 获取图片文件 --------
  50.    objFileMainName = fileDir & MR.Value
  51.  
  52.     ' [VBA open a file if only know part of the file name without extension name]( https://stackoverflow.com/a/2861006 )
  53.    objFileName = Dir(objFileMainName & ".*")
  54.  
  55.     ' [VBA check if file exists]( https://stackoverflow.com/a/33771924 )
  56.    If Dir(objFileName, vbDirectory) = "." Then
  57.       MsgBox "未找到指定文件。请修改图片的文件名或单元格的内容,使二者相同"
  58.       MR.ClearComments
  59.       Exit Sub
  60.     End If
  61.  
  62.     MR.Comment.Shape.Fill.UserPicture PictureFile:=fileDir & objFileName
  63.  
  64.     ' -------- 调整图片尺寸 --------
  65.    Set objFile = objDir.ParseName(objFileName)
  66.  
  67.     ' [VBA extract substrings in image attributes]( https://stackoverflow.com/a/46504821 )
  68.    size_ = objFile.ExtendedProperty("Dimensions")
  69.     size_delimiter = InStr(size_, "x")
  70.     width_ = Val(Mid(size_, 2, size_delimiter - 2))
  71.     height_ = Val(Mid(size_, size_delimiter + 2, Len(size_)))
  72.  
  73.     ' [VBA get screen resolution]( https://stackoverflow.com/a/41940087 )
  74.    'MsgBox width_ & " x " & height_ & vbCrLf & Application.UsableWidth & " x " & Application.UsableHeight
  75.  
  76.     Select Case True
  77.       Case width_ > Application.UsableWidth
  78.         height_ = height_ / width_ * Application.UsableWidth * 0.75
  79.         width_ = Application.UsableWidth * 0.75
  80.       Case height_ > Application.UsableHeight
  81.         width_ = width_ / height_ * Application.UsableHeight * 1.15
  82.         height_ = Application.UsableHeight * 1.15
  83.     End Select
  84.  
  85.     MR.Comment.Shape.Width = width_
  86.     MR.Comment.Shape.Height = height_
  87.  
  88.   End If
  89. Next
  90. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement