I' m making program which export data from excel saved on disk to my textboxes in Form Application.
I have in code constant localization of excel.
Dim ExcelApp As Excel.Application = New Excel.Application
Dim ReteilerWorkbook As Excel.Workbook = ExcelApp.Workbooks.Open("C:\Users\TR\2.xlsx")
With OpenFileDialog1
.InitialDirectory = "C:\"
.ShowDialog()
End With
Now i want to add button with opendialog to choose the Excel from different localization.
You can choose the file you want manually by using the OpenFileDialog
Dim OpenFileDialog1 As New OpenFileDialog With {
.CheckFileExists = True
}
If OpenFileDialog1.ShowDialog = DialogResult.OK Then
Dim ExcelApp As Excel.Application = New Excel.Application
Dim ReteilerWorkbook As Excel.Workbook = ExcelApp.Workbooks.Open(OpenFileDialog1.FileName)
End If
Related
I have some blank instance of excel opened. I want to be able to close the instances of excel that does not have a workbook associated with it.
Public xlsApp As Excel.Application
Public xlsWB As Excel.Workbook
public openExcel()
Try
Dim path As String
path = "C:\excel.xlsm"
xlsWB = xlsApp.Workbooks.Open(path)
Catch ex As Exception
My.Application.Log.WriteException(ex, TraceEventType.Error, "Additional information or details")
Exit Sub
End Try
end sub
Public Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
====when run will open one excel, if excel window is close, the instance stays on, you will see the blank excel workbook. I want to close that instance when this sub re runs.
public doThis()
releaseObject(xlsApp)
releaseObject(xlsWB)
openExcel() <<==
end sub
For Excel to close properly you have to unload all of your COM objects
Dim xl As Excel.Application = New Excel.Application()
Dim wb As Excel.Workbook = xl.Workbooks.Add()
Dim ws As Excel.Worksheet = wb.Worksheets.Add()
wb.Close()
xl.Quit()
ReleaseComObject(ws)
ReleaseComObject(wb)
ReleaseComObject(xl)
ws = Nothing
wb = Nothing
xl = Nothing
If you aren't consuming the Excel instance and you are just displaying it then maybe try this. This will kill the process as soon as it is created so you'll want to put the p.Kill() in another place. Also note that this kills the process so no changes will be saved.
Dim p As System.Diagnostics.Process = System.Diagnostics.Process.Start(path)
p.Kill()
I am trying to list all the opened workbooks and their corresponding sheets in the task bar after that I should be able to select one workbook of the list and open it. My first try was to to show Excel process in Task-Manager but it only shows one open workbook without detecting the number of sheets.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim name As Process() = Process.GetProcessesByName("Excel")
For Each names In name
ListView1.Items.Add(names.MainWindowTitle)
If names.MainWindowTitle <> "" Then
ListBox1.Items.Add(names.MainWindowTitle)
End If
Next
End Sub
Next I tried to use this code but also I can only display one opened workbook, I don't know how to loop through them. I don't have problem of changing the whole code if you have any other method cause I am not sure that it's the solution.
Dim oXL As Microsoft.Office.Interop.Excel.Application
oXL = TryCast(System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application"), Microsoft.Office.Interop.Excel.Application)
oXL.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlMinimized
Dim y As String
y = oXL.ActiveWorkbook.Name
ListBox1.Items.Add(y)
Dim objExcel As Excel.Application = System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application")
Dim objWorkBook As Excel.Workbook = Nothing
Dim totalWorkBooks As Integer = objExcel.Workbooks.Count
For i As Integer = 1 To totalWorkBooks
objWorkBook = objExcel.Workbooks(i)
With objWorkBook
FullName = .FullName
OnlyName = .Name
Using a button in VB.NET form, an .xlsm file is opened and saved as .xlsx. The code in the .xlsx file (Workbook_BeforeClose event) is NOT deleted after the file is saved, therefore, when I want to close the file the code runs! After reopening the file there is no code left.
This is my VB.NET class:
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkbooks As Excel.Workbooks
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
xlApp = New Excel.Application
xlWorkbooks = xlApp.Workbooks
xlWorkBook = xlWorkbooks.Open("C:\Temp\testTemplate.xlsm")
xlApp.DisplayAlerts = False
xlWorkBook.SaveAs(Filename:="C:\Temp\testTemp.xlsx", FileFormat:=51) '51=xlOpenXMLWorkbook
xlApp.DisplayAlerts = True
xlApp.Visible = True
'Clean Up
releaseObject(xlWorkBook)
releaseObject(xlWorkbooks)
releaseObject(xlApp)
End Sub
Private Sub releaseObject(ByVal obj As Object)
Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
GC.WaitForPendingFinalizers()
End Try
End Sub
End Class
This is in the Excel file, workbook module:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
MsgBox "event still runs"
End Sub
How to save the file properly so that NO code would remain in it?
You're right, the code isn't deleted until after it's closed. I suppose you could set some type of flag in the xlsm's BeforeClose event that checks its file type and only runs if it's xlsm. Or you could do a Worksheets.Copy instead of the SaveAs and save the resulting workbook (which won't contain the VBA) as an xlsx, but there could be reference issues to clean up. Or you could set xlApp.EnableEvents=False, close the newly saved xlsx, set it back to True and re-open the xlsx.
Here's a post I wrote on the second option: http://yoursumbuddy.com/copy-an-xlsm-xlsx/
Hi there i am looking to use the print dialog box when printing from my VB program. I am trying to print Word Documents, i can successfully print one document, however if i specify more than one document in print dialog box then the printer will still only print one. All i need is to be able to choose how many documents i want to print.
Thanks
Below is my code for the relevant button:
Private Sub Button19_Click(sender As Object, e As EventArgs) Handles Button19.Click
Dim f As New OpenFileDialog
Dim p As New PrintDialog
Dim app As Word.Application
Dim doc As Word.Document
'Open file and print dialogs to get desired document and printer
If p.ShowDialog = Windows.Forms.DialogResult.OK Then
'Create instance of Word Application
app = New Word.Application
'Set Printer
app.WordBasic.FilePrintSetup(Printer:=p.PrinterSettings.PrinterName, DoNotSetAsSysDefault:=1)
'Set filename to object type
Dim filename As Object = f.FileName
Dim m As Object = System.Reflection.Missing.Value
'Open document
doc = app.Documents.Open("C:\CamJam\Tickets\Monday\drivingrange_mon_pm.docx")
'Print document
app.PrintOut()
'Close document
app.Documents.Close()
'Quit word application
app.Quit()
'Release
app = Nothing
End If
End Sub
I have a vb .net app which opens an Excel file and puts values in it. But if the user (who I have to assume is extremely dumb :) ), closes the workbook, next time my app will try to put a value to the file, it will show an error since there is no excel file open.
How can I either prevent excel from being closed, or disable the buttons when excel is closed?
I'm working with Excel 2003 on Windows 7
EDIT: Here is the code to open Excel
Private Sub Open_button_Click(sender As Object, e As EventArgs) Handles Open_button.Click
OpenFileDialog1.FileName = ""
OpenFileDialog1.Filter = "Excel files (*.xls)|*.xls"
OpenFileDialog1.ShowDialog()
filePath = OpenFileDialog1.FileName
If System.IO.File.Exists(filePath) Then
oExcel = CreateObject("Excel.Application")
oExcel.Visible = True
oBook = oExcel.Workbooks.Open(filePath)
oSheet = oBook.WorkSheets(1)
oExcel.Sheets(1).Select()
oExcel.ScreenUpdating = True
xlDown = Microsoft.Office.Interop.Excel.XlDirection.xlDown
xlUp = Microsoft.Office.Interop.Excel.XlDirection.xlUp
Me.Activate()
End If
End Sub
You can add event handler to detect the close event and disable the button in the handler.
From https://support.microsoft.com/en-us/kb/822750
Private EventDel_BeforeBookClose As Excel.AppEvents_WorkbookBeforeCloseEventHandler
Then after you have created the Excel objects
EventDel_BeforeBookClose = New Excel.AppEvents_WorkbookBeforeCloseEventHandler( _
AddressOf BeforeBookClose)
AddHandler xlApp.WorkbookBeforeClose, EventDel_BeforeBookClose
And add a sub to handle the event
Private Sub BeforeBookClose(ByVal Wb As Excel.Workbook, ByRef Cancel As Boolean)