Word 2007 VBA pause macro for user to manually select cursor position or range with the mouse

576 Views Asked by At

I have many macros which act on a selected range (eg a row in a table, or a random selection area (not in a table)), only part of the active document, in Word 2007. (They perform multiple tasks on the same selection - eg format specified strings, replace multiple spaces with tabs, resize images etc)

I have tried to write an inputbox procedure which allows the user to select the range or check and modify the pre-selected range (if they forgot to do so before executing the macro, or need to select a specific area during a macro.

It is looking like the inputbox doesn't have that capability in word?

Can anyone help with how this can be achieved?

I've done it in excel with InputBox but the code doesn't work in Word, and couldn't find anything helpful on google...

This is the code in Excel which does what I want to do in 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

The code/syntax for word is clearly completely different - tried many changes but none have got past popup error codes.

"TYPE:=8" "NAMED ARGUMENT NOT FOUND"

After above line removed, The Inputbox shows a 1-line bit of the actual string selected, DOESNT show the area highlighted in the doc, and does not permit clicking in the doc

"as Range" added at the end "Expected: End of statement"

"address"-"Method or data member not found"

"range" "sub or function not defined"

1

There are 1 best solutions below

2
Cindy Meister On

Indeed, the way Word implements the VBA InputBox is different from Excel. Excel is "one, big table"; Word is really "just text", formatted on-screen to look like a table, where required. That's the "why" behind the behavior, FWIW.

You should be able to do something similar, but it will take a bit more work. I can't give you an exact solution since the exact requirements aren't clear and it would be a bit "too broad". But to get you started on the basic "specify the range" part...

(Note: I'm typing this from memory on a mobile device, so there may be syntax errors)

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