我有一个 Word 文档,其中包含我想要解析为 Excel 文件的数据。源文件长达数百页。我一直在使用 VBA,但我刚刚开始学习该语言,并且在尝试输入 .doc 文件时遇到了很多困难。我已经能够使用Open和线路输入语句从 .txt 文件中检索,但当我尝试 .doc 文件时只有乱码。
我提供了两个屏幕截图链接。
第一个是我的输入数据示例的屏幕截图。
http://img717.imageshack.us/i/input.jpg/
第二个是我想要的输出的屏幕截图。
http://img3.imageshack.us/i/outputg.jpg/
我已经开发了一种我想要完成的算法。我只是在编码方面遇到困难。下面是我开发的伪代码。
Variables:
string line = blank
series_title = blank
folder_title = blank
int series_number = 0
box_number = 0
folder_number = 0
year = 0
do while the <end_of_document> has not been reached
input line
If the first word in the line is “series”
store <series_number>
store the string after “:”into the <series_title>
end if
call parse_box(rest of line)
output < series_number > <series_title> < box_number > < folder_number ><folder_title> <year>
end do while
function parse_box(current line)
If the first word in the line is “box”
store <box_number>
end if
call parse_folder(rest of line)
end function
function parse_folder(current line)
If first word is “Folder”
store <folder_number>
end if
call parse_folder_title(rest of line)
end function
function parse_folder_title_and_year(current line)
string temp_folder_title
store everything as <temp_folder_title> until end of line
if last word in <temp_folder_title> is a year
store <year>
end if
if < temp_folder_title> is empty/blank
//use <folder_title> from before
else
<folder_title> is < temp_folder_title> minus <year>
end if
end parse_folder_title_and_year
提前感谢您的所有帮助和建议
fopen 和 input 命令通常仅适用于纯文本文件(您可以在记事本中阅读的内容)。如果您想以编程方式读取 Microsoft Word 文档,则必须将 Microsoft Word 12.0 对象库(或系统上的最新版本)添加到 VBAProject 引用中,并使用 Word API 打开和阅读文档。
Dim odoc As Word.Document
Set odoc = oWrd.Documents.Open(Filename:=DocumentPath, Visible:=False)
Dim singleLine As Paragraph
Dim lineText As String
For Each singleLine In ActiveDocument.Paragraphs
lineText = singleLine.Range.Text
'Do what you've gotta do
Next singleLine
Word没有“线”的概念。您可以阅读文本范围、段落和句子。进行实验并找到最适合将输入文本放入可管理块中的方法。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)