Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将word文档解析为excel文件_Excel_Vba_Programming Languages_Ms Word - Fatal编程技术网

将word文档解析为excel文件

将word文档解析为excel文件,excel,vba,programming-languages,ms-word,Excel,Vba,Programming Languages,Ms Word,我有一个word文档,其中包含要解析为excel文件的数据。源文件有数百页长。我一直在使用VBA,但我刚刚开始学习该语言,在尝试输入.doc文件时遇到了很多困难。我已经能够使用打开和行输入语句从.txt文件中检索,但在尝试.doc文件时,只会出现乱码 我已经包括了两个屏幕截图链接 第一个是我输入数据样本的屏幕截图。 第二个是我所需输出的屏幕截图。 我已经开发了一个算法来实现我的目标。我只是在编码上有困难。下面是我开发的伪代码 Variables: string

我有一个word文档,其中包含要解析为excel文件的数据。源文件有数百页长。我一直在使用VBA,但我刚刚开始学习该语言,在尝试输入.doc文件时遇到了很多困难。我已经能够使用打开行输入语句从.txt文件中检索,但在尝试.doc文件时,只会出现乱码

我已经包括了两个屏幕截图链接

第一个是我输入数据样本的屏幕截图。

第二个是我所需输出的屏幕截图。

我已经开发了一个算法来实现我的目标。我只是在编码上有困难。下面是我开发的伪代码

    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
变量:
字符串行=空白
系列标题=空白
文件夹标题=空白
int系列_编号=0
框号=0
文件夹编号=0
年份=0
在未到达目标位置时执行此操作
输入线
如果行中的第一个字是“系列”
百货商店
将“:”之后的字符串存储到
如果结束
调用解析框(行的其余部分)
输出<序列号><盒号><文件夹号>
结束时间
函数解析框(当前行)
如果行中的第一个单词是“box”
百货商店
如果结束
调用parse_文件夹(行的其余部分)
端函数
函数parse_文件夹(当前行)
如果第一个单词是“文件夹”
百货商店
如果结束
调用parse_文件夹_标题(行的其余部分)
端函数
函数解析文件夹标题和年份(当前行)
字符串临时文件夹标题
将所有物品储存到生产线末端
如果最后一句话是一年
百货商店
如果结束
如果为空/空白
//从前使用
其他的
是减去
如果结束
结束解析文件夹标题和年份

提前感谢您的所有帮助和建议

fopen和input命令通常只适用于纯文本文件(您可以在记事本中阅读的内容)。如果要以编程方式读取Microsoft word文档,则必须将Microsoft word 12.0对象库(或系统上的最新版本)添加到VBA项目引用中,并使用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没有“线”的概念。您可以阅读文本范围、段落和句子。尝试并找出最适合在可管理块中获取输入文本的方法。

fopen和input命令通常只适用于纯文本文件(您可以在记事本中读取的内容)。如果要以编程方式读取Microsoft word文档,则必须将Microsoft word 12.0对象库(或系统上的最新版本)添加到VBA项目引用中,并使用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没有“线”的概念。您可以阅读文本范围、段落和句子。尝试并找出最适合将输入文本置于可管理块中的方法。

以下是实际有效的代码

'Create a New Object for Microsoft Word Application
Dim objWord As New Word.Application
'Create a New Word Document Object
Dim objDoc As New Word.Document
'Open a Word Document and Set it to the newly created object above
Set objDoc = objWord.Documents.Open(Filename:=DocFilename, Visible:=False)

Dim strSingleLine As Paragraph
Dim strLineText As String

For Each strSingleLine In objDoc.Paragraphs
    strLineText = strSingleLine.Range.Text
    'Do what you've gotta do
Next strSingleLine

下面是实际工作的代码

'Create a New Object for Microsoft Word Application
Dim objWord As New Word.Application
'Create a New Word Document Object
Dim objDoc As New Word.Document
'Open a Word Document and Set it to the newly created object above
Set objDoc = objWord.Documents.Open(Filename:=DocFilename, Visible:=False)

Dim strSingleLine As Paragraph
Dim strLineText As String

For Each strSingleLine In objDoc.Paragraphs
    strLineText = strSingleLine.Range.Text
    'Do what you've gotta do
Next strSingleLine

这个答案缺少“oWrd”的定义,我猜它是Set oWrd=CreateObject(“Word.Application”)这个答案缺少“oWrd”的定义,我猜它是Set oWrd=CreateObject(“Word.Application”)