【问题标题】:Word 2007 VBA pause macro for user to manually select cursor position or range with the mouseWord 2007 VBA 暂停宏,供用户使用鼠标手动选择光标位置或范围
【发布时间】:2020-01-28 18:35:17
【问题描述】:

在 Word 2007 中,我有许多宏作用于选定范围(例如表格中的一行,或随机选择区域(不在表格中)),仅是活动文档的一部分。(它们执行多项任务在同一选择上 - 例如格式化指定的字符串,用制表符替换多个空格,调整图像大小等)

我尝试编写一个输入框程序,允许用户选择范围或检查和修改预先选择的范围(如果他们在执行宏之前忘记这样做,或者需要在宏期间选择特定区域.

输入框好像没有这个功能?

谁能帮忙解决这个问题?

我已经用 InputBox 在 excel 中完成了它,但代码在 Word 中不起作用,并且在 google 上找不到任何有用的东西......

这是 Excel 中的代码,它执行我想要在 word 中执行的操作:

     'input box prompt+pause to select range
    Set User_RangeSelection = Application.InputBox("select cells to move, with the mouse", Default:=Selection.Address, Type:=8)
        If User_RangeSelection Is Nothing Then 'if cancel is clicked - otherwise selects existing cell selection
            MsgBox "No Selection made, exiting."
'            Exit Sub 'cancels the whole macro after this routine!
        End If
'---------------------------------------------------
    User_SelectedRange = User_RangeSelection.Address(False, False) 'name the selection as a range
    Range(User_SelectedRange).Select 'selects range IN SHEET; OR enter single commands here
'------------------------- Code to execute on/with selection

word 的代码/语法显然完全不同 - 尝试了许多更改,但没有一个得到过去的弹出错误代码。

“TYPE:=8”“未找到命名参数”

删除上述行后,输入框显示实际所选字符串的 1 行位,不显示文档中突出显示的区域,并且不允许单击文档

在“预期:语句结束”末尾添加“as Range”

“地址”-“未找到方法或数据成员”

“范围”“未定义子或函数”

【问题讨论】:

    标签: vba ms-word user-input office-2007


    【解决方案1】:

    确实,Word 实现 VBA InputBox 的方式与 Excel 不同。 Excel是“一张,大表”; Word 实际上是“只是文本”,在需要时在屏幕上格式化为看起来像一个表格。这就是行为背后的“原因”,FWIW。

    你应该能够做类似的事情,但需要更多的工作。我不能给你一个确切的解决方案,因为确切的要求并不明确,而且有点“太宽泛”。但是为了让您开始基本的“指定范围”部分......

    (注意:我是在移动设备上从内存中输入的,因此可能存在语法错误)

    Dim CellRef as String, RowIndex as Variant, ColIndex as Variant
    Dim CommaPos as Long
    Dim Tbl as Word.Table, Cel as Word.Cell, Dim rng as Word.Range
    
    If Selection.Information(wdWithinTable) Then
      CommaPos = Instr(CellRef, ",")
      CellRef = InputBox("Type in the cell row and column (1,3)")
      If CommaPos <> 0 Then
        RowIndex = Left(CellRef, CommaPos-1)
        ColIndex = Mid(CellRef, CommaPos + 1)
        Set tbl = Selection.Tables(1)
        Set Cel = tbl.Cell(rowIndex, colIndex)
        Set rng = cel.Range
      Else
        MsgBox "Please enter a correct cell reference")
      End If
    Else
      MsgBox "Please click in the table, first"
    End If
    

    【讨论】:

    • 我已经澄清了这个问题,它与表格无关,它是关于在没有选择或选择错误的情况下暂停用户选择的宏。无论如何谢谢:)
    • 好吧,这根本不可能@Piecevcake 宏必须停止并重新启动。您可以获得的最接近的是非模态用户表单(这意味着用户可以在用户表单打开时在文档中工作)。然后单击用户窗体中的按钮以运行“继续”宏。
    猜你喜欢
    • 2013-05-24
    • 2011-04-10
    • 1970-01-01
    • 2013-08-28
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多