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

Search for string and then assign to variable 2

Status
Not open for further replies.

GOSCO

Technical User
Sep 26, 2000
134
GB
Im trying to do a wildcard search for a string in word and store as a variable using a macro.

Can't see how to do this! Any help appreciated.
 



Hi,

What application?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Post your code. This is quite straightforward, so it should be easy to correct your code...once we see it. Basically though:

1. declare your variable
2. perform the search
3. assign the search result to the variable

Are you doing this ONCE, or many times? Say your document is (keeping it simple):

This is yadda yadrgfda foryadgbbasha
Code:
Option Explicit

Sub Wild()
Dim r As Range
Dim myVar As String

Set r = ActiveDocument.Range
With r.Find
   .ClearFormatting
   .MatchWildcards = True
   .Text = "yad*a"
   .Execute
   myVar = r.Text
End With
MsgBox myVar
End Sub
This executes ONCE, and resulting messagebox would have the first search .Found text: "yadda"
Code:
Sub Wild_2()
Dim r As Range
Dim AllOfThem As String

Set r = ActiveDocument.Range
With r.Find
   .ClearFormatting
   .MatchWildcards = True
   .Text = "yad*a"
   Do While .Execute(Forward:=True) = True
      AllOfThem = AllOfThem & r.Text & _
            vbCrLf
   Loop
End With
MsgBox AllOfThem
End Sub
this executes a loop through the document and builds a string variable of ALL .Found that meet the search criteria. Result would be:

yadda
yadrgfda
yadgbba

Notice that the last string put into the variable is yadgbba, NOT foryadgbbasha (the whole "word"). That is because the search is "yad*a". It certainly could be adjusted to grab the containing word (if the search string is found), but you did not state your requirement.


Gerry
 
Hi Gerry thanks for the help. My aim is to extract words from the document and then save as using the extracted words.

A few problems starting with@

Line1
~Line2~
Line3
Line4

Works ok

Line1
Line2
Line3
Line4

Should produce nothing but produces all 4 lines!


Code:
Sub test()
'
' test Macro
'
'
Set objDoc = ActiveDocument
Dim r As Range
Dim FileName As String

Set r = ActiveDocument.Range
With r.Find
   .ClearFormatting
   .MatchWildcards = True
   .Text = "~*~"
   .Execute
End With
FileName = Replace(r.Text, "~", "")
FileName = RTrim(FileName)
FileName = LTrim(FileName)
FileName = "C:\" & FileName & ".doc"
MsgBox FileName
objDoc.SaveAs (FileName)
End Sub
 
When you do a successful Find (as in your first example), the Range, [blue][tt]r[/tt][/blue], is redefined to be the Found text, in this case [blue]~Line2~[/blue]. You then do your stuff with that.

But you must check whether the Find is successful. In the second case, because [blue]"~*~"[/blue] is not found, [blue][tt]r[/tt][/blue] is not reset, and remains equal to [blue][tt]ActiveDocument.Range[/tt][/blue]. The Replace then does nothing to the text, which is assigned, complete, to [blue][tt]FilleName[/tt][/blue].

You could, for example, do this (untested):

Code:
[green]' ...[/green]
[blue]Set r = ActiveDocument.Range
With r.Find
   .ClearFormatting
   .MatchWildcards = True
   .Text = "~*~"
   .Execute
End With
[red]If r.Find.Found = true then[/red]
FileName = [green]' etc. [/green]
[red]Else
    [green]' do something else[/green]
EndIf[/red][/blue]

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Ok thanks for that I have amended my code. Can you take a quick look to see if I can do anything better. My aim is once the macro is run that I check the document has been saved properly. Is this neccesary? As if it didn't save objDoc.SaveAs (FileName) would fail and cause a VBS error?

Code:
Sub test()
'
' test Macro
'
'

Dim r As Range
Dim FileName As String, filesys, createdate, Today

Set r = ActiveDocument.Range
With r.Find
   .ClearFormatting
   .MatchWildcards = True
   .Text = "~*~"
   .Execute
End With

' Search for filename in document and save
If r.Find.Found = True Then
    FileName = Replace(r.Text, "~", "")
    FileName = RTrim(FileName)
    FileName = LTrim(FileName)
    FileName = "C:\" & FileName & ".doc"
    objDoc.SaveAs (FileName)
    
    ' Check file has been saved
    ' Check File exists first and if it does check the time stamp to be sure
    If objFSO.FileExists(FileName) Then
        ' Get Date of document saved.
        Set tempfile = objFSO.GetFile(FileName)
        createdate = tempfile.DateCreated
        Today = Date
        ' Check Date of file is today
        If DateDiff("d", Today, createdate) = 0 Then
                MsgBox "File appears to be saved correctly"
        Else
                MsgBox "There is a problem, with verifying file is saved please contact System Administrator"
        End If
    Else
        MsgBox "There is a problem, with verifying file is saved please contact System Administrator"
        Exit Sub
    End If

Else
    MsgBox "Error finding filename in document, please try again. Document was NOT sent. Sorry."
End If

End Sub
 
It is not normally necessary to confirm that a file has been successfully saved. If the SaveAs returns without error, especially if background saving is switched off, it should be safe to assume the Save has been successful. I don't suppose it does any harm; equally, however, if the save is to removable storage, your check doesn't necessarily guarantee anything.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Great Thanks for all your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top