Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Problem inserting text into files

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,61691
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Problem inserting text into files
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Problem inserting text into files - 6/22/2008 6:18:42 AM   
  vbbeginner11

 

Posts: 5
Score: 0
Joined: 6/22/2008
Status: offline
Hey everyone,

I have this vbscript that scans a file and inserts text but it only scans one file.  I need it to scan through a folder and insert the text into all files in that directory.
script is below:

strDocument = "c:\Users\test\testfile.rtf"

Set objWord = CreateObject("Word.Application")
objWord.Visible = False
Set objDoc = objWord.Documents.Open(strDocument)
Set objSelection = objWord.Selection

intLine = 1
intTotal = objDoc.Paragraphs.Count
For i = 1 To intTotal
If i > 6 Then
 strPrepend = "ABC|" & intLine & "|ABC|"
 startRange = objDoc.Paragraphs(i).Range.Start
 endRange = objDoc.Paragraphs(i).Range.End
 Set tRange = objDoc.Range(startRange, endRange)
 strLine = tRange.Text
 strLine = Left(strLine, Len(strLine) - 1)
 If strLine = "" Then
  tRange.Text = strPrepend & vbCrLf
 Else
  tRange.Text = strPrepend & strLine & vbCrLf
 End If
 intLine = intLine + 1
End If
Next

objDoc.Save
objWord.Quit

Is there anyway I can get this script to scan all files in a directory and insert the text to all files instead of just one at a time?

Any help would be appreciated.

Thank you.
 
 
Post #: 1
 
 RE: Problem inserting text into files - 6/22/2008 6:46:31 AM   
  dm_4ever


Posts: 2664
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
Use FileSystemObject to loop through the files in a directory...pass their paths in and open each to append your text

...code
...creat word object
...get directory to loop through files
For Each objFile in colFiles
    Set objDoc = objWord.Documents.Open(objFile.Path)
    ...code
    objDoc.Save
Next

objWord.Quit

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to vbbeginner11)
 
 
Post #: 2
 
 RE: Problem inserting text into files - 6/22/2008 7:12:35 AM   
  vbbeginner11

 

Posts: 5
Score: 0
Joined: 6/22/2008
Status: offline
dm_4ever, thank you for your reply.  I kind of understand what you mean but not 100% sure.  Would you mind editing the script I posted to the way it should work?
I've tried editing it multiple times and keep getting a 'expected next'  error, code 800A03FC.

Thank you for your time and replay.

(in reply to dm_4ever)
 
 
Post #: 3
 
 RE: Problem inserting text into files - 6/22/2008 7:45:27 AM   
  dm_4ever


Posts: 2664
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
Start with something like this....

Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim strFolderPath : strFolderPath = "C:\Temp"
Dim objFolder : Set objFolder = objFSO.GetFolder(strFolderPath)
Dim objFile
For Each objFile In objFolder.Files
   WScript.Echo objFile.Path
Next

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to vbbeginner11)
 
 
Post #: 4
 
 RE: Problem inserting text into files - 6/22/2008 8:57:24 AM   
  vbbeginner11

 

Posts: 5
Score: 0
Joined: 6/22/2008
Status: offline
Ok, so my stupidity is stopping this script from fully running.

Here is what I have now:

Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim strFolderPath : strFolderPath = "C:\Users\dan\alyreport\"
Dim objFolder : Set objFolder = objFSO.GetFolder(strFolderPath)
Dim objFile
For Each objFile In objFolder.Files
    Set objFile = FSO.OpenTextFile(oFile.Path, intForReading, False)
strData = objFile.ReadAll
objFile.Close
Set objFile = Nothing
Set objWord = CreateObject("Word.Application")
objWord.Visible = False
Set objDoc = objWord.Documents.Open(objFile.Path)
Set objSelection = objWord.Selection

intLine = 1
intTotal = objDoc.Paragraphs.Count
For i = 1 To intTotal
If i > 6 Then
 strPrepend = "NTE|" & intLine & "|L|"
 startRange = objDoc.Paragraphs(i).Range.Start
 endRange = objDoc.Paragraphs(i).Range.End
 Set tRange = objDoc.Range(startRange, endRange)
 strLine = tRange.Text
 strLine = Left(strLine, Len(strLine) - 1)
 If strLine = "" Then
  tRange.Text = strPrepend & vbCrLf
 Else
  tRange.Text = strPrepend & strLine & vbCrLf
 End If
 intLine = intLine + 1
End If
Next

objDoc.Save
objWord.Quit


I replaced "For Each objFile in objFolder.Files  WScript objFile.Path"  because I didn't want to be prompt with the full path.  I thought this would work. 
Now when I run this I get   line 37 char 17  Expected Next    800A03FC.

The same message I was receiving before.  Any ideas what I am doing wrong?   I've also tried to make other changes but I would get an error saying objRequired. 

Thank you.

Sorry.

(in reply to dm_4ever)
 
 
Post #: 5
 
 RE: Problem inserting text into files - 6/22/2008 9:48:56 AM   
  dm_4ever


Posts: 2664
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
Maybe.... (untested)



      

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to vbbeginner11)
 
 
Post #: 6
 
 RE: Problem inserting text into files - 6/23/2008 12:35:01 AM   
  vbbeginner11

 

Posts: 5
Score: 0
Joined: 6/22/2008
Status: offline
Hey dm,

The ran perfect after the changes.  I ran it on my vista machine and everything went smooth.  I tried it on an XP machine and it keeps popping open microsoft office with the dialog box to save the files after the changes.  What could be different from running it on my vista machine or an xp machine.  Any ideas?

Thank you.

(in reply to dm_4ever)
 
 
Post #: 7
 
 RE: Problem inserting text into files - 6/23/2008 12:57:13 AM   
  vbbeginner11

 

Posts: 5
Score: 0
Joined: 6/22/2008
Status: offline
Sorry for the last post.  The file permission was read only so it would prompt office to save with a different file name.

Thank you dm for your help.

(in reply to vbbeginner11)
 
 
Post #: 8
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> Problem inserting text into files Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts