1
1

CardsTools.bas 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. Attribute VB_Name = "CardsTools"
  2. Public Function MakeRectangleToPowerClip(w As Double, h As Double)
  3. Dim ssr As ShapeRange, s As Shape
  4. Dim cnt As Integer
  5. Dim i As Integer
  6. Set ssr = ActiveSelectionRange
  7. cnt = ssr.count
  8. If cnt = 0 Then Exit Function
  9. Dim jxsr As New ShapeRange
  10. ' 为每个选择的对象创建一个矩形
  11. For i = 1 To cnt
  12. Set s = Rectangle(w, h)
  13. jxsr.Add s
  14. Next i
  15. sr_Arrangement jxsr, 30
  16. jxsr.SetOutlineProperties 0# '// 没轮廓
  17. jxsr.Move 0, jxsr.SizeHeight + 30
  18. '// 批量调整尺寸和居中对齐
  19. For i = 1 To cnt
  20. SetShapeSize ssr(i), w, h
  21. ssr(i).CenterX = jxsr(i).CenterX
  22. ssr(i).CenterY = jxsr(i).CenterY
  23. jxsr(i).name = "powerclip_ok"
  24. ssr(i).AddToPowerClip jxsr(i)
  25. Next i
  26. jxsr.CreateSelection
  27. End Function
  28. '// 功能:解包当前选择的所有 PowerClip 对象
  29. Public Function PowerClip_ExtractShapes()
  30. Dim s As Shape
  31. Dim pwc As PowerClip ' 存储 PowerClip 对象
  32. For Each s In ActiveSelectionRange
  33. Set pwc = Nothing ' 每次循环重置变量
  34. ' 错误处理:尝试获取形状的 PowerClip 属性
  35. On Error Resume Next
  36. Set pwc = s.PowerClip ' 如果 s 不是 PowerClip,这里会出错
  37. On Error GoTo 0 ' 恢复正常错误处理
  38. ' 检查是否成功获取到 PowerClip 对象
  39. If Not pwc Is Nothing Then
  40. '// s.CreateSelection ' 选中当前 PowerClip 容器
  41. pwc.ExtractShapes ' 解包:将内容从容器中取出
  42. End If
  43. Next s
  44. End Function
  45. '// 建立矩形 Width x Height 单位 mm
  46. Private Function Rectangle(width As Double, Height As Double) As Shape
  47. Dim s As Shape
  48. Set s = ActiveLayer.CreateRectangle(0, 0, 0 + width, 0 - Height)
  49. s.Fill.ApplyNoFill
  50. Set Rectangle = s
  51. End Function
  52. '// 简洁版本:确保一边正好等于目标尺寸,另一边不小于指定最小值
  53. Private Function SetShapeSize(s As Shape, w As Double, h As Double)
  54. If s Is Nothing Then Exit Function
  55. Dim originalWidth As Double
  56. Dim originalHeight As Double
  57. Dim ratio As Double
  58. originalWidth = s.SizeWidth
  59. originalHeight = s.SizeHeight
  60. ratio = originalWidth / originalHeight
  61. Dim newWidth As Double
  62. Dim newHeight As Double
  63. '// 尝试方案1:宽固定为85,计算高
  64. newWidth = w
  65. newHeight = w / ratio
  66. '// 如果高太小(小于45),则改用方案2:高固定为54
  67. If newHeight < h Then
  68. newHeight = h
  69. newWidth = h * ratio
  70. '// 如果宽太小(小于85),则按比例放大直到宽等于85
  71. If newWidth < w Then
  72. newWidth = w
  73. newHeight = w / ratio
  74. End If
  75. End If
  76. '// 应用新尺寸
  77. s.SetSize newWidth, newHeight
  78. End Function
  79. Private Function sr_Arrangement(ssr As ShapeRange, Space_Width As Double)
  80. Dim s As Shape
  81. Dim cnt As Integer
  82. cnt = 1
  83. ActiveDocument.ReferencePoint = cdrTopLeft
  84. For Each s In ssr
  85. ActiveDocument.ReferencePoint = cdrTopLeft + cdrBottomTop
  86. If cnt > 1 Then s.SetPosition ssr(cnt - 1).RightX + Space_Width, ssr(cnt - 1).topY
  87. cnt = cnt + 1
  88. Next s
  89. End Function
  90. Public Function Save_CdrX4_File(CDRX4_FileName As String)
  91. Dim SaveOptions As StructSaveAsOptions
  92. Set SaveOptions = CreateStructSaveAsOptions
  93. With SaveOptions
  94. .EmbedVBAProject = True
  95. .Filter = cdrCDR
  96. .IncludeCMXData = False
  97. .Range = cdrAllPages
  98. .EmbedICCProfile = False
  99. .Version = cdrVersion14
  100. End With
  101. ActiveDocument.SaveAs CDRX4_FileName, SaveOptions
  102. End Function
  103. Private Function GetImageFiles(folderPath As String, fileList As Collection)
  104. Dim fileName As String
  105. Dim ext As String
  106. ' 确保路径以反斜杠结尾
  107. If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"
  108. ' 使用Dir函数获取第一个文件
  109. fileName = Dir(folderPath & "*.*")
  110. ' 遍历所有文件
  111. Do While fileName <> ""
  112. ' 获取文件扩展名
  113. ext = LCase(Right(fileName, Len(fileName) - InStrRev(fileName, ".")))
  114. ' 检查是否是图片文件
  115. Select Case ext
  116. Case "jpg", "jpeg", "png", "gif", "bmp", "tif", "tiff"
  117. fileList.Add folderPath & fileName
  118. End Select
  119. ' 获取下一个文件
  120. fileName = Dir
  121. Loop
  122. End Function
  123. Private Function MoveImageFile_Name(Optional ByVal sourceFileName As String, Optional ByVal destFileName As String) As Boolean
  124. On Error Resume Next
  125. ' 如果目标文件存在,直接添加后缀
  126. Dim fso As Object
  127. Set fso = CreateObject("Scripting.FileSystemObject")
  128. If fso.FileExists(destFileName) Then
  129. Dim i As Long
  130. i = 1
  131. Do While fso.FileExists(destFileName)
  132. destFileName = Replace(destFileName, ".", "_" & i & ".")
  133. i = i + 1
  134. Loop
  135. End If
  136. ' 移动文件
  137. Name sourceFileName As destFileName
  138. MoveImageFile_Name = (err.Number = 0)
  139. On Error GoTo 0
  140. End Function
  141. Public Function Import_Images()
  142. Dim folderPath As String
  143. Dim backtupPath As String
  144. Dim fileList As New Collection
  145. Dim sr As New ShapeRange
  146. ' 设置文件夹路径
  147. folderPath = "D:\Cards"
  148. backtupPath = "D:\Cards\BACKUP"
  149. Call GetImageFiles(folderPath, fileList)
  150. ' 批量导入图片
  151. Dim f As Variant
  152. For Each f In fileList
  153. ActiveDocument.ClearSelection
  154. ActiveLayer.Import f
  155. sr.Add ActiveSelection
  156. Next f
  157. sr.CreateSelection
  158. ' 移动图片到备份文件夹
  159. Dim sourceFileName As String
  160. Dim dstFileName As String
  161. For Each f In fileList
  162. sourceFileName = f
  163. desFileName = Replace(sourceFileName, "D:\Cards", "D:\Cards\BACKUP")
  164. MoveImageFile_Name sourceFileName, desFileName
  165. Next f
  166. End Function
  167. Public Function Images2NewDoc()
  168. Dim doc As Document
  169. Set doc = CreateDocument()
  170. doc.Unit = cdrMillimeter
  171. Call Import_Images
  172. End Function