[英]Check path folder by vb
I am trying to get folderpath and then looping for Excel sheet in this folder我正在尝试获取文件夹路径,然后在此文件夹中循环 Excel 工作表
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
But at the line objBook =objApp.Workbooks.open(...)
I get an error of但在
objBook =objApp.Workbooks.open(...)
我得到一个错误
The path must be full
路径必须是完整的
What is wrong with the code?代码有什么问题?
To process all the files in the directory, you can make use of the Directory.EnumerateFiles function.要处理目录中的所有文件,您可以使用Directory.EnumerateFiles函数。
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?在代码使用完毕后处理 Excel 应用程序并不完全简单,因此我从使用 VB.NET 处理 Excel com 对象的正确方法中为您添加了一些内容? .
.
I do not know what you intended with the = True
in the command to open the workbook, so I left it out.我不知道您在打开工作簿的命令中使用
= True
目的是什么,所以我把它省略了。
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
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.使用
Combine()
方法——它会调用你在DoCombine()
方法中使用的代码,并确保计算机上没有大量的Excel 副本在运行——你会在任务管理器中看到它们,即使它们没有屏幕上。
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.使用
Option Strict On
(几乎)总是一个好主意,因为它可以让 Visual Studio 向您展示代码中的某些内容。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim strFileName As String
Dim xl As New Excel.Application
Dim wb As Excel.Workbook
Dim ws As Excel.Worksheet
If FolderBrowserDialog1.ShowDialog = System.Windows.Forms.DialogResult.OK Then
Dim Directory = FolderBrowserDialog1.SelectedPath
Dim Files() As System.IO.FileInfo
Dim DirInfo As New System.IO.DirectoryInfo(Directory)
Files = DirInfo.GetFiles("*", IO.SearchOption.AllDirectories)
For Each File In Files
wb = xl.Workbooks.Open(File.FullName)
For Each ws In wb.Sheets
strFileName = Microsoft.VisualBasic.Left(File.Name,
(InStrRev(File.Name, ".", -1, vbTextCompare) - 1))
If ws.Name = "SCHEDULE" Then
ws.Range("S7", "S50").Value2 = strFileName
End If
Next
wb.Close()
Next
xl.Quit()
End If
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.