Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在vba中打开现有的excel文件?_Excel_Vb.net - Fatal编程技术网

如何在vba中打开现有的excel文件?

如何在vba中打开现有的excel文件?,excel,vb.net,Excel,Vb.net,我想知道如何使用现有用户名打开现有excel文件。e、 g如果用户名输入为louis,并且它是一个新用户名,则会创建一个excel文件louis.xlsx。然后,如果用户名再次是Louis,我想打开同一个文件,因为它已经存在 我的代码如下: Dim SourcePath As String = "D:\VisualStudio\JuegoDIF\" + userName + ".xlsx" Dim SaveDirectory As String = &qu

我想知道如何使用现有用户名打开现有excel文件。e、 g如果用户名输入为
louis
,并且它是一个新用户名,则会创建一个excel文件
louis.xlsx
。然后,如果用户名再次是Louis,我想打开同一个文件,因为它已经存在

我的代码如下:

Dim SourcePath As String = "D:\VisualStudio\JuegoDIF\" + userName + ".xlsx"
Dim SaveDirectory As String = "D:\VisualStudio\JuegoDIF\"

Dim Filename As String = System.IO.Path.GetFileName(SourcePath)
Dim SavePath As String = System.IO.Path.Combine(SaveDirectory, Filename)

If System.IO.File.Exists(SavePath) Then
    'Files exists
              
    'RIGHT HERE IS WHERE I SHOULD WRITE THE IMPLEMENTATION
Else
    'File Don't Exists
    'Creating an Excel File with user information

    Dim oExcel As Object
    Dim oBook As Object
    Dim oSheet As Object

    'Start a new workbook in Excel
    oExcel = CreateObject("Excel.Application")
    oBook = oExcel.Workbooks.Add

    'Add data to cells of the first worksheet in the new workbook
    oSheet = oBook.Worksheets(1)

    oSheet.Range("A1").Value = "Date"
    oSheet.Range("A2").Value = Now
    oSheet.Range("B1").Value = "Category"
    oSheet.Range("C1").Value = "correct"
    oSheet.Range("D1").Value = "answer"
    oSheet.Range("E1").Value = "Time"

    Dim usuario As String
    usuario = UserName

    'Format Cells
    oSheet.Range("A1:E1").Font.Bold = True
    'Save the Workbook and Quit
    oBook.SaveAs ("D:\VisualStudio\JuegoDIF\" + UserName + ".xlsx")
    oExcel.Quit
End If

@悉达多我明白了。。。。是的,Vb.Net索引我删除了我上面的评论,因为那是针对VBA的。请参阅:)使用方法
oBook=oExcel.Workbooks.Open()
@SiddharthRout非常感谢,我要尝试一下