Redirecting output of a command in a macro

K

KLH

I'm trying to do something similar to redirecting output in DOS and shell
scripting, where you can use the output from one command as the input for
another command.

I'm trying to create a macro that will create a bookmark name by copying and
pasting a bit of text from the document. At one point in the macro there is
this command:

Selection.Copy

This copies the text that I want to use as a macro name. Later, there is
this section:

With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:="Bkmk1"
.DefaultSorting = wdSortByName
.ShowHidden = False

The problem is, every time I run the macro, it uses "Bkmk1" as the input for
the bookmark name. Is there a way to set the text copied in the first
command as a variable, and then use the value of that variable for the
bookmark name?
 
G

Greg Maxey

Provided your selected text represents a "valid" bookmark name:

Sub Test()
Dim pStr As String
pStr = Selection.Text
Bookmark?
With ActiveDocument.Bookmarks
.Add Range:=Selection.Range, Name:=pStr
.DefaultSorting = wdSortByName
.ShowHidden = False
End With
End Sub

otherwise you will need to incorporate some error handling.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top