首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用word VBA解析.doc文件

如何使用word VBA解析.doc文件
EN

Stack Overflow用户
提问于 2014-09-18 15:32:00
回答 1查看 1.4K关注 0票数 0

我在一个文件夹中有160个word文档,每个.doc至少包含一个短语,比如'IO:',我希望复制在'IO:'之后开始的所有文件名,并在光标找到Report Output:时停止复制。以下是一个示例输入:

`步骤名称:步骤3- GP00BMDR

Step描述: GENISYS的主要批处理驱动程序,它处理外部事务和内部事务,更新主程序,生成到会计子系统的事务记录,并生成打印文件。

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

代码语言:javascript
复制
                   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` `

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

代码语言:javascript
复制
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函数是否也适用于word VBA?

EN

回答 1

Stack Overflow用户

发布于 2014-09-18 15:36:00

现在,当测试代码时,我在步骤Set Objfile = wordApplication.Documents.Open(file)中得到了类型错配,为什么呢?

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

你可以试试:

代码语言:javascript
复制
Documents.Open(file.Path)

我还有另一个疑问,读行功能在word VBA中也能工作吗?

不,我想不是这样的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25916837

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档