通过VB检查路径文件夹 [英] Check path folder by vb

查看:42
本文介绍了通过VB检查路径文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取文件夹路径,然后在该文件夹中为Excel工作表循环

I am trying to get folderpath and then looping for Excel sheet in this folder

Imports Excel= Microsoft.Office.Interop.Excel

Public Sub Combine()

    Dim objApp As Excel.Application
    Dim objBook As Excel.Workbook
    Dim objSheet As Excel.Worksheet
    Dim myrange As Excel.Range
    Dim folderPath As String
    Dim fileName As String

    folderPath = @"FolderPath"
    fileName = Dir(folderPath & "*.xls")

    Do While (fileName <> "")

        objBook = objApp.Workbooks.Open(Filename:=Txt_GetPath.Text & fileName = True)
        objBook.Activate()

        For Each objSheet In objBook.Sheets
            objSheet.Copy(After:=objBook.Sheets(1))
        Next objSheet

        objBook.Close()

    Loop

End Sub

但是在 objBook = objApp.Workbooks.open(...)这一行,我得到了一个错误

But at the line objBook =objApp.Workbooks.open(...) I get an error of

路径必须是完整的

The path must be full

代码有什么问题?

推荐答案

要处理目录中的所有文件,可以使用

To process all the files in the directory, you can make use of the Directory.EnumerateFiles function.

在代码使用完毕后,处置Excel应用程序并不完全简单,因此我从为您添加了一些内容使用VB.NET处置Excel com对象的正确方法?.

It is not completely straightforward to dispose of the Excel application when the code has finished using it, so I added some in for you from The proper way to dispose Excel com object using VB.NET?.

我不知道您在打开工作簿的命令中使用 = True 的意图是什么,所以我将其遗漏了.

I do not know what you intended with the = True in the command to open the workbook, so I left it out.

Option Strict On

Imports System.IO
Imports Excel = Microsoft.Office.Interop.Excel

Public Sub DoCombine()

    Dim objApp As New Excel.Application
    Dim objBook As Excel.Workbook
    Dim objSheet As Excel.Worksheet

    Dim folderPath As String = Txt_GetPath.Text

    For Each excelFile In Directory.EnumerateFiles(folderPath, "*.xls")

        objBook = objApp.Workbooks.Open(Filename:=excelFile)
        objBook.Activate()

        For Each objSheet In objBook.Sheets
            objSheet.Copy(After:=objBook.Sheets(1))
        Next objSheet

        objBook.Close()

    Next

    objApp.Quit()

End Sub

Public Sub Combine()

    ' Call the method to do the actual work
    DoCombine()

    ' Now Let the GC clean up (twice, to clean up cycles too)
    GC.Collect()
    GC.WaitForPendingFinalizers()
    GC.Collect()
    GC.WaitForPendingFinalizers()

End Sub

使用 Combine()方法-它将调用您在 DoCombine()方法中使用的代码,并确保没有很多Excel副本在计算机上运行-即使它们不在屏幕上,您也会在任务管理器中看到它们.

Use the Combine() method - it will call the code you use in the DoCombine() method and make sure that there are not lots of copies of Excel left running on the computer - you would see them in Task Manager even if they were not on the screen.

使用 Option Strict On (几乎总是)是一个好主意,因为它让Visual Studio向您显示代码中有些地方不正确.

It is (almost) always a good idea to use Option Strict On because it lets Visual Studio show you where some things are wrnog in code.

这篇关于通过VB检查路径文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