macro

chazzerman01

Board Regular
Joined
Nov 18, 2019
Messages
62
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
 

CountTepes

Board Regular
Joined
Nov 8, 2010
Messages
92
Office Version
365, 2016
Platform
Windows
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
 

chazzerman01

Board Regular
Joined
Nov 18, 2019
Messages
62
sorry to be a pain but is it possible to make it so that it only does the first set of quotation marks
 

CountTepes

Board Regular
Joined
Nov 8, 2010
Messages
92
Office Version
365, 2016
Platform
Windows
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
 

Macropod

Retired Moderator
Joined
Aug 27, 2007
Messages
3,320
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.
 

chazzerman01

Board Regular
Joined
Nov 18, 2019
Messages
62
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
 

Macropod

Retired Moderator
Joined
Aug 27, 2007
Messages
3,320
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
 

Forum statistics

Threads
1,085,185
Messages
5,382,194
Members
401,779
Latest member
Thonor

Some videos you may like

This Week's Hot Topics

Top