Take a look at this macro:
You can probably use parts of this macro for your use case.
For more information, see the wiki Searching the Forum for Answers section.