473,385 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

VBScript to insert text into text file and save as new filename

4
HI folks,

I need 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 so I can run it from my desktop.

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 the modified file for me and I couldn;t figure how to get it outside of Word. Code below.

Expand|Select|Wrap|Line Numbers
  1.  
  2. ' Test1 Macro
  3. ' To open FIle-Open dialog box
  4.     With Dialogs(wdDialogFileOpen)
  5.     .Name = "*.txt"
  6.     .Show
  7.     End With
  8. ' To insert the text
  9.     Selection.TypeParagraph
  10.     Selection.TypeParagraph
  11.     Selection.TypeParagraph
  12.     Selection.HomeKey Unit:=wdStory
  13.     Selection.TypeText Text:="The next line sends a 'Change State' command to system"
  14.     Selection.TypeParagraph
  15.     Selection.TypeText Text:="M3"
  16.     Selection.TypeParagraph
  17.     Selection.TypeText Text:="The next line tells system to Dwell for 5 seconds"
  18.     Selection.TypeParagraph
  19.     Selection.TypeText Text:="G04H5"
  20.  
  21. 'This is where the error occurs  
  22. With Dialogs(wdDialogFileSave)
  23.     .Name = "*.*"
  24.     .Show
  25. End With
  26.  
  27. End Sub 
  28.  
Help greatly appreciated
Jan 23 '07 #1
2 19698
ahynes
4
OK, I've got the code to Work in VBA in MS Word, now how do i get it to run without requiring Word? i.e. so it runs as astnadalone EXE or BAT or anything like that?

Thanks for any help

Alan

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub CommandButton1_Click()
  3. With Dialogs(wdDialogFileOpen)
  4.     .Name = "*.txt"
  5.     .Show
  6.     End With
  7. 'Edit the additional text to go at the START of the document in this section
  8.     Selection.HomeKey Unit:=wdStory
  9.     With Selection
  10.         .TypeParagraph
  11.         .TypeParagraph
  12.         .TypeParagraph
  13.         .TypeText "Copyright 2007"
  14.         .TypeParagraph
  15.         .TypeParagraph
  16.         .TypeText "This is a control file for the system"
  17.         .TypeParagraph
  18.         .TypeParagraph
  19.         .TypeText Text:="Command M3 sends a 'Change State' command to system"
  20.         .TypeParagraph
  21.         .TypeText Text:="M3"
  22.         .TypeParagraph
  23.         .TypeParagraph
  24.         .TypeText Text:="Command G04H5 tells the system to Dwell for 5 seconds"
  25.         .TypeParagraph
  26.         .TypeText Text:="G04H5"
  27.     End With
  28.  
  29. 'Edit the additional text to go at the END of the document in this section
  30.     With Selection
  31.         .EndKey Unit:=wdStory
  32.         .TypeParagraph
  33.         .TypeParagraph
  34.         .TypeParagraph
  35.         .TypeText Text:="This is end of file"
  36.     End With
  37.  
  38. 'Rest of code saves file with .nc extension and in txt format
  39.     Dim strDocName As String
  40.     Dim intPos As Integer
  41.  
  42.     'Ask the user to provide a filename
  43.     strDocName = InputBox("Please enter the filename you want for your new file.")
  44.  
  45.     'Add ".nc" extension
  46.         strDocName = strDocName & ".nc"
  47.  
  48.     'Save file with new extension
  49.     ActiveDocument.SaveAs FileName:=strDocName, _
  50.         FileFormat:=wdFormatText
  51.  
  52. 'MessageBox to tell the user it worked ok and the name of the new file
  53.  
  54.     Dim Message As String 'Defines "Message" as the string for the MsgBox command
  55.  
  56.     Message = "Your file has been saved as '" & strDocName & "'" & Chr(13) & " It is now compatible with your system"
  57.     'strDocName is the filename and Chr(13) is a paragraph character
  58.  
  59.     MsgBox (Message)
  60.  
  61.     ActiveDocument.Close
  62.  
  63.  
  64. End Sub
  65.  
  66.  
Jan 24 '07 #2
Killer42
8,435 Expert 8TB
The following thoughts occur...
  • You can only compile your VB code as a standalone executable if you have Visual Basic. The VBA that comes with Office only uses it for scripting.
  • If you don't want to have to play around with Word, you could try this. Save the macro in a Word document, but name the macro AutoExec so that it starts automatically when the document is opened. At the end of the macro, get it to close the document. That way, the Word document works kind of like a standalone Exe. Well, a tiny bit. :)
  • If you have some way to insert the file name on the command line, you could do a fairly simple batch file. It would work something like this.
    • Put your text into two files, Header.txt and Footer.txt (or whatever names you choose)
    • Let's call your batch file MyMacro.Bat. And for the sake of argument, we'll say that the file you want to manipulate is called FileName.Txt.
    • Your batch file will say something like
      Expand|Select|Wrap|Line Numbers
      1. Copy Header.txt+%1+Footer.Txt %1.nc
    • You invoke it by executing the command line MyMacro Filename.txt
  • I don't know whether this really matters, but there's no need to load the file document into Word to manipulate it. VB(A) can easily work directly with the text file. It's even likely to be faster, but by so little that I doubt you'd notice the difference.
Jan 25 '07 #3

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

Similar topics

4
by: chris.dunigan | last post by:
I'm looking for an example of how to execute an existing DTS­ package from an ASP (VB)script and would appreciate any and all response. ­I don't even know if it's possible Thanks - Chuck...
0
by: AxOn | last post by:
Hi! I'm having trouble creating a save funktion. How can I save the contens of a new richTextBox that is created from an "external" class file? The open funktion looks like this: public void...
1
by: JNariss | last post by:
Hello, I have created a connection to my Access database with Dreamweaver and made a simple form with 4 fields. The code behind this form was/is: <%@LANGUAGE="VBCRIPT" CODEPAGE="1252"%>...
6
by: Julien | last post by:
Hello, I have some files located in a file server and managed by a SQL database from a web based interface using ASP + VBSCRIPT technology. I need to automatically copy those files to a web...
0
by: teddymeu | last post by:
Hi Guys, since I've done nothing but ask questions these last few weeks to get my first application up and running I thought it was about time to share the wealth and help out a newbie like me since...
4
by: viper888 | last post by:
Hi to all, I'm the newly appointed network administrator in our office, and upon scanning all the PCs that are connected to the network, (by the way we're using a windows 2000 server) almost all...
1
by: anniefs | last post by:
hi help me i m so much stuck int he code and i have no time .... i used ASP VBscipt and javascript functions with MS database javascript function add records in MS DB by using ASP vbscript...
1
by: dittytwo | last post by:
Hi there The below code works if you don't try to do anything to the word document which is great (Not :D) I am having problems with the context command both text (i.e initial document with no...
0
by: =?Utf-8?B?TWFyY2VlcG9v?= | last post by:
I'm a newbie using Visual Studio and Vb.Net. The vbscript below opens a Windows Explorer Search Window. The Windows Explorer Search Window has fields in which the user can insert: (i) a search...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.