Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-05-2016, 01:55 AM
PRA007's Avatar
PRA007 PRA007 is offline How to find all string within string. Windows 7 64bit How to find all string within string. Office 2010 32bit
Competent Performer
How to find all string within string.
 
Join Date: Dec 2014
Location: Ahmedabad, Gujrat, India
Posts: 145
PRA007 is on a distinguished road
Default How to find all string within string.

I am using VBscript, FSO for opening the file and do some replacements. After that I get strtxt as string having file contents. Now I want to find lines containing specific srtring.

The question I searched very vigorously but could not find the answer.

I got little bit of help from the below mentioned link but I think should ask here to check weather this possible or not.



This is cross post from Here:
vbscript - How to extract lines from String - Stack Overflow
Reply With Quote
  #2  
Old 02-05-2016, 03:02 AM
macropod's Avatar
macropod macropod is offline How to find all string within string. Windows 7 64bit How to find all string within string. Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

You haven't given enough details to work with; it's not even apparent why you're using vbscript or FSO, or what you want to do with the string you're looking for. For code to Find a string within a defined range, see, for example:
https://www.msofficeforums.com/word-...le-column.html
https://www.msofficeforums.com/word-...nces-text.html
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 02-05-2016, 03:17 AM
PRA007's Avatar
PRA007 PRA007 is offline How to find all string within string. Windows 7 64bit How to find all string within string. Office 2010 32bit
Competent Performer
How to find all string within string.
 
Join Date: Dec 2014
Location: Ahmedabad, Gujrat, India
Posts: 145
PRA007 is on a distinguished road
Default

I want to make multiple regex find and replace in the rtf text so to extract some information. as I want to play with text and not the rtf as opened in word I am using fso.

This is the code sniplet:

Code:
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fso, FileName, TextLine, Tempfile, inTempfile, strPath, Temp, intemp
FileName = "C:\Users\rahul\Desktop\PLR\1.rtf"
TempFile = "C:\Users\rahul\Desktop\PLR\Temp\1.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set inFile = fso.OpenTextFile(FileName)
Do While inFile.AtEndOfStream <> True
    TextLine = inFile.ReadAll
Loop
Set regEx_ = new regExp
With regEx_
    .Global = True
    .MultiLine = True
    .IgnoreCase = True
    .Pattern = "\{\\\w{4}\\\w{4}\\\w{11}"
TextLine = regEx_.replace(TextLine, "mmrk$&")
'many such find and replace to get IMP lines Having mmrk
End with
set Temp = fso.CreateTextFile(TempFile, True)
    Temp.Write TextLine
    Temp.Close
Set Temp = Nothing

Set intemp = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\Users\rahul\Desktop\PLR\Temp\1.txt")
Set outFile = CreateObject("Scripting.FileSystemObject").CreateTextFile("C:\Users\rahul\Desktop\PLR\Temp\11.txt",True)

Do Until intemp.AtEndOfStream
    Dim line : line = intemp.Readline
    If Left(line, 4) = "mmrk" then outFile.writeLine(line)
Loop

intemp.Close
outFile.Close
inFile.Close
The problem is

If you see the code, You will notice that I am saving the string first as file and opening the same for reading again. Is there anyway we can extract lines containing specific string from string so as to avoid it saving.
Reply With Quote
  #4  
