VBA实例6 CorelDraw 批量生成设备位号、连续编号

问题引入

制作可燃气体检测报警系统气体探头(即气体检测报警仪)位号标签

思路

  1. 创建艺术文本对象,填入字符。连续编号用遍历循环即可,Format函数加前导零。亦可通过读取Office对象(Excel)中的内容提取需要填入的位号,时间有限,本文不做探讨。后续在《VBA实例4 Excel隐患排查治理台账》详细讲解。
  2. 根据组合所在行列调整水平位置和垂直位置。
  3. 同样的思路也可用于反应釜位号、仪表位号等批量创建。

效果

在这里插入图片描述
懒得插视频了,视频审核万把年……

参数

主要用到Layer.CreateArtisticText方法

Function CreateArtisticText(Left As Double, Bottom As Double, Text As String, 
	[LanguageID As cdrTextLanguage = cdrLanguageNone], 
	[CharSet As cdrTextCharSet = cdrCharSetMixed], [Font As String], 
	[Size As Single], 
	[Bold As cdrTriState = cdrUndefined], 
	[Italic As cdrTriState = cdrUndefined], 
	[Underline As cdrFontLine = cdrMixedFontLine], 
	[Alignment As cdrAlignment = cdrMixedAlignment]) As Shape
VGCore.Layer 的成员
Creates artistic text on a layer
参数描述默认值
Left指定左边水平位置默认值为0
Bottom指定底部垂直位置默认值为0
Text指定艺术文本的内容需填入的文本内容
LanguageID指定的语言可选,默认值为cdrLanguageNone(0)
CharSet指定字符集。可选,默认值为cdrCharSetMixed(-1)
Font指定字体可选,CDR默认字体
Size指定字体大小可选,默认值为0
Bold指定是否应用粗体可选,默认值为cdrUndefined(-2)
Italic指定是否应用斜体可选,默认值为cdrUndefined(-2)
Underline指定要应用的下划线可选,默认值为cdrMixedFontLine(7)
Alignment指定对齐可选,默认值为cdrMixedAlignment(6)

实现

Function drawOne(x0 As Double, y0 As Double, i As String) As Shape
    Dim s1 As Shape, s2 As Shape, s3 As Shape, s4 As Shape
    Dim cm As Double
    cm = 1 / 2.54
    
    Set s1 = Application.ActiveLayer.CreateArtisticText(x0, y0 + 59.2 * cm, "100", _
        Font:="Times New Roman", Size:=24, Bold:=cdrTrue)
    Set s2 = Application.ActiveLayer.CreateArtisticText(x0, y0 + 55.7 * cm, "气体报警器", _
        Font:="SimHei", Size:=30, Bold:=cdrTrue)
    Set s3 = Application.ActiveLayer.CreateArtisticText(x0, y0 + 52.2 * cm, i, _
        Font:="Times New Roman", Size:=24, Bold:=cdrTrue)
    
    Set s4 = Application.ActiveLayer.CreateRectangle(x0, y0 + 60 * cm, x0 + 2.5 * cm, y0 + 52 * cm)
    
    Application.ActiveDocument.CreateShapeRangeFromArray(s1, s2, s3, s4).AlignAndDistribute 3, 0, 0, 0, False, 2
    
    Set drawOne = s2
End Function

Sub draw_one()
    Dim s2 As Shape, arr(), count As Integer, shp As Shape
    
    Set s2 = drawOne(0, 0, "01")
    ReDim Preserve arr(count)
    arr(count) = s2.ZOrder
    
    For Each shp In ThisDocument.ActiveLayer.Shapes
        Debug.Print shp.StaticID
        Debug.Print shp.ZOrder
        ThisDocument.ActiveLayer.Shapes(shp.ZOrder).CreateSelection
    Next shp
    
    ActiveLayer.CreateArtisticText Left
    
    ThisDocument.ActiveLayer.Shapes.All.CreateSelection
End Sub

Sub drawMore()
    Dim s2 As Shape, arr(), count As Integer, shp As Shape
    Dim idx, curRow, curCol
    Dim x As Double, y As Double, i As String, cm As Double
    Dim startTime As Single, endTime As Single
    startTime = Timer
    
    cm = 1 / 2.54
    For idx = 1 To 40
        x = curCol * 5 * cm
        y = curRow * -10 * cm
        i = Format(idx, "00")
        Debug.Print i
        
        Set s2 = drawOne(x, y, i)
        ReDim Preserve arr(count)
        Set arr(count) = s2
        count = count + 1
        
        If (idx Mod 10) = 0 Then
            curRow = curRow + 1
            curCol = 0
        Else
            curCol = curCol + 1
        End If
    Next idx
    
    ThisDocument.CreateShapeRangeFromArray(arr).CreateSelection
    
    endTime = Timer - startTime
    tempString = "Create all shapes successful." & vbCrLf & _
        "It takes " & Format(Timer - startTime, "0.000") & " seconds."
    
    ThisDocument.ActiveWindow.ActiveView.ToFitAllObjects
        
    MsgBox tempString, Title:=Now()
    
'    Call deleteAll
End Sub

Sub deleteAll()
    ThisDocument.ActiveLayer.Shapes.All.CreateSelection
    ThisDocument.Selection.Delete
End Sub
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值