Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
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: 22,467
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
  #2  
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
  #3  
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: 22,467
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
  #4  
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
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 12:00 AM.


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