【问题标题】:Insert a rectangle at the cursor in the Outlook editor window在 Outlook 编辑器窗口中的光标处插入一个矩形
【发布时间】:2020-10-18 11:35:41
【问题描述】:

我正在尝试在 Outlook 编辑器窗口中的光标处插入一个矩形。下面的代码适用于 Word。

在 Outlook 中,oSel.Information(wdVerticalPositionRelativeToPage) 返回 -1,我相信这是 VBA 告诉我它在某种程度上未定义的方式。

在当前 Outlook 编辑器中,是否有其他方法可以获取光标相对于文档的垂直位置?

Public Sub addShape()
    Dim oShp As Word.Shape
    Dim oSel As Word.Selection
    
    Set oSel = Application.ActiveInspector.WordEditor.Application.Selection
    Set oShp = ActiveDocument.Shapes.addShape(msoShapeRectangle, getXCoord(oSel), getYCoord(oSel), 50, 25)
    
    With oShp
        .Fill.Visible = False
        .Line.Visible = True
        .Line.ForeColor.RGB = RGB(0, 176, 80)
        .Line.Weight = 2.25
    End With
    
    Set oShp = Nothing
    Set oSel = Nothing
End Sub

Private Function getXCoord(ByVal oSel As Word.Selection) As Double
    getXCoord = oSel.Information(wdHorizontalPositionRelativeToPage)
End Function

Private Function getYCoord(ByVal oSel As Word.Selection) As Double
    getYCoord = oSel.Information(wdVerticalPositionRelativeToPage)
End Function

【问题讨论】:

  • "如果选择或范围不在屏幕区域内,则返回 - 1" docs.microsoft.com/en-us/dotnet/api/…
  • 谢谢@TimWilliams,您知道文档中的“屏幕区域”是什么意思吗?在 Word 中,如果选择不在屏幕上,它会起作用。我已确保所选内容在 Outlook 编辑器中可见,但它也不起作用。
  • 对不起,我不知道,你说得对,我测试过,即使选择远离屏幕,它也能在 Word 中工作......

标签: vba outlook ms-word


【解决方案1】:

这里,你用就可以了

getYCoord = oSel.Range.Information(wdVerticalPositionRelativeToPage)

除非我在完整的 Word 应用程序中单独打开了一个文档,否则此行将失败

Set oShp = ActiveDocument.Shapes.addShape(msoShapeRectangle, getXCoord(oSel), getYCoord(oSel), 50, 25)

我认为你需要更多类似的东西

Set oShp = oSel.Application.ActiveDocument.Shapes.addShape(msoShapeRectangle, getXCoord(oSel), getYCoord(oSel), 50, 25)

或者也许从设置开始

Dim oWrd As Word.Application
Set oWrd = Application.ActiveInspector.WordEditor.Application
Set oSel = oWrd.Selection
Set oShp = oWrd.ActiveDocument.Shapes.addShape(msoShapeRectangle, getXCoord(oSel), getYCoord(oSel), 50, 25)

完成后使用以下内容。

Set oWrd = Nothing

【讨论】:

  • 我最终使用了getYCoord = oSel.Range.Information(wdVerticalPositionRelativeToPage)(获取选择的范围属性的信息,而不是直接获取选择的信息)以及您在末尾推荐的设置oWrd你的帖子。谢谢。
猜你喜欢
  • 2021-08-05
  • 2019-03-05
  • 1970-01-01
  • 1970-01-01
  • 2012-11-03
  • 2016-08-01
  • 2011-02-24
  • 1970-01-01
相关资源
最近更新 更多