【发布时间】:2018-02-16 09:10:48
【问题描述】:
我根据 Stroke Scribe ActiveX 对象的生产者提供的示例编写了一个小函数。这是一个插件,它允许我们通过 VBA 宏在 Microsoft Word 中创建一个 QR 码对象。
问题在于设置形状位置
shp.Left = 0 + LeftMargin
shp.Top = 0 + TopMargin
我想把这个形状(二维码)放在最左上角的特定页面上。但有时形状会跳转到上一页(底部)或其他位置(垂直中心)。
你能帮我识别一个问题并修复它,以便每次都能在左上角找到形状对象吗?
代码:
Sub QRCodeGenerator(SOP, BookmarkID, Page, TopMargin, LeftMargin)
Dim doc As Document
Set doc = Application.ActiveDocument
For Each sh In doc.Shapes
If sh.Type = msoOLEControlObject Then
If sh.OLEFormat.ProgID = "STROKESCRIBE.StrokeScribeCtrl.1" Then
sh.Delete
End If
End If
Next
With doc.PageSetup
usable_w = .PageWidth
usable_h = .PageHeight
End With
Dim pg As Range
Set pg = doc.GoTo(wdGoToPage, wdGoToRelative, Page)
Dim shp As Shape
Set shp = doc.Shapes.AddOLEControl(ClassType:="STROKESCRIBE.StrokeScribeCtrl.1", Anchor:=pg)
Dim sMyString As String
sMyString = ActiveDocument.Bookmarks(BookmarkID).Range.Text
sMyString = Replace(sMyString, "FORMTEXT ", "")
shp.LockAspectRatio = msoFalse
shp.Height = InchesToPoints(0.6)
shp.Width = shp.Height
shp.Left = 0 + LeftMargin
shp.Top = 0 + TopMargin ' // usable_h - shp.Height * 3 + TopMargin
Dim ss As StrokeScribe
Set ss = shp.OLEFormat.Object
ss.Alphabet = QRCODE 'StrokeScribe will draw a QR code picture
ss.Text = SOP & ";" & sMyString 'Any text you want to encode in the barcode
ss.QrECL = H 'Changes the default error correction level. This can be omitted
ss.QrMinVersion = 3 'Specifies the minimum barcode size. This can be omitted
ss.FontColor = RGB(0, 0, 0)
' ss.UTF8 = True 'Enable this, if you want to encode national characters for smartphones
If ss.Error Then
MsgBox ss.ErrorDescription
End If
End Sub
【问题讨论】:
-
1.
shp.Top = usable_h - shp.Height * 3 + TopMargin将条形码放在页面底部。如果将其更改为shp.Top = shp.Height * 3 + TopMargin会怎样? 2. 有没有什么位置可以稳定粘上条码或者跳出其他位置? -
您好 AcsErno,感谢您的评论。
shp.Top = usable_h - shp.Height * 3 + TopMargin是我将对象放在页面底部的“测试”公式,但仍然不起作用:/ -
要记住的是,Word 中并不真正存在“页面”。页面是动态创建的。更改文档和分页也会更改。通过定义形状
RelativeToPage的位置,您承诺提供一个稳定的文档,其中“页面”不会改变。换句话说,一旦您完成测试并且不再对文档的其余部分进行更改,您的问题很可能会消失。另一方面,考虑相对于段落锚定形状,这样更容易控制,甚至可以将其设为 InlineShape。