Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Make Random Selection in Word 2007

Status
Not open for further replies.

davidh9876

Programmer
Jul 18, 2005
2
US
I am trying to write a VBA macro for Word 2007. What I need is for the user to sart the macro, the macro to prompt the user to make a selection by highlighting text in the document, having the macro process that selection and to have this all repeat until the user is done selecting text. There is no way to know before run time how many selections there will be or what they will exceptfor the user to highlight text.

If anyone is familiar with AutoCAD VBA, I'm looking for the equivalent of the AcadSelectionSet.SelectOnScreen method. I can't find anything anywhere that shows how to do this. Please help!!!
 
What do you mean by
having the macro process that selection

Could you not simplify the process by:
Having the user select text
Run a macro assigned to a keyboard shortcut to do the process
and return to the document?

This old world keeps spinning round - It's a wonder tall trees ain't layin' down
 
I agree. davidh9876, could you please clarify precisely what you are trying to do? The problem is that once a macro procedure starts there can be issues with returning focus to the document (for the user to select text) and coming BACK to the processes in the procedure (macro).

Procedures (macros), as white605 points out, can easily handle whatever IS the Selection. However, you seem to be asking for a procedure to start, wait for the user to make a Selection, THEN do something, and then wait again.

I am not sure what AcadSelectionSet.SelectOnScreen does, but in Word, the Selection object IS whatever is selected on-screen.

If the action you wish to perform is the same - say, expand the Selection to a sentence and then make the selected text bold (but it could be anything) - it would be much simpler to make a shortcut key that executes the macro to perform that action. The user selects whatever and hits the shortcut key.

Technically, yes, what you appear to asking can be done.
Code:
Option Explicit

Private Sub cmdAction_Click()
   If Len(Selection.Text) > 1 Then
      With Selection
         .Expand Unit:=wdSentence
         .Font.Bold = True
      End With
   Else
      MsgBox "Nothing selected."
   End If
End Sub

Private Sub cmdDone_Click()
   Unload Me
End Sub
This is code for a userform with two commandbuttons, nothing more.

The userform has SystemModal = False. This is critical as it is the only way for the user to be able to switch to the document (to select something) and get back to the active userform.

The Action button simply checks to see if the Selection is greater than one character, and if it is, expand the Selection the sentence, and make it bold. Otherwise it displays a messagebox stating nothing is selected. You could (and I would) have that message displayed on the userform rather than waste the effort of displaying a messagebox.

Note that the test is for Len of the Selection.Text, NOT something like:
Code:
If Selection.Text <> "" Then

While perhaps not intuitive, Selection.Text is NEVER "".

If you have the cursor (Selection) blinking - say - just before the q of:

The quick brown fox

The "q" is NOT selected, the cursor is blinking by its lonesome just before it. You may think that nothing is selected, but that is incorrect.

MsgBox Selection.Text would return......"q".

If collapsed to a point, Selection.Text returns the next character.

In any case, the above code does indeed allow the user to do multiple selections and then perform actions on those Selections. However, if you try this you will rapidly see that it is rather awkward to use because of the need to switch back and forth.

Gerry
 
What I want is a macro that calls up every document in a directory. When the document is opened, I want the macro to prompt the user to highlight some text and then prompt him to select from a list of fields. The macro will then create a docvariable based on this selection and replace the highlighted text with a field linked to the newly created docvariable. The process will repeat until the user has finished. The macro will then save/close the document and the next document will open up...

This seems like such a simple thing to do and yet it seems to be inpossible in Word VBA.
 
No, it is certainly not impossible. It is quite possible. As a programmer you must know that the first thing you do is explicitly, precisely, detail the requirements. I suggest you start there. I have explained what some of the issues could be.

I suggest you actually try something, then come back and ask specific questions when you have some.

Some starting points:

"What I want is a macro that calls up every document in a directory. " This is straightforward; use the Dir function.

"When the document is opened, I want the macro to prompt the user to highlight some text and then prompt him to select from a list of fields. "

I have commented on issues of switching focus. "Promt" is a slippery term. Considering you are trying to perform further action I would suggest - as I stated previously - to use a userform. The "prompt" could be done as text on the userform: "Click the document and select (highlight) text. When text is selected, click this dialog again."

The user does so, and clicks back to the userform. On the userform is your "list of fields".

The user selects from the list.

"The macro will then create a docvariable based on this selection and replace the highlighted text with a field linked to the newly created docvariable."

A field "linked" to the docvariable? Do you mean a DOCVARIABLE field replaces the selected text? That would not be difficult.

"This seems like such a simple thing to do and yet it seems to be inpossible in Word VBA. "

Simple? No, it is NOT simple, but it is also NOT difficult.

All it takes is clear and precise logic. In other words, a clear and precise listing of the step-by-step actions/requirements.

For example: What are - precisely - your "list of fields"? What does that mean?

This is not impossible at all.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top