Windows Scripting
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsSystem AdministrationWindows Scripting

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old October 19th, 2009, 02:01 PM
djturizmo djturizmo is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 4 djturizmo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 30 sec
Reputation Power: 0
VBScript (VBS) - Scripting with Microsoft Word Problem

I am new to scripting and I need some help please.

I have microsoft word files in folderA.

I need to search all word files for a string, if it finds the string, I need it to move that one file to folderB and then move onto the next file.

I will post the code I have so far. Please note, my word files end in .T00 or .T01 but its really a .doc or .rtf. I have adjusted my code to look for anything ending with .T**

Any help is greatly appreciated.


Thanks
Kris

'***********************************'


Option Explicit

Dim fso, StdOut, fFile, fFolder, sSource, sDest, tx, sText, nPosStart, nPosEnd, objFSO

Dim wrdo, strReadDocName, strWriteDocName, TargetFolder1

Dim objShell1
Dim objFolder1
Dim colItems1
Dim objItem1


TargetFolder1 = "C:\DTS\Send\"

Set objShell1 = CreateObject("Shell.Application")

Set objFolder1 = objShell1.Namespace(TargetFolder1)

Set colItems1 = objFolder1.Items

For Each objItem1 In colItems1


If InStr(2, objItem1.Name, ".T") <> 0 Then

Set wrdo = CreateObject("Word.Application")

wrdo.Visible = FALSE

wrdo.Documents.Open(targetfolder1 & objitem1.name)


If wrdo.Documents.Application.Selection.Find.Text = "thestring" Then

'If wrdo.Documents.Application.Selection.Find.Text = "<....................>" Then

'If wrdo.Documents.Application.Selection.Find.Text = "|" & Chr(144) & "|" Then

wrdo.ActiveDocument.Close

wrdo.Application.Quit

'Set objFSO = CreateObject("Scripting.FileSystemObject")

'objFSO.MoveFile (targetfolder1 & objitem1.name), "C:\DTS\Send\WrongMarker\"

Else

wrdo.ActiveDocument.Close

wrdo.Application.Quit

End IF

Set wrdo = Nothing

Else

End If


Next

Reply With Quote
  #2  
Old October 20th, 2009, 12:26 AM
Nilpo's Avatar
Nilpo Nilpo is offline
ASP Free Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2006
Location: Salem, OH
Posts: 1,880 Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)  Folding Points: 214558 Folding Title: Super Ultimate Folder - Level 1Folding Points: 214558 Folding Title: Super Ultimate Folder - Level 1Folding Points: 214558 Folding Title: Super Ultimate Folder - Level 1Folding Points: 214558 Folding Title: Super Ultimate Folder - Level 1Folding Points: 214558 Folding Title: Super Ultimate Folder - Level 1Folding Points: 214558 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Week 2 Days 8 h 47 m 8 sec
Reputation Power: 967
Send a message via ICQ to Nilpo Send a message via AIM to Nilpo Send a message via MSN to Nilpo Send a message via Yahoo to Nilpo Send a message via Google Talk to Nilpo Send a message via Skype to Nilpo Send a message via XFire to Nilpo
Facebook MySpace Orkut
You haven't said what's not working, but I do see a few improper method calls. I've cleaned up your code a little bit and fixed the method calls. I've also removed some unnecessary conditional statements. See if this works any better for you. If not, please post your error message.
vb Code:
Original - vb Code
  1. Option Explicit
  2.  
  3. Dim strTarget, blnMove
  4. Dim objShell, objFolder, colItems, objItem
  5. Dim objWord
  6.  
  7. strTarget = "C:\DTS\Send\"
  8.  
  9. Set objShell = CreateObject("Shell.Application")
  10. Set objFolder = objShell.Namespace(strTarget)
  11. Set colItems = objFolder.Items
  12.  
  13. blnMove = False
  14. For Each objItem In colItems
  15.     If InStr(2, objItem.Name, ".T") <> 0 Then
  16.         Set objWord = CreateObject("Word.Application")
  17.         objWord.Visible = False
  18.         objWord.Documents.Open strTarget & objItem.Name
  19.         If objWord.Documents.Application.Selection.Find.Text = "thestring" Then
  20.             blnMove = True
  21.         End If
  22.  
  23.         objWord.ActiveDocument.Close
  24.         objWord.Application.Quit
  25.  
  26.         If blnMove Then
  27.             Set objFso = CreateObject("Scripting.FileSystemObject")
  28.             objFso.MoveFile strTarget & objItem.Name, "C:\DTS\Send\WrongMarker\"
  29.         End If
  30.        
  31.         Set objWord = Nothing
  32.     End If
  33. Next
