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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Return string -Word VBA

Status
Not open for further replies.

NNNNN

MIS
Dec 2, 2002
90
GB
HI

I have a document which contains some words in brackes
... (a word) ....... (a nother word).... etc
the words in the brackets need to be changed each time the document is used.

I would like to have a macro that will find the each instance of "(" and store it in a varable together with the following characters until a closing bracket is met in other word the coide should

Find "(" keep moving to the next character, when it reaches "(" store those characters then find the next "("
and repeat the process.
When completed my variable would contain something like "(a word)" & chr(13) & chr(10) & (a nother word)

THANKS
 
A starting point:
Function getParens()
With Selection.Find
.ClearFormatting
.Text = "[(][!)]{1,}[)]"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
Do While .Execute
getParens = getParens & Selection.Text & vbCrLf
Loop
End With
End Function

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
It would be SOOOOOOOO much easier to use bookmarks.

Gerry
 
Can bookmakrs be used/created with VBA

The words in brackets may not always be in the same place in the document.
as sometimes some paragraphs are omitted or additional ones are used ?

How would you find each word in brackets and create a biikmark dynamicaly?
Thanks
 
The line .Text = "[(][!)]{1,}[)]"

seem to have no affect ?
 
How do you call the function ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Sub Macro1()
call getParens()
End Sub
Is there something I should be doing?
 
Sub Macro1()
MsgBox getParens()
End Sub

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Depending on your regional setting you may try this:
.Text = "[(][!)]{1;}[)]"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
You need to add ...

.MatchWildcards = True

... to use the pattern matching capabilities of Word's Find


Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
 
Yes bookmarks can be created and used dynamically with VBA.

But perhaps another solution would be to use formfields. It depends on your requirements. What, exactly, are you doing? Are these changes user edits? Is the user changing these chunks of text? If so, then maybe use formfields. Have the master document a template, load the template and the user inputs the appropriate text.

Blah blah blah (formfield) blah blah blah blah (formfield).

No need to do a search. The user simply inputs whatever text into the formfield.

Gerry
 
Hi NNNN,

The following macro replaces a specified bookmark with a new text string.
Code:
Sub UpdateBookMark()
Dim NewTxt As String
Dim BmkRng As Range
BmkNm = InputBox("Bookmark Name")
NewTxt = InputBox("New Bookmark Text")
	If ActiveDocument.Bookmarks.Exists(BmkNm) Then
		Set BmkRng = ActiveDocument.Bookmarks(BmkNm).Range
		BmkRng.Text = NewTxt
		ActiveDocuments.Bookmarks.Add BmkNm, BmkRng
	Else
		MsgBox "Bookmark: " & BmkNm & " not found."
	End If
Set BmkRng = Nothing
End Sub
Alternatively, if calling the macro with parameters for the bookmark’s name and new string:
Code:
Sub UpdateBookmark(BmkNm As String, NewTxt As String)
Dim BmkRng As Range
If Documents.Count > 0 Then
    If ActiveDocument.Bookmarks.Exists(BmkNm) Then
        Set BmkRng = ActiveDocument.Bookmarks(BmkNm).Range
        BmkRng.Text = NewTxt
        ActiveDocuments.Bookmarks.Add BmkNm, BmkRng
    Else
	MsgBox "Bookmark: " & BmkNm & " not found."
    End If
End If
Set BmkRng = Nothing
End Sub

Cheers
 
Thank you very much

This has been a very helpful topic

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top