【问题标题】:VBA Word Shape location - top left cornerVBA Word Shape 位置 - 左上角
【发布时间】: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。

标签: vba ms-word qr-code shape


【解决方案1】:

Word SHAPE 对象必须锚定到 Range。该 Range 的页面位置决定了 Shape 将显示在哪个页面上。您无法将形状“锁定”到特定页面。

也就是说,可以规定 Shape 始终出现在锚定 Range 所在页面的同一位置。

如果您在文档编辑完成之前添加这样的形状,这总是很棘手,因为编辑可以将锚定段落移动到不同的页面。选择一个不太可能移动的段落作为锚点会有所帮助,例如页面上的第一段。

很久以前,我曾经写过一个宏,用于在打印或保存之前检查 Shapes 的页面位置。在插入和定位形状时,我给它一个包含页码的名称。在打印/保存之前,宏会检查形状名称中的页码与形状所在的页面。如果两者不匹配,请剪切形状并将其粘贴到正确页面上的段落(它会记住其位置设置)。

下面的代码示例演示了如何命名形状、将锚锁定到特定段落并将形状与页面的左上角齐平。

Sub ShapePosTopLeft()
    Dim doc As word.Document
    Dim shp As word.Shape
    Dim rng As word.Range

    Set doc = ActiveDocument
    Set rng = doc.GoTo(wdGoToPage, wdGoToRelative, Page)
    Set rng = rng.Paragraphs(1).Range
    Set shp = doc.Shapes.AddOLEControl(ClassType:="STROKESCRIBE.StrokeScribeCtrl.1", Anchor:=rng)


    With shp
        .Name = "Shape_Page" & Page
        .LockAnchor = True
        .RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
        .RelativeVerticalPosition = wdRelativeVerticalPositionPage
        .Left = 0
        .Top = 0
    End With
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 2018-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-08
    • 1970-01-01
    相关资源
    最近更新 更多