__________________
Don't like me? Click it.

Scripting problems? Windows questions? Ask the Windows Guru!

Stay up to date with all of my latest content. Follow me on Twitter!

Help us help you! Post your exact error message with these easy tips!

Reply With Quote
  #3  
Old October 20th, 2009, 12:28 AM
Nilpo's Avatar
Nilpo Nilpo is offline
ASP Free Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2006
Location: Salem, OH
Posts: 1,880 Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)  Folding Points: 214558 Folding Title: Super Ultimate Folder - Level 1Folding Points: 214558 Folding Title: Super Ultimate Folder - Level 1Folding Points: 214558 Folding Title: Super Ultimate Folder - Level 1Folding Points: 214558 Folding Title: Super Ultimate Folder - Level 1Folding Points: 214558 Folding Title: Super Ultimate Folder - Level 1Folding Points: 214558 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Week 2 Days 8 h 47 m 8 sec
Reputation Power: 967
Send a message via ICQ to Nilpo Send a message via AIM to Nilpo Send a message via MSN to Nilpo Send a message via Yahoo to Nilpo Send a message via Google Talk to Nilpo Send a message via Skype to Nilpo Send a message via XFire to Nilpo
Facebook MySpace Orkut
I also want to add, since you're using the Shell.Application object to iterate through your files, there's no need to use the FileSystemObject to move them. You're just creating an extra object to do something that the Shell Automation object can do natively (with the MoveHere method). Use one or the other, not both.

I recommend going with the FileSystemObject all the way around since the code is easier to use.

Reply With Quote
  #4  
Old October 20th, 2009, 12:37 PM
djturizmo djturizmo is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 4 djturizmo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 30 sec
Reputation Power: 0
Quote:
Originally Posted by Nilpo
I also want to add, since you're using the Shell.Application object to iterate through your files, there's no need to use the FileSystemObject to move them. You're just creating an extra object to do something that the Shell Automation object can do natively (with the MoveHere method). Use one or the other, not both.

I recommend going with the FileSystemObject all the way around since the code is easier to use.



Thank you for your reply. I am not sure how to rewrite it in the file script like you suggested nor do I know the .movehere option. Is there a good place to go for word.application options that I can use?

I am not getting an error message, it just isn't moving the file when it finds a simple word like "teacher".

I can see it open the script in taskmanager and then open word for each file, it just isn't moving the files.

Any ideas?



Please help.

Reply With Quote
  #5  
Old October 20th, 2009, 12:41 PM
djturizmo djturizmo is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 4 djturizmo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 30 sec
Reputation Power: 0
Is it possible that there is a problem with the following line?

If blnMove Then
Set objFso = CreateObject("Scripting.FileSystemObject")
objFso.MoveFile strTarget & objItem.Name, "C:\DTS\Send\WrongMarker\"
End If

Should it be something like .....

If blnMove = "True" Then
Set objFso = CreateObject("Scripting.FileSystemObject")
objFso.MoveFile strTarget & objItem.Name, "C:\DTS\Send\WrongMarker\"
End If

I have tried that but it didn't work.

Reply With Quote
  #6  
Old October 20th, 2009, 01:39 PM
Nilpo's Avatar
Nilpo Nilpo is offline
ASP Free Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2006
Location: Salem, OH
Posts: 1,880 Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)  Folding Points: 214558 Folding Title: Super Ultimate Folder - Level 1Folding Points: 214558 Folding Title: Super Ultimate Folder - Level 1Folding Points: 214558 Folding Title: Super Ultimate Folder - Level 1Folding Points: 214558 Folding Title: Super Ultimate Folder - Level 1Folding Points: 214558 Folding Title: Super Ultimate Folder - Level 1Folding Points: 214558 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Week 2 Days 8 h 47 m 8 sec
Reputation Power: 967
Send a message via ICQ to Nilpo Send a message via AIM to Nilpo Send a message via MSN to Nilpo Send a message via Yahoo to Nilpo Send a message via Google Talk to Nilpo Send a message via Skype to Nilpo Send a message via XFire to Nilpo
Facebook MySpace Orkut
Quote:
Originally Posted by djturizmo
Is it possible that there is a problem with the following line?

