逐行阅读Word文档 [英] Reading word document line by line

查看:123
本文介绍了逐行阅读Word文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是VBScript的新手.我使用VBScript逐字读取MSWord文件.这是我的脚本代码.

Hi, I am new in VBScript. i used a VBScript for reading MSWord file word by word. here is the code of my Script.

Option Explicit
REM We use "Option Explicit" to help us check for coding mistakes
REM the Word Application
Dim objWord
REM the path to the Word file
Dim wordPath
REM the document we are currently reading data from
Dim currentDocument
REM the number of Words in the current document
Dim numberOfWords
Dim i
Dim objDialog
Set objDialog = CreateObject("UserAccounts.CommonDialog")
objDialog.Filter = "VBScript Scripts|*.vbs|All Files|*.*"
objDialog.FilterIndex = 1
objDialog.InitialDir = "C:\"
intResult = objDialog.ShowOpen
If intResult = 0 Then
    Wscript.Quit
Else
    Wscript.Echo objDialog.FileName
End If
REM where is the Word file located?
wordPath = "C:\Documents and Settings\sapuser\Desktop\harry.doc"
WScript.Echo "Extract Data from " & wordPath
REM Create an invisible version of Microsoft Word
Set objWord = CreateObject("Word.Application") 
REM don't display any messages about documents needing to be converted
REM from  old Word file formats
objWord.DisplayAlerts = 0
REM open the Word document as read-only
REM open (path, confirmconversions, readonly
objWord.Documents.Open wordPath, false, true
REM Access the document
Set currentDocument = objWord.Documents(1)
REM How many words are in the document
NumberOfWords = currentDocument.words.count               
WScript.Echo "There are " & NumberOfWords & " words " & vbCRLF
For i = 1 to NumberOfWords
	WScript.Echo currentDocument.words(i)
Next
REM Close the document
currentDocument.Close
REM Free memory used to store the document object
Set currentDocument = Nothing
REM exit Microsoft Word
objWord.Quit
Set objWord = Nothing



在这一行中,我们计算了我的doc文件中的单词总数.
NumberOfWords = currentDocument.words.count

并且此for循环逐字显示我的doc文件的内容



In this line we count the total number of words in my doc files
NumberOfWords = currentDocument.words.count

and this for loop display the contents of my doc file word by word

For i = 1 to NumberOfWords<br />
	WScript.Echo currentDocument.words(i)<br />
Next<br />



但我想逐行阅读我的doc文件.有没有能获得一行而不是单词的功能?
请建议我一种逐行显示我的文档的方法.

谢谢.



But I want to read my doc file line by line. Is there any function which gets a line rather than a word?
Please suggest me a way to get a line by line display of my doc.

Thanks.

推荐答案

尝试使用Paragraphs
Try using Paragraphs
For Each p In currentDocument.Paragraphs
    WScript.Echo p.Range.Text
Next p


这篇关于逐行阅读Word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