PDA

View Full Version : Problem with find text



Nerd901
09-29-2011, 08:13 AM
Hi,

I have a "big" problem.

I'm development a program in ms office word 2010.This program need find a character or tag,apply a style in text between tags / characters, and erase this tags.
aplicar
I managed to find a one tag and erase they, but with two different tags. I really dont know.
I thought about in use a if, and count the two first character,or paragraph start. but I couldn't find a function.


Text Exemple:

H1TESTH1
H2THIS IS MY TESTH2

H1TESTINGH1
H2THIS WILL BE MY TESTH2

My code:



Sub Titulo()
Dim StartWord As String, EndWord As String
StartWord = "H1"
EndWord = "H1"

Do While ActiveDocument.Content.Duplicate.Find.Execute(findtext:=StartWord) = True

With ActiveDocument.Content.Duplicate
.Find.Execute findtext:=StartWord & "*" & EndWord, MatchWildcards:=True
.MoveStart wdCharacter, Len(StartWord)
.MoveEnd wdCharacter, -Len(EndWord)
.ParagraphFormat.Style = ("Title")

End With

With Selection.Find
.ClearFormatting
.Text = "H1"
With .Replacement
.Text = ""
.ClearFormatting

End With


.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceOne
End With
Loop
End Sub






Very tks.

gmaxey
09-29-2011, 09:46 AM
I visited your fair country when I was in the U.S. Navy (USS FRANK CABLE) around 2002 I think.

Try:
Option Explicit
Sub FormatTextBetweenTags_DeleteTags()
Dim arrTags() As String
Dim i As Long
Dim strTag As String
arrTags = Split("H1|H2", "|")
For i = 0 To UBound(arrTags)
strTag = arrTags(i)
Processor strTag
Next i
lbl_Exit:
Exit Sub
End Sub
Sub Processor(ByRef strTagPassed As String)
Dim oRng As Range
Dim oRngAnchor As Range
Dim oRngProcess As Range
Dim i As Long
Resetsearch
Set oRng = ActiveDocument.Range
oRng.Collapse wdCollapseStart
With oRng.Find
.Text = strTagPassed
.Wrap = wdFindStop
While .Execute
.Text = strTagPassed
oRng.Delete
Set oRngAnchor = oRng
If .Execute Then
Set oRngProcess = oRng.Duplicate
oRngProcess.Delete
oRngProcess.Start = oRngAnchor.Start
oRngProcess.Style = "Title"
oRng.Collapse direction:=wdCollapseEnd
End If
Wend
End With
End Sub
Sub Resetsearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub

Nerd901
09-29-2011, 10:40 AM
Great! You liked?

Tks for your answer,really!

But i need apply different styles in tags.

for example,when tag is H2 style is Title.
When tag is H1 style is Heading 1


how i can change and make this?


Very tks!

gmaxey
09-29-2011, 11:57 AM
Use a Select Case statement:


Select Case strTagPassed
Case "H1"
oRngProcess.Style = "Title"
Case "H2"
oRngProcess.Stye = "Whatever"
End Select

Nerd901
09-30-2011, 04:25 AM
Problem solved.Its works!

Tks for your Help Greg.