我有一个word文档,其中包含我想解析为excel文件的数据。源文件长数百页。我一直在使用VBA,但是我刚开始学习该语言,并且在尝试输入.doc文件时遇到了很多困难。我已经能够使用Open和Line Input语句从.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
感谢您的所有帮助和建议
打开和输入命令通常仅适用于纯文本文件(您可以在记事本中阅读的内容)。如果要以编程方式读取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没有“线”的概念。您可以阅读文本范围,段落和句子。实验并找到最有效的方法来使您的输入文本易于管理。
这里是实际起作用的代码。