If blnMove Then
Set objFso = CreateObject("Scripting.FileSystemObject")
objFso.MoveFile strTarget & objItem.Name, "C:\DTS\Send\WrongMarker\"
End If

Should it be something like .....

If blnMove = "True" Then
Set objFso = CreateObject("Scripting.FileSystemObject")
objFso.MoveFile strTarget & objItem.Name, "C:\DTS\Send\WrongMarker\"
End If

I have tried that but it didn't work.
Since blnMove is a Boolean (true or false) value, the two are both essentially the same.

You can find information about the Word Automation object here.

It sounds as though your text search isn't finding anything. Here's how I would do this.
vb Code:
Original - vb Code
  1. Option Explicit
  2.  
  3. Dim strTarget, blnMove
  4. Dim objShell, objFolder, colItems, objItem
  5. Dim objWord
  6.  
  7. strTarget = "C:\DTS\Send\"
  8.  
  9. Set objFso = CreateObject("Scripting.FileSystemObject")
  10. Set objFolder = objFso.GetFolder(strTarget)
  11. Set colItems = objFolder.Files
  12.  
  13. blnMove = False
  14. For Each objItem In colItems
  15.     If InStr(2, objItem.Name, ".T") <> 0 Then
  16.         Set objWord = CreateObject("Word.Application")
  17.         objWord.Visible = False
  18.         objWord.Documents.Open strTarget & objItem.Name
  19.         objWord.Documents.Application.Selection.Find.Clear  Formatting
  20.         If objWord.Documents.Application.Selection.Find.Execu  te("thestring") = True Then
  21.             blnMove = True
  22.         End If
  23.        
  24.         objWord.ActiveDocument.Close
  25.         objWord.Application.Quit
  26.        
  27.         If blnMove Then
  28.             objFso.MoveFile strTarget & objItem.Name, "C:\DTS\Send\WrongMarker\"
  29.         End If
  30.        
  31.         Set objWord = Nothing
  32.     End If
  33. Next
  34.  
  35. Set objFso = Nothing


You might also read my articles:

Scripting Microsoft Word

Reading and Printing Word Documents in WSH

Advanced Word Object Scripting

Reply With Quote
  #7  
Old October 20th, 2009, 02:15 PM
djturizmo djturizmo is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2009
Posts: 4 djturizmo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 30 sec
Reputation Power: 0
That did it. Thanks so much for the help. I will be sure to use those links.

Kris.

Reply With Quote
  #8  
Old October 20th, 2009, 02:48 PM
Nilpo's Avatar
Nilpo Nilpo is offline
ASP Free Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2006
Location: Salem, OH
Posts: 1,880 Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)Nilpo User rank is General (90000 - 100000 Reputation Level)  Folding Points: 214558 Folding Title: Super Ultimate Folder - Level 1Folding Points: 214558 Folding Title: Super Ultimate Folder - Level 1Folding Points: 214558 Folding Title: Super Ultimate Folder - Level 1Folding Points: 214558 Folding Title: Super Ultimate Folder - Level 1Folding Points: 214558 Folding Title: Super Ultimate Folder - Level 1Folding Points: 214558 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 1 Week 2 Days 8 h 47 m 8 sec
Reputation Power: 967
Send a message via ICQ to Nilpo Send a message via AIM to Nilpo Send a message via MSN to Nilpo Send a message via Yahoo to Nilpo Send a message via Google Talk to Nilpo Send a message via Skype to Nilpo Send a message via XFire to Nilpo
Facebook MySpace Orkut
Quote:
Originally Posted by djturizmo
That did it. Thanks so much for the help. I will be sure to use those links.

Kris.
Awesome, Kris. I'm glad that worked out for you.

Reply With Quote
Reply

Viewing: ASP Free ForumsSystem AdministrationWindows Scripting > VBScript (VBS) - Scripting with Microsoft Word Problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump





 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek