首页 > 解决方案 > 打开文件夹中的所有word文档并将内容复制并粘贴回excel

问题描述

我有两个我尝试过的代码版本略有不同,不幸的是两者都不起作用。我需要一些帮助来弄清楚为什么以及如何做我需要做的事情谢谢

第一段代码不知何故立即结束了循环并且不满足初始条件表达式,尽管我不确定为什么,因为它应该调用该文件夹中的所有 .docx 文件

第二段代码抛出错误 Invalid Use of Property with this line Set wApp.Visible = True 我不知道为什么

第一个版本

Dim folder As String
Dim doc As Document

folder = "G:\GAV\Educational On Assignment Folder\On Assignment Tour Reports\2019\On Tour Questionnaire"
file = Dir(folder & "*.dox*")
r = 1

Do While Len(file) < 0
    Set doc = Documents.Open(Filename:=folder & file)
    ActiveDocument.Selection.WholeStory
    Selection.Copy
    Workbooks("Reports Excel").Activate
    Cells(1, r).Paste
    doc.Close
    r = r + 1
    file = Dire
Loop

第二版

Dim wApp As Word.Application
Dim wDoc As Word.Document
Dim mySource As Object
Set obj = CreateObject("Scripting.FileSystemObject")
Set mySource = obj.getfolder("G:\GAV\Educational On Assignment Folder\On Assignment Tour Reports\2019\On Tour Questionnaire")

For Each file In mySource.Files(Word.Application)
    If Len(file.Name) > 0 And InStr(1, file.Name, "$") = 0 Then
    Set wApp.Visible = True
    Set wDoc = wApp.Documents.Open(muSource & "\" & file.Name, , ReadOnly)
    ActiveDocument.Selection.WholeStory
    Selection.Copy
    Workbooks("Reports Excel").Activate
    Cells(1, r).Paste
    doc.Close
    r = r + 1
    wApp.Quit
    Set wApp = Nothing
    End If
Next file

我需要 Excel 打开文件夹中的每个文件,复制其全部内容并粘贴到 Excel 中的列。应该很简单

标签: excelvba

解决方案


推荐阅读