There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
Inserting Data in a Text File


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
newbie123's Avatar
Computer Specs
Junior Member with 3 posts.
 
Join Date: Feb 2007
Experience: Beginner
26-Feb-2007, 07:21 PM #1
Inserting Data in a Text File
Hello Everyone,

This is my first post on this site. I am also working on my first macro and it is turning out to be quite complicated but the mere idea of it working is enough to keep me at it.

In short, I want to use an excel macro to read a text file for a specific line of text, and then write data to the text file on the line before. In other words, rather than the data being added to the end of the file it's added to a particular section of the file after the existing data of that section.

Is this possible? Does anyone have example code for this? I have searched online and tried another forum and no luck so far. I appreciate any tips or sample code, thankyou!!
Jimmy the Hand's Avatar
Senior Member with 794 posts.
 
Join Date: Jul 2006
Location: Hungary
Experience: With Excel, fairly good.
27-Feb-2007, 04:46 PM #2
Hi and
Welcome to TSG!

Let me suggest something and then you say if I got it right or not. It's an algorithm.

1. Read a line from textfile "A".
2. Write the same line to a temporary textfile "B"
3. repeat steps 1 and 2 until you reach the specific line in textfile "A".
4. Write the additional data into textfile "B"
5. repeat steps 1 and 2 until you reach the end of textfile "A"
6. Kill (delete) textfile "A"
7. Rrename "B" to "A"

How does it sound?

Jimmy
__________________
'
It is advised to provide a clear, detailed description of the task, so that others can understand it, and offer the best possible help. Otherwise, you risk experts ignoring your request.
Rollin_Again's Avatar
Distinguished Member with 2,834 posts.
 
Join Date: Sep 2003
Location: Atlanta, GA - Planet Earth
Experience: Brilliant When Sober
28-Feb-2007, 11:20 AM #3
Please post sample text files so we can see where and how you want to move the data.

Regards,
Rollin
newbie123's Avatar
Computer Specs
Junior Member with 3 posts.
 
Join Date: Feb 2007
Experience: Beginner
28-Feb-2007, 03:14 PM #4
Jimmy the Hand,

I understand your method and that is certainly a possibility. My only concern is that i want to write to all of the sections of the file at the same time, that is, the user inputs "A", and the macro writes "B" to section 1, "C" to section 2, "D" to section 3, and so on. I suppose I could set it up such that it just rewrites the file in the manner you suggest 6 times (or as many sections as I need to write to). Perhaps this is the method to use.

Rollin_Again,

Please find attached the "template" file as I call it. You'll see there are several sections that will be filled with data. Here is the basic layout the program requires input:

Joint Commands that generate joints in 3-D space.

Member Commands that generate members between these joints in 3-D space.

Group Commands that make groups of the members for convenience (i.e. group "IBEAM" is a group that contains all of the members that are I-beams).

etc...

I have already written the macro to generate all of the commands for each section based on just two inputs from the user. The problem as stated is how to write this to a text file. Keep in mind I cannot do a batch generation, that is, i cant write all these to separate files and then combine later because sometimes the user has a big file and is just adding one or two more members. Therefore, it needs to be able to append to the document and not alter its previous contents.

If either of you need further explanation or anything else please let me know, thanks so much!!!
Attached Files
File Type: txt LeadShieldingTemplate.txt (2.4 KB, 27 views)
Jimmy the Hand's Avatar
Senior Member with 794 posts.
 
Join Date: Jul 2006
Location: Hungary
Experience: With Excel, fairly good.
28-Feb-2007, 05:26 PM #5
Hi Newbie,

I don't think my method should be repeated 6 times. All section inserts could be done in one run. The only thing needed would be to provide section names and the corresponding texts to insert.

But I've got another idea, that could be even more simple. Open the textfile in Excel, so that each line gets into one cell in column A. Insert as many rows as you want, where you want, and then, if ready, save the text file again. This little code opens your textfile, inserts 2 rows before Row 15, puts the string "Newly Inserted Text" into A15 and saves the textfile again.

Code:
Sub InstertIntoTextfile()
    Workbooks.OpenText Filename:="C:\LeadShieldingTemplate.txt", StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array(0, 2)
    Rows(15).Insert shift:=xlDown
    Rows(15).Insert shift:=xlDown
    Range("A15") = "Newly Inserted Text"
    Application.DisplayAlerts = False
    ActiveWorkbook.Save
    ActiveWorkbook.Close
    Application.DisplayAlerts = True
End Sub
HTH

Jimmy
__________________
'
It is advised to provide a clear, detailed description of the task, so that others can understand it, and offer the best possible help. Otherwise, you risk experts ignoring your request.
newbie123's Avatar
Computer Specs
Junior Member with 3 posts.
 
Join Date: Feb 2007
Experience: Beginner
01-Mar-2007, 10:28 AM #6
Jimmy the Hand,

That seems like its going to do the trick, however, to make it do what i really want, when it inserts the text file into excel i can do a search for a particular phrase in the cells (basically the line of text after where i want to insert my new text) rather than predefining the position as this position changes everytime new text is added. Does this sound right?

Thank you so much for your help on this, I'll have to play with it a little more to adapt to my macro but I think you solved my problem!!
Jimmy the Hand's Avatar
Senior Member with 794 posts.
 
Join Date: Jul 2006
Location: Hungary
Experience: With Excel, fairly good.
01-Mar-2007, 11:33 AM #7
Quote:
Originally Posted by newbie123
when it inserts the text file into excel i can do a search for a particular phrase in the cells (basically the line of text after where i want to insert my new text) rather than predefining the position as this position changes everytime new text is added. Does this sound right?
Absolutely so. I was thinking the same. I only drew up a draft of an idea, and counted on your being able to figure out how to find the sections
Sorry for not being specific enough. In case you need more help, pleas let us know.

Quote:
I think you solved my problem!!
You think wrong
The only one who can solve your problems is the same one that causes them. And that's yourself. Help is all around, and asking for it is part of the solution. The unfortunate thing is that many people never reach the point where they would ask...

Cheers

Jimmy
__________________
'
It is advised to provide a clear, detailed description of the task, so that others can understand it, and offer the best possible help. Otherwise, you risk experts ignoring your request.
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 01:02 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.