我正在創建一個採用Excel文件的程序,在其上標記信息並將其保存在文件位置。使用現有的Excel文件保存數據並以新名稱保存
我可以很容易地創建一個新的Excel工作表,把它的信息,然後將其保存到文件位置。這不是我需要的。在表單中,我希望它能夠拉取我創建的現有空白Excel文件模板,將表單中輸入的信息加蓋到它上面,重命名文件並將其保存在文件位置(類似於「另存爲」)。這樣,最初就會有一個空白的主模板文件。
我不知道如何抓住該Excel文件和而不是創建一個新的Excel文件。
下面是一些示例代碼:
If EmployeeInfo.empNameTextBox.Text = "" Or EmployeeInfo.dateBox.Text = "" Then
'prompt user must include name and date at least to save
MessageBox.Show("In order to save a file, you must include the name AND the date", "Fill in Name/Date!",
MessageBoxButtons.OK, MessageBoxIcon.Error)
'minimize the password form and open back up the EmployeeInfo form
EmployeeInfo.Show()
Me.Hide()
Else
'create and save the excel file
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 = "Last Name"
oSheet.Range("B1").Value = "First Name"
oSheet.Range("A1:B1").Font.Bold = True
oSheet.Range("A2").Value = "Litoris"
oSheet.Range("B2").Value = "Mike"
'Save the Workbook and Quit Excel
oBook.SaveAs("N:\IT\Device Images\Incomplete\" + EmployeeInfo.empNameTextBox.Text + EmployeeInfo.dateBox.Text)
oExcel.Quit
'minimize this form and go back to main form
ImageTool.Show()
Me.Hide()
End If
爲了證實,我並不想開始一個新的Excel工作簿和無法弄清楚如何拉我創造了我現有的文件。