Old 02-05-2016, 04:45 AM
macropod's Avatar
macropod macropod is offline How to find all string within string. Windows 7 64bit How to find all string within string. Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Regardless of whether you want to "want to play with text and not the rtf as opened in word", that doesn't mean you can't open the file as text in Word and process it as such. For example:
Code:
Sub Demo()
Dim wdDoc As Document
Set wdDoc = Documents.Open(FileName:="C:\Users\rahul\Desktop\PLR\1.rtf, Format:=wdOpenFormatText, AddToRecentFiles:=False)
With wdDoc
  'process here
  .Close SaveChanges:=True
End With
End Sub
That said, here is another macro you could use in any Office application:
Code:
Sub EditRTFFile()
'Requires a reference to Microsoft VBScript Regular Expressions 5.5
Dim DataFile As String, TextLine As String, Data As String, regEx
DataFile = "C:\Users\rahul\Desktop\PLR\1.rtf"
Set regEx = CreateObject("vbScript.RegExp")
Data = ""
Open DataFile For Input As #1
Do Until EOF(1)
  Line Input #1, TextLine
  On Error Resume Next
  With regEx_
    .Global = True
    .MultiLine = True
    .IgnoreCase = True
    .Pattern = "\{\\\w{4}\\\w{4}\\\w{11}"
    TextLine = regEx_.Replace(TextLine, "mmrk$&")
  End With
  Data = Data & TextLine & vbCrLf
Loop
Close #1
Open DataFile For Output As #1
Print #1, Data
Close #1
Set regEx = Nothing
End Sub
The above assumes you have a reason for processing the file one line at a time, rather than just opening it as a text file and simply processing all lines together.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 02-06-2016, 04:30 AM
PRA007's Avatar
PRA007 PRA007 is offline How to find all string within string. Windows 7 64bit How to find all string within string. Office 2010 32bit
Competent Performer
How to find all string within string.
 
Join Date: Dec 2014
Location: Ahmedabad, Gujrat, India
Posts: 145
PRA007 is on a distinguished road
Default

Problem is not in opening file, reading it line by line and doing regex. Real problem starts after that.

For example I am getting by any means, StrTxt as string which contains multiple lines or I don't know how string behaves inside object. As far as my problem concerns I get StrTxt from two souces.

1. by opening a file.

2. from httprequests.

In both the case I end up with StrTxt as string.

So the question is, If My StrTxt contains multiple occurrences of a string like "string", how to get all of it.

For example in hypothetical string.

Code:
StrTxt = "mmrkxyz" & vbCr & "mmrkyzx" & vbCr & "xyz" & vbCr & "yzx" & vbCr & "mmarkxxx"
Now I Just want to get all the lines that have initial mmrk as string like this, so that I can further process it.

Code:
StrTxt = "mmrkxyz" & vbCr & "mmrkyzx" & vbCr & "mmarkxxx"
I don't know how to better explain this, but I can not find any answer of this.

To make it simple, My strings contains multiple lines. I just want it to reduced to lines that contains specific characteristics like Left(StrTxt, 4) = "mmrk".
Reply With Quote
  #6  
Old 02-06-2016, 03:21 PM
macropod's Avatar
macropod macropod is offline How to find all string within string. Windows 7 64bit How to find all string within string. Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by PRA007 View Post
So the question is, If My StrTxt contains multiple occurrences of a string like "string", how to get all of it.

For example in hypothetical string.

Code:
StrTxt = "mmrkxyz" & vbCr & "mmrkyzx" & vbCr & "xyz" & vbCr & "yzx" & vbCr & "mmarkxxx"
Now I Just want to get all the lines that have initial mmrk as string like this, so that I can further process it.
Well, I'm sure you know that a simple Find/Replace in Word can process all instances in a single pass, which is why it might be good to open the file with Word. And, for simple strings in VBA, so to could the VBA Replace method process all instances in a single pass.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 02-07-2016, 11:51 PM
PRA007's Avatar
PRA007 PRA007 is offline How to find all string within string. Windows 7 64bit How to find all string within string. Office 2010 32bit
Competent Performer
How to find all string within string.
 
Join Date: Dec 2014
Location: Ahmedabad, Gujrat, India
Posts: 145
PRA007 is on a distinguished road
Default

That Is what I am currently doing sir. Have no problem with That but thought if withing VBA it is possible It would be great for knowledge.
Reply With Quote
  #8  
Old 02-08-2016, 02:49 AM
macropod's Avatar
macropod macropod is offline How to find all string within string. Windows 7 64bit How to find all string within string. Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

So, since you agree it can be done with Find/Replace, why don't you do that with VBA?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 02-08-2016, 08:52 PM
PRA007's Avatar
PRA007 PRA007 is offline How to find all string within string. Windows 7 64bit How to find all string within string. Office 2010 32bit
Competent Performer
How to find all string within string.
 
Join Date: Dec 2014
Location: Ahmedabad, Gujrat, India
Posts: 145
PRA007 is on a distinguished road
Default

Because I get the text as string from httprequest's response text. I then insert that text to instance of new word doc (temp), then extract the text and close the word unsaved. This works OK, but thought If it can be directly done within VBA what is the need to open new instance of word un-necessarily.
Reply With Quote
  #10  
Old 02-08-2016, 09:58 PM
macropod's Avatar
macropod macropod is offline How to find all string within string. Windows 7 64bit How to find all string within string. Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by PRA007 View Post
If it can be directly done within VBA what is the need to open new instance of word un-necessarily.
Nothing in what I have said implies starting another Word session. Furthermore, everything I have suggested envisages the use of VBA. Indeed, all the code I have posted is VBA, not the vbscript you've been trying to use!!! I'd have thought that much, at least, was obvious.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #11  
Old 02-09-2016, 02:52 AM
PRA007's Avatar
PRA007 PRA007 is offline How to find all string within string. Windows 7 64bit How to find all string within string. Office 2010 32bit
Competent Performer
How to find all string within string.
 
Join Date: Dec 2014
Location: Ahmedabad, Gujrat, India
Posts: 145
PRA007 is on a distinguished road
Default

My Mistake. I will try and then comment further.
Reply With Quote
  #12  
Old 02-10-2016, 04:12 AM
PRA007's Avatar
PRA007 PRA007 is offline How to find all string within string. Windows 7 64bit How to find all string within string. Office 2010 32bit
Competent Performer
How to find all string within string.
 
Join Date: Dec 2014
Location: Ahmedabad, Gujrat, India
Posts: 145
PRA007 is on a distinguished road
Default Find sub-strings in String.

Earlier I was doing things differently but after this answer https://www.msofficeforums.com/word-...ng-string.html I get an Idea that is outlined here.


I am getting StrTxt as string after httpreq response text.

Now I want to find each Substring in StrTxt using regex.

Code:
Set re = New RegExp
re.Pattern    = "pattern"
re.Global     = True
re.IgnoreCase = True  'if required

For Each m In re.Execute(StrTxt)
  Msgbox m
  'run another function for each substring.
Next
How to do this? I mean I know that for each control must be variant or object but I don't know how to do this.

Last edited by PRA007; 02-10-2016 at 10:55 AM.
Reply With Quote
  #13  
Old 02-11-2016, 12:22 AM
macropod's Avatar
macropod macropod is offline How to find all string within string. Windows 7 64bit How to find all string within string. Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

PRA007, Please don't start multiple threads on the same topic. I've merged them.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #14  
Old 02-11-2016, 01:30 AM
PRA007's Avatar
PRA007 PRA007 is offline How to find all string within string. Windows 7 64bit How to find all string within string. Office 2010 32bit
Competent Performer
How to find all string within string.
 
Join Date: Dec 2014
Location: Ahmedabad, Gujrat, India
Posts: 145
PRA007 is on a distinguished road
Default

Okay. My first question for file and extract line containing the string.

This question is about how to find Strings within a string and not the line So I thought that might be different though the solution will be almost similar.
Reply With Quote
  #15  
Old 02-11-2016, 02:01 AM
macropod's Avatar
macropod macropod is offline How to find all string within string. Windows 7 64bit How to find all string within string. Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

As I've said, that can be done with Word's Find/Replace. There have been posts discussing that on these forums (e.g. https://www.msofficeforums.com/word-...html#post95275). Or it can even be done with Regex within Word.

But, until you tell us what the strings and substrings are, it's a bit much expecting us to tell you how to do it if it entails the use of wildcards, especially.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Tags
vbscript, word vba



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to find all string within string. Wildcard replace any string in context with a specified string wardw Word 7 05-07-2018 09:13 AM
How to find all string within string. How can I compare a string in a cell to another string? Amitti Word VBA 2 04-10-2017 07:35 PM
How to find all string within string. Why is this Find string not working TechEd Word VBA 5 07-05-2014 08:12 PM
How to find all string within string. Way to search for a string in text file, pull out everything until another string? omahadivision Excel Programming 12 11-23-2013 12:10 PM
How to find all string within string. Bad view when using Find and Find & Replace - Word places found string on top line paulkaye Word 4 12-06-2011 11:05 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:17 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft