By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,300 Members | 1,795 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,300 IT Pros & Developers. It's quick & easy.

Script to insert text into text file

P: 4
HI folks,

I'm a chem engineer with no VB programming knowledge (as you'll see from my question!)

I want a script to open a .txt file, insert pre-defined text into the start and end of the file, then close teh saved file with a .nc extension.

I'd like to have this as a batch file or exe file which i can installt o run from my users desktops.

I tried doing it in Word using macros and the FileDialog thingy to bring up dialogs for the user to select his files but it would not save for me and I couldn;t figure how to get it outside of Word. Code below.
Expand|Select|Wrap|Line Numbers
  1. ' Test1 Macro
  2. ' To open FIle-Open dialog box
  3.     With Dialogs(wdDialogFileOpen)
  4.     .Name = "*.txt"
  5.     .Show
  6.     End With
  7. ' To insert the text
  8.     Selection.TypeParagraph
  9.     Selection.TypeParagraph
  10.     Selection.TypeParagraph
  11.     Selection.HomeKey Unit:=wdStory
  12.     Selection.TypeText Text:="The next line sends a 'Change State' command to system"
  13.     Selection.TypeParagraph
  14.     Selection.TypeText Text:="M3"
  15.     Selection.TypeParagraph
  16.     Selection.TypeText Text:="The next line tells system to Dwell for 5 seconds"
  17.     Selection.TypeParagraph
  18.     Selection.TypeText Text:="G04H5"
  20. 'This is where the error occurs  
  21. With Dialogs(wdDialogFileSave)
  22.     .Name = "*.*"
  23.     .Show
  24. End With
  26. End Sub
Help greatly appreciated
Jan 22 '07 #1
Share this Question
Share on Google+
2 Replies

Expert 100+
P: 2,364
Hello Mr. Hynes!

I am may have a simpler solution program for you. Are you typing in the text or will the text be added by your form load?

I am keeping a log of your message to ensure I get to it whn needed. I have an example program here fro the college, may be helpful to you.

Feb 3 '07 #2

Expert 5K+
P: 8,434
If the text is the same every time, you could probably something with a batch file. I'm thinking something along the lines of...
Expand|Select|Wrap|Line Numbers
  1. COPY HeaderStuff.Txt+YourTextFile.txt+FooterStuff.Txt
...or, if you can provide the input file name as a parameter to the batch file...
Expand|Select|Wrap|Line Numbers
  1. COPY HeaderStuff.Txt+%1+FooterStuff.Txt
This one would produce a modified copy with .nc added onto the end of the existing filename and extension - Eg.
Feb 4 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.