FAQ TBarCode OCX
通过 Excel VBA 打印时:条码未更新
通过 Excel VBA 打印时:条码未更新
您更改了链接单元格或条码控件的内容,然后直接在 VBA 中打印。但您发现条码控件中仍显示旧值。原因是 Excel 在打印前不会向 ActiveX 控件发送重绘命令(如果是从 VBA 内部执行打印操作)。这是 Excel 中的一个缺陷。
有一种变通方法可以强制重绘对象:您需要在 VBA 中以编程方式逐一访问每个条码 ActiveX 控件,并在打印前更改其尺寸,这样 Excel 就会重新绘制它。
'redraw a single barcode object by addressing it through its instance name Dim origValue origValue = TBarCode101.Width TBarCode101.Width = origValue + 1 TBarCode101.Width = origValue
' redraw all ActiveX Controls on the current sheet
Sub UpdateOLEControls()
Dim myShape As shape
Dim counter As Integer
Dim origWidth As Single
For counter = 1 To ActiveSheet.Shapes.Count
Set myShape = ActiveSheet.Shapes(counter)
If (myShape.Type = msoOLEControlObject) Then
' resize => force redraw
origWidth = myShape.Width
myShape.Width = origWidth + 1
myShape.Width = origWidth
End If
Next counter
End Sub