ArrangeForm.frm 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. '// 用户窗口初始化
  2. Private Sub UserForm_Initialize()
  3. ActiveDocument.Unit = cdrMillimeter
  4. Dim sr As ShapeRange
  5. Dim ls, hs, lj, hj, pw, ph As Double
  6. pw = ActiveDocument.Pages.First.SizeWidth
  7. ph = ActiveDocument.Pages.First.SizeHeight
  8. TextBox1.text = 2
  9. TextBox2.text = 5
  10. TextBox3.text = 0
  11. TextBox4.text = 0
  12. Set sr = ActiveSelectionRange
  13. If sr.Count > 0 Then
  14. ls = Int(sr.SizeWidth + 0.5)
  15. hs = Int(sr.SizeHeight + 0.5)
  16. Label_Size.Caption = "尺寸: " & ls & "×" & hs & "mm"
  17. lj = Int(pw / ls)
  18. hj = Int(ph / hs)
  19. Dim jh, jl, t As Double
  20. jl = Int(pw / hs)
  21. jh = Int(ph / ls)
  22. '// Debug.Print lj, hj, jl, jh
  23. If jh * jl > hj * lj Then
  24. lj = jl
  25. hj = jh
  26. If lj * ls > pw Or hj * hs > ph Then
  27. t = lj
  28. lj = hj
  29. hj = t
  30. End If
  31. End If
  32. TextBox1.text = lj
  33. TextBox2.text = hj
  34. End If
  35. End Sub
  36. Private Sub CommandButton1_Click()
  37. On Error GoTo ErrorHandler
  38. API.BeginOpt
  39. Dim ls, hs As Integer: Dim lj, hj As Double
  40. Dim matrix As Variant: Dim sr As ShapeRange
  41. ls = Val(TextBox1.text)
  42. hs = Val(TextBox2.text)
  43. lj = Val(TextBox3.text)
  44. hj = Val(TextBox4.text)
  45. matrix = Array(ls, hs, lj, hj)
  46. Set sr = ActiveSelectionRange
  47. If ls * hs = 0 Then Exit Sub
  48. If ls = 1 Or hs = 1 Then
  49. arrange_Clone_one matrix, sr
  50. GoTo ErrorHandler
  51. End If
  52. '// 代码运行时关闭窗口刷新
  53. ActiveDocument.BeginCommandGroup: Application.Optimization = True
  54. '// 拼版矩阵
  55. arrange_Clone matrix, sr
  56. Unload Me
  57. ErrorHandler:
  58. API.EndOpt
  59. End Sub
  60. '// 拼版矩阵 matrix = Array(ls, hs, lj, hj)
  61. Private Function arrange_Clone(matrix As Variant, sr As ShapeRange)
  62. ls = matrix(0): hs = matrix(1)
  63. lj = matrix(2): hj = matrix(3)
  64. x = sr.SizeWidth: Y = sr.SizeHeight
  65. Set s1 = sr '// Set s1 = sr.Clone
  66. '// StepAndRepeat 方法在范围内创建多个形状副本
  67. Set dup1 = s1.StepAndRepeat(ls - 1, x + lj, 0#)
  68. Set dup2 = ActiveDocument.CreateShapeRangeFromArray(dup1, s1).StepAndRepeat(hs - 1, 0#, -(Y + hj))
  69. '// s1.Delete
  70. End Function
  71. Private Function arrange_Clone_one(matrix As Variant, sr As ShapeRange)
  72. ls = matrix(0): hs = matrix(1)
  73. lj = matrix(2): hj = matrix(3)
  74. x = sr.SizeWidth: Y = sr.SizeHeight
  75. Set s1 = sr '// Set s1 = sr.Clone
  76. '// StepAndRepeat 方法在范围内创建多个形状副本
  77. If ls > 1 Then
  78. Set dup1 = s1.StepAndRepeat(ls - 1, x + lj, 0#)
  79. Else
  80. Set dup1 = s1
  81. End If
  82. If hs > 1 Then
  83. Set dup2 = ActiveDocument.CreateShapeRangeFromArray(dup1, s1).StepAndRepeat(hs - 1, 0#, -(Y + hj))
  84. End If
  85. '// s1.Delete
  86. End Function