Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / July 2007

Tip: Looking for answers? Try searching our database.

replace, instr etc. in text file

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
muybn - 16 Jun 2007 23:07 GMT
I'm trying to do a VB command that will replace all instances of "," with a
tab in a text file. My code is:

Sub test()
   Dim strScrap As String, strRepl As String, intCt As Integer, arrText As
Variant
   
   strRepl = Chr(34) & Chr(44) & Chr(34)
   Open "C:\directory\file" For Input As #1
       Do While InStr(strScrap, strRepl) > 0
           Line Input #1, strScrap
           arrText = Replace(strScrap, strRepl, Chr(9))
       Loop
   Close #1
End Sub

When I execute it, however, I find instr = 0 but the file is full of this
text. What am I missing?

Signature

Bryan

Doug Robbins - Word MVP - 16 Jun 2007 23:27 GMT
Why don't you just record the use of the Edit>Replace menu item with a comma
in the Find what control and ^t in the Replace with control.

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> I'm trying to do a VB command that will replace all instances of "," with
> a
[quoted text clipped - 15 lines]
> When I execute it, however, I find instr = 0 but the file is full of this
> text. What am I missing?
muybn - 16 Jun 2007 23:59 GMT
I normally do and I would, but with the huge amount of records I'm affecting
in this particular macro, Klaus advised me to use this method for better
speed and memory considerations.

Signature

Bryan

> Why don't you just record the use of the Edit>Replace menu item with a comma
> in the Find what control and ^t in the Replace with control.
[quoted text clipped - 18 lines]
> > When I execute it, however, I find instr = 0 but the file is full of this
> > text. What am I missing?
Russ - 17 Jun 2007 01:37 GMT
Bryan,
Is this closer to what you want? I didn't test it. But looking at it in a
program flow type of way, it looks workable. It loads arrText with each
amended line and unaltered line of the file and you'll need to write them
back out with another subroutine.

Sub test()
   Dim strScrap As String, strRepl As String, intCt As Integer
   Dim arrText() As String
   
   strRepl = Chr(34) & Chr(44) & Chr(34)
   Open "C:\directory\file" For Input As #1
       Do While Not EOF(1)    ' Loop until end of file.
           Line Input #1, strScrap
           ReDim Preserve arrText(1 To UBound(arrText) + 1)
           If InStr(strScrap, strRepl) > 0 then
              arrText(UBound(arrText)) = Replace(strScrap, strRepl, Chr(9))
           Else
              arrText(UBound(arrText)) = strScrap
           End If
       Loop
   Close #1
End Sub

...
For intCt = 1 to UBound(arrText)
'write each line back out
Next intCt

> I normally do and I would, but with the huge amount of records I'm affecting
> in this particular macro, Klaus advised me to use this method for better
[quoted text clipped - 22 lines]
>>> When I execute it, however, I find instr = 0 but the file is full of this
>>> text. What am I missing?

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

mary fran - 14 Jul 2007 17:12 GMT
Our question is similar, so perhaps you can help us as well.

We are trying to find comma followed by number and replace the comma with a
tab, followed by the same number.  We do not want to replace all commas with
a tab, just those commas followed by a number.

Find      , ^#
Replace ^t^&

This does not work because it adds the tab but leaves the comma.  What do we
need to do differently?

> Why don't you just record the use of the Edit>Replace menu item with a comma
> in the Find what control and ^t in the Replace with control.
[quoted text clipped - 18 lines]
> > When I execute it, however, I find instr = 0 but the file is full of this
> > text. What am I missing?
Jay Freedman - 14 Jul 2007 20:04 GMT
Use a wildcard search
(http://www.gmayor.com/replace_using_wildcards.htm) with these
expressions:

Find         , ([0-9]{1,})
Replace    ^t\1

The Find expression (which is not valid if wildcard searching is
turned off) uses the parentheses to mark the number part of the
expression, and the \1 in the Replace expression refers to that part.

It appears that your first attempt included a space between the comma
and the number, so I included it above. If that's not correct, remove
the space.

--
Regards,
Jay Freedman
Microsoft Word MVP        FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

>Our question is similar, so perhaps you can help us as well.
>
[quoted text clipped - 30 lines]
>> > When I execute it, however, I find instr = 0 but the file is full of this
>> > text. What am I missing?
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.