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 1st, 2008, 05:17 AM
xfactor20 xfactor20 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 2 xfactor20 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 12 sec
Reputation Power: 0
Question WSH - Using the Word Automation Object

Pardon the foolish question. Can you complete the "recursive read" function below. I am trying to get the contents of the Word file. Thanks in advance.



Quote:
Originally Posted by Nilpo
The problem here is that Word files are not plain text files. You need to be able to handle the Word formatting. In order to do that, your best bet is to use Microsoft Word itself.

You can create an instance of Microsoft Word by instantiating it's ActiveX control object:
vb Code:
Original - vb Code
  1. Set objWord = CreateObject("Word.Application")
Now you can open a file by using the Documents.Open() method which returns a file object:
vb Code:
Original - vb Code
  1. Set objDoc = objWord.Documents.Open("C:\myfolder\mydoc.doc")
At this point, you can treat your object as a standard text file and use any of VBScript's Read methods. Don't forget that you will still contain special characters such as LF's.

Reply With Quote
  #2  
Old October 1st, 2008, 09:52 AM
Nilpo's Avatar
Nilpo Nilpo is offline
ASP Free Beginner (1000 - 1499 posts)
 
Join Date: Jun 2006
Location: Salem, OH
Posts: 1,254 Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)  Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 6 Days 10 h 25 m 23 sec
Reputation Power: 667
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 xfactor20
Pardon the foolish question. Can you complete the "recursive read" function below. I am trying to get the contents of the Word file. Thanks in advance.
This is the best way to read a Word Document. Although this should not be used for extremely large files.
vb Code:
Original - vb Code
  1. Const wdDoNotSaveChanges = 0
  2.  
  3. strDocument = "C:\mydoc.docx"
  4.  
  5. Set objWord = CreateObject("Word.Application")
  6. objWord.Visible = False
  7. objWord.DisplayAlerts = False
  8. objWord.Documents.Open strDocument,, True
  9. Set objDoc = objWord.ActiveDocument
  10.  
  11. Set objRange = objDoc.Content  'Is equivalent to: Set objRange = Range()
  12. strContents = objWord.CleanString(objRange.Text)
  13.  
  14. objWord.Quit wdDoNotSaveChanges
  15.  
  16. MsgBox strContents
__________________
Click the image if at any point you don't like my decision.

Scripting problems? Windows questions? Ask the Windows Guru!


Reply With Quote
  #3  
Old October 2nd, 2008, 12:44 AM
xfactor20 xfactor20 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2008
Posts: 2 xfactor20 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 12 sec
Reputation Power: 0
Modify MS Word file

Thanks much, this is clarifying. However, I am having some continued problems with this object model. I am trying to: 1) Open a word document; 2) read line by line; 3) insert text as I come across matches to my constant strings that I have defined and 4) Write all of the new text out to a word file. Any further assistance you can provide is highly appreciated.




Quote:
Originally Posted by Nilpo
This is the best way to read a Word Document. Although this should not be used for extremely large files.
vb Code:
Original - vb Code
  1. Const wdDoNotSaveChanges = 0
  2.  
  3. strDocument = "C:\mydoc.docx"
  4.  
  5. Set objWord = CreateObject("Word.Application")
  6. objWord.Visible = False
  7. objWord.DisplayAlerts = False
  8. objWord.Documents.Open strDocument,, True
  9. Set objDoc = objWord.ActiveDocument
  10.  
  11. Set objRange = objDoc.Content  'Is equivalent to: Set objRange = Range()
  12. strContents = objWord.CleanString(objRange.Text)
  13.  
  14. objWord.Quit wdDoNotSaveChanges
  15.  
  16. MsgBox strContents

Reply With Quote
  #4  
Old October 2nd, 2008, 03:59 AM
Nilpo's Avatar
Nilpo Nilpo is offline
ASP Free Beginner (1000 - 1499 posts)
 
