macro

chazzerman01

Board Regular
Joined
Nov 18, 2019
Messages
65
I know this is an excel board but please does anyone know in word vba how to find a text string between a pair of speech marks

please someone help if you can it would be much appreciated been trying to solve it for the last hour
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
There may be other ways of doing this, but I came up with this quick and dirty demonstration of a way it can be done..
I'm sure you can adapt it into a function.
I have it highlighting the words in between the quotes as well as displaying the found words in a message box.

Some of the variables I used to help me debug the logic. Also as word uses Unicode characters, it is easier than if it used straight Ascii.

VBA Code:
Sub FindText()

Started = False     'These are to help keep track
Finished = False    'of where we are between quotes

Dim FoundText As String
For Each c In ThisDocument.Characters
    s = c.Text
    t = AscW(s)
    Select Case t
        Case 8220 ' This is the unicode for open quotes
            Started = True
        Case 8221 ' This is the unicode for close quotes
            Finished = True
            Started = False
        Case Else
            If Started Then
                FoundText = FoundText & s
                c.Font.Color = vbBlue
            End If
    End Select
If Finished Then
    MsgBox FoundText
    Finished = False
    FoundText = ""
End If

Next

End Sub
 
Upvote 0
Yes.

But if you do, it will select everything from the quote to the end of the document.

It will do that anyway, if you have no closing quotes.

You only have to remove or comment out these lines

VBA Code:
        Case 8221 ' This is the unicode for close quotes
            Finished = True
            Started = False
 
Upvote 0
It's way simpler than that! Use a wildcard Find, where:
Find = “*”
Easily turned into a macro, if needed, but you haven't said you need one or what you want done with the found string.
 
Upvote 0
Sub FindText()
started = False 'These are to help keep track
finished = False 'of where we are between quotes
Dim xl As Excel.Application
Dim foundtext As String
For Each c In ActiveDocument.Characters
s = c.Text
t = AscW(s)
Select Case t
Case 8220 ' This is the unicode for open quotes
started = True
Case 8221 ' This is the unicode for close quotes
finished = True
started = False
Case Else
If started Then
foundtext = foundtext & s
'c.Font.Color = vbBlue
End If
End Select
Next
bab:
End Sub

basically I have a macro that selects the active word document which would be a template made for invoices, on the template theres a pair of speech marks which has a currency value but the wording can change or be different to the last. the final aim of this macro is to search for the first set of speech marks which would contain the currency value. when it finds the data it will get copied onto an excel sheet. your help is much appreciated, thank you
 
Upvote 0
This would be best done from Excel, rather than from Word. The following Excel macro captures the first quoted string from the active Word document and pastes it into the ActiveCell.
VBA Code:
Sub Demo()
Application.ScreenUpdating = False
Dim WordApp As Word.Application
Set WordApp = GetObject(, "Word.Application")
With WordApp.ActiveDocument.Range
  With .Find
    .Text = "“*”"
    .MatchWildcards = True
    .Wrap = wdFindStop
    .Forward = True
    .Execute
  End With
  If .Find.Found = True Then ActiveCell.Value = Replace(Replace(.Text, "“", ""), "”", "")
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,415
Messages
6,119,375
Members
448,888
Latest member
Arle8907

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top