1,216
社区成员
发帖
与我相关
我的任务
分享
Private Sub CellPrint(s As String, x As Integer, y As Integer, h As Integer, w As Integer)
's 打印字串,x,y坐标点,h行高度,w列宽度
'默认字体为五号宋体
'字符串长度不定,格式大约为:
'"7月2日已批示,同意该同志去法国考察。需转陈秘书。"
'问题:要将内容打印在参数所固定的方格中,难题是字符串中有半角数字和半角标点,我不知道在哪个字符前折行?
'我的代码如下,但总不能对齐。特请教各位!
Dim a_st, i As Integer, n As Integer, l As Integer, j As Integer, k As Integer, lw As Integer
a_st = Split(s, vbCrLf)
n = UBound(a_st)
k = 0
lw = 6 '每行容纳字符数
For i = 0 To n
l = Len(a_st(i))
For j = 0 To l \ lw
Printer.CurrentX = x
Printer.CurrentY = y + k * h
Printer.Print Mid(a_st(i), j * lw + 1, lw)
k = k + 1
Next
Next
Exit Sub
Private Sub CellPrint(s As String, x As Integer, y As Integer, h As Integer, w As Integer)
Dim aLines() As String
Dim sText As String
Dim i As Long, k As Long
Dim jMax As Long, j1 As Long, j2 As Long
Dim lw As Long
k = 0
aLines = Split(s, vbCrLf)
For i = 0 To UBound(aLines)
sText = aLines(i)
jMax = Len(sText)
j1 = 1
While j1 <= jMax
For j2 = j1 + 1 To jMax
lw = Printer.TextWidth(Mid$(sText, j1, j2 - j1 + 1))
If lw > w Then Exit For
Next
Printer.CurrentX = x
Printer.CurrentY = y + k * h
Printer.Print Mid$(sText, j1, j2 - j1)
k = k + 1
j1 = j2
Wend
Next
End Sub