Join Date: Jun 2006
Location: Salem, OH
Posts: 1,254 Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)Nilpo User rank is Brigadier General (60000 - 70000 Reputation Level)  Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1Folding Points: 206875 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 6 Days 10 h 25 m 23 sec
Reputation Power: 667
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 xfactor20
Thanks much, this is clarifying. However, I am having some continued problems with this object model. I am trying to: 1) Open a word document; 2) read line by line; 3) insert text as I come across matches to my constant strings that I have defined and 4) Write all of the new text out to a word file. Any further assistance you can provide is highly appreciated.
You first need to understand that a Word file is not a text file. Text files have definite line endings. Word files do not necessarily have definite line endings. They also contain markup that prevent them from being read as plain text. Thus, the Word Automation object does not treat them like text files. It treats them like Word documents. Let me take your points one at a time.
Quote:
Originally Posted by xfactor20
1) Open a word document;
Opening a Word document is as simple as creating an instance of the Word automation object and using it's Open method. There are various other properties that control the programs behavior.
vb Code:
Original - vb Code
  1. Const wdDoNotSaveChanges = 0
  2.  
  3. strDocument = "C:\mydoc.docx"
  4.  
  5. Set objWord = CreateObject("Word.Application")
  6. objWord.Visible = False
  7. objWord.DisplayAlerts = False
  8. objWord.Documents.Open strDocument,, True
  9. Set objDoc = objWord.ActiveDocument
Here I've also showed you how to access the Document object as well.
Quote:
Originally Posted by xfactor20
2) read line by line
Again, you shouldn't attempt to read Word files line by line. You can easily access the entire contents of the document using the Document object.
vb Code:
Original - vb Code
  1. Set objRange = objDoc.Content
  2. strContents = objWord.CleanString(objRange.Text)
For larger documents, you should move through smaller ranges by reading one paragraph at a time.
vb Code:
Original - vb Code
  1. Set colParagraphs = objDoc.Paragraphs
  2. For Each objParagraph In colParagraphs
  3.     strParaText = objParagraph.Range.Text
  4. Next
Quote:
Originally Posted by xfactor20
3) insert text as I come across matches to my constant strings that I have defined
Again, there's no need to work line by line. There's really no need to even read in any text at all. You can use Word's own Find and Replace tool instead.
vb Code:
Original - vb Code
  1. Const wdDoNotSaveChanges = 0
  2. Const wdFindContinue = 1
  3. Const wdReplaceAll = 2
  4.  
  5. strFindText = "Text to find"
  6. strReplaceText = "Text to replace with"
  7.  
  8. Set objWord = CreateObject("Word.Application")
  9. objWord.Visible = False
  10.  
  11. Set objDoc = objWord.Documents.Open("mydoc.doc")
  12. Set objSelection = objWord.Selection
  13.  
  14. With objSelection.Find
  15.     .MatchWholeWord = vbTrue
  16.     .MatchCase = vbFalse
  17.     .Wrap = wdFindContinue
  18.     .Forward = vbTrue
  19.     .Text = strFindText
  20.     .Replacement.Text = strReplaceText
  21.     blnFound = .Execute(,,,,,,,,,, wdReplaceAll)
  22.    
  23.     If blnFound Then
  24.         intMatches = .Found
  25.     Else
  26.         intMatches = 0
  27.     End If
  28. End With
  29.  
  30. objDoc.Close
  31. objWord.Quit wdDoNotSaveChanges
  32.  
  33. WScript.Echo intMatches & " instances were replaced."
Word allows you to control all of the search and replace options as well. Here are the constants you may find useful.
vb Code:
Original - vb Code
  1. 'WdReplace
  2. Const wdReplaceNone = 0   'Only Find matches
  3. Const wdReplaceOne = 1   'Only replace first instance
  4. Const wdReplaceAll = 2   'Replace all matches
  5.  
  6. 'WdFindWrap
  7. Const wdFindStop = 0   'Stop when the end of the document is reached
  8. Const wdFindContinue = 1   'Continue from the beginning if the search began in the middle of the document
  9. Const wdFindAsk = 2
Quote:
Originally Posted by xfactor20
4) Write all of the new text out to a word file.
There's no need to "write the new text out to a word file". You can actually use the Word automation object to save a new document. The Document object provides access to Word's familiar Save As command.
vb Code:
Original - vb Code
  1. objDoc.SaveAs("C:\newfilename.doc")
Comments on this post
micky agrees: great explaination

Reply With Quote
Reply

Viewing: ASP Free ForumsSystem AdministrationWindows Scripting > WSH - Using the Word Automation Object


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.