473,657 Members | 2,800 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

4 New Member
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 19709
ahynes
4 New Member
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 Recognized Expert Expert
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
12670
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 Gatto Dan Guzman Apr 27 2000, 12:00 am show options
0
2383
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 openFile() { OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Title = "Select a Text File";
1
4217
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"%> <!--#include virtual="/Connections/EmployeeStatusChange.asp" --> <%
6
12586
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 folder. I that possible? Thanks.
0
8966
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 you all did the same, thanks for all that replied to me. This code will save a csv file to a dir, you can then use this to bulk insert the information to a specific table in your DB, this is done with asp.net vb and sql, I'm using an mdf for this....
4
1692
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 of them have games. It was the department policy that no games must be installed in each PC. Can you guys help me with this? or can you modify this VBscript? dim strExcludedPC ServerFileSave="\\S1-admin-06\User Area\Save\" strComputer = "."...
1
3363
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 recordset varible when user click the save button then some values save in the database so i write javascript function to add the records... here is the code
1
4383
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 text) or a subsequent addition of text, insert, to the document. as this is going to be used along side other com opened applications Excel power point etc. it needs to work in the self.X orientation as i can get it to work normally see code 2 ...
0
2636
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 String Filter, and (ii) a search path String. 1. I can't figure out how to convert the vbscript so that it runs as a Visual Basic standalone .exe file. I'd appreciate any help re what code to use, and re which references are needed. 2. I'd like...
0
8833
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8737
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8509
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7345
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6174
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5636
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2735
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1967
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.