如何使用单词VBA解析.doc文件

时间:2014-09-18 15:32:00

标签: vba ms-word word-vba

我坚持使用这个单词VBA并需要一些帮助。我在一个文件夹中有160个单词文档,每个.doc包含至少一个短语,如'IO:'我想复制所有文件名后开始'IO:'并在光标找到Report Output:时停止复制。以下是一个示例输入:


`步骤名称:步骤3 - GP00BMDR

步骤描述:: GENISYS主批处理驱动程序,它处理外部事务和内部事务,更新主服务器,生成事务记录到记帐子系统并生成打印文件。

文件规格:     输入:1。GPFTRNW - PHGP.GPFTRNW.TRN.STD.KSDS

                   2. GPFSCIM – PHGP.GPFSCIM.SCI.KSDS

                   3. GPFSCSM – PHGP.GPFSCSM.SCS.KSDS


               IO: 1. GPFPDGT – PHGP.GPFPDGT.PDG.TRN.KSDS

                   2. GPFRTXT – PHGP.GPFRTXT.RTX.KSDS

报告输出:Nil`


所以我想在IO:之后复制.doc名称和文件名,并在光标到达Report Output:时停止。这是我的剧本:

Sub Ftp_Step_Details()

'this macro checks for FTP in respective steps and copy and writes in a cell along with the     corresponding JCL

Dim wordApplication As Word.Application
Dim wordDocument As Word.Document
Dim flag As String
Dim Folder As String, J As String, FLD As Object
Dim Objfile As Object
Dim objfso As Object
Dim intRow As String
Dim contents As String
flag = True
Dim intResult As Integer
Dim strPath As String
    'the dialog is displayed to the user
intResult = Application.FileDialog(msoFileDialogFolderPicker).Show
'checks if user has cancled the dialog
If intResult <> 0 Then
    'dispaly message box
    strPath = Application.FileDialog( _
        msoFileDialogFolderPicker).SelectedItems(1)
End If
 Set objExcel = CreateObject("Excel.Application")
 Set objWorkbook = objExcel.Workbooks.Open("D:\FILE-LIST\File-List.xlsx")
 objExcel.Visible = True
 objExcel.Workbooks.Add
 objExcel.Cells(1, 1).Value = "Jcl Name"
 objExcel.Cells(1, 2).Value = "File Names"
 'Folder = "D:\TEST-IO"  'JCL source goes here
 Set objfso = CreateObject("Scripting.FileSystemObject")
 Set wordApplication = CreateObject("Word.Application")
 intRow = 2
 'Opening the file in READ mode
Set FLD = objfso.GetFolder(strPath)
For Each file In FLD.Files
Set Objfile = wordApplication.Documents.Open(file)
   Do While Not Objfile.AtEndOfStream
       contents = Objfile.ReadLine
       If contents Like "*IO:" Then
           flag = True
        End If

       If contents Like "*Report Output:*" Then
           flag = False
       End If
       If flag = True Then
            objExcel.Cells(intRow, 1).Value = file.Name
            objExcel.Cells(intRow, 2).Value = contents3
            intRow = intRow + 1
       End If

     Loop
  Next
  Objfile.Close
   MsgBox "THANK YOU"
  End Sub

现在正在测试代码我在步骤TYPE MISMATCH中获得Set Objfile = wordApplication.Documents.Open(file)为什么会这样?

我有另一个疑问Readline功能也适用于单词VBA?

1 个答案:

答案 0 :(得分:0)

  

现在正在测试代码我在步骤Set Objfile = wordApplication.Documents.Open(file)中获得TYPE MISMATCH为什么会这样?

因为File是类型Scripting.File,它是一个Object,而Documents.Open方法需要一个字符串。

你可以尝试:

Documents.Open(file.Path)
  

我有另一个疑问,Readline功能也适用于单词VBA吗?

不,我不这么认为。

相关问题