Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Insert Text into a File 2

Status
Not open for further replies.

ToeKnee2

Programmer
Jun 8, 2006
21
GB
Hi All,

I have a bit of code that should go through every file in the folder specified and insert a bit of text. This is fine, except it doesn't work.

Could anyone advise why this doesn't work?


Code:
Sub replacetxt()
Dim fso, fol, f
Dim a As String

Set fso = CreateObject("Scripting.filesystemobject")
Set fol = fso.GetFolder("C:\Test\")


For Each f In fol.Files
    a = FreeFile
    Open f.Name For Output As a
    
    Print #a, "This is new text"
    Close a
Next f

Set f = Nothing
Set fol = Nothing
Set fso = Nothing
End Sub

Thanks in advanced.
 
What exactly is it doing? My guess, every file is being overwritten with a new text file containing the text "This is new text"


Try first opening for append.

Have you tried

Print A, "This is new text"

What errors are you getting?

Except it doesnt work does not give us a lot to work with.


Andy Baldwin

"Testing is the most overlooked programming language on the books!"

Ask a great question, get a great answer. Ask a vague question, get a vague answer.
Find out how to get great answers FAQ219-2884.
 
Do not use f.Name. Use f. It is already identified as a file.
Code:
Sub replacetxt()
Dim fso, fol, f
Dim a

Set fso = CreateObject("Scripting.filesystemobject")
Set fol = fso.GetFolder("C:\Test\")

For Each f In fol.Files
    a = FreeFile
    Open f For Output As #a
    Print #a, "This is new text"
    Close #a
Next f

Set f = Nothing
Set fol = Nothing
Set fso = Nothing
End Sub
Using Output will replace the contents of the file. Are you sure you do not want to use Append?

Gerry
My paintings and sculpture
 
If I may suggest something...

There are debugging tools, and you should use them. If you used either a Debug.Print line, OR stepped through your code and rested the cursor on the variables, you would find:

f.Name = filename

BUT:

f = C:\Test\filename

f is the file itself (which you declared with the For Each f In fol.Files), and includes the full path.

f.Name is the name of the file...and does not include the path.

Gerry
My paintings and sculpture
 
Thanks fumei,

The problem was with the f.Name function.

This is now working correctly for entering plain text.

However, I now need to enter some html code into some of the files. The code is exactly the same for each file, and the code I am inserting into the file should be the only thing in the file.

If I use the code above, it won't let me use html tags and urls. Could you pelase advise how I might change the script to allow for a chunk of code to be inserted into the files instead.

The Code I am trying to insert is

Code:
<html>
<head>
<h2>Sorry, but you are not allowed to view this page</h2>
<META HTTP-EQUIV="Refresh"
      CONTENT="2; URL=/intranet/index.php">
</head>
</html>

I am still learning VB and I have trawled through internet resoures to find out how to do this but to no avail.

Any help would be much appriciated.

Thanks.
 

Have you tried:
Code:
Dim strMyHTML As String

strMyHTML = "<html>" & vbNewLine & "<head>" & vbNewLine & _
    "<h2>Sorry, but you are not allowed to view this page</h2>" & vbNewLine & _
    "<META HTTP-EQUIV=" & Chr(34) & "Refresh" & Chr(34) & vbNewLine & _
    "      CONTENT=" & Chr(34) & "2; URL=/intranet/index.php" & Chr(34) & ">" & vbNewLine & _
    "</head>" & vbNewLine & "</html>"

.....

For Each f In fol.Files
    a = FreeFile
    Open f For Output As #a
    Print #a, strMyHTML
    Close #a
Next f

HTH

---- Andy
 
Sorry guys, so busy I forgot to reply and tell you all that it works and I am an extremely happy lad.

Thanks for all your help.


Andrzejek and fumei both deeserve stars for your assistance, but I think I can only give one to Andrzejek because he gave me the working solution first. :)

Thanks again guys.


 
Andrzejek and fumei both deeserve stars for your assistance, but I think I can only give one to Andrzejek because he gave me the working solution first. :)
Well, thanks whoever gave me the star.

ToeKnee2 I would like to point out that the string issue was the second thing you asked for. The working solution was to use f, not f.Name, which I first gave you.

Otherwise, you may be using a string...but it would still not be working.
The problem was with the f.Name function.
It is not a function.

Gerry
My paintings and sculpture
 
Hi Gerry,

I gave you a star aswell for you're solution to my first issue.

Please accept my apology for calling f.Name a funtion. :)

Thanks again,

Tony
 
ooops. Uh, there was no need to apologize re: f.Name. Why? I was simply pointing it out. It is good to try and use terms as accurately as possible. It makes it easier for us to understand what someone is saying. And (hopefully) it makes it easier for someone posting to understand.

Of course, it all has to be in context. As this is the VBA forum, "function" used within the context of what is in code should mean...ummmm, what a function means. That is, a procedure (a set of instructions) that returns a single value. At least generally they do. You certainly can have functions that do not explicitly return a value, but normally that is the reason for writing functions. As opposed to a Sub which is a procedure (a set of instructions) that does not return a value.

In this case, f.Name is a property, which was exactly the problem in the code. The code was trying to perform action on an object (the file...f), but was given a property (f.Name).

Anyway, again, no need to apologize for anything. We are ALL learning here...and this is supposed to be fun...sort of...maybe...except when VBA (and especially in Word) does strange things.

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top