I'm developing an Excel Add-in using VB.NET. On this Add-in, I create a new Ribbon, and inside this Ribbon there will be a Menu of Excel Workbooks and each line of the Menu should have a submenu for the Workbooks' Worksheets (Those file names, paths and sheets are being retrieved from DB).
I'm having problems trying to insert those Workbooks as Menu Items. I created a DAL to retrieve all data that I need and tested , it works, my problem is to add each element of the Workbook list. Any suggestions are welcome.
Public Class Ribbon1
Private Sub Ribbon1_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) Handles MyBase.Load
Dim listaWorkbooks As New List(Of Workbook)
Dim serviceExecuta As New ServiceExecuta
listaWorkbooks = serviceExecuta.BuscaWorkbooks()
For Each Workbook In listaWorkbooks
Menu1.Items.Add(Workbook.getNome)
Next
End Sub
End Class
I use a button to load the worksheets in to Combo boxes in the Ribbon...
Same way, you can check for workbooks as well.
please refer to below:
Private Sub Button1_Click(sender As Object, e As RibbonControlEventArgs) Handles Button19.Click
ComboBox1.Items.Clear()
Dim rdi As RibbonDropDownItem
Dim i As Integer
Dim nm() As String
For i = 1 To Globals.ThisAddIn.Application.Sheets.Count
ReDim nm(i)
nm(i) = Globals.ThisAddIn.Application.Sheets(i).Name
rdi = Globals.Factory.GetRibbonFactory().CreateRibbonDropDownItem()
rdi.Label = nm(i)
ComboBox1.Items.Add(rdi)
Next i
End Sub
May be you can try something like this :
For i = 1 To Globals.ThisAddIn.Application.Workbooks.Count
ReDim nm(i)
nm(i) = Globals.ThisAddIn.Application.Workbooks(i).Name
rdi = Globals.Factory.GetRibbonFactory().CreateRibbonDropDownItem()
rdi.Label = nm(i)
ComboBox1.Items.Add(rdi)
Next i
Good Luck!
Related
I am new to this forum. This is my first post, though I have spent a lot of time here searching for answers.
I have used VBA in Excel for many years, but have recently started using VB in Visual Studio 2015. I have created Form 1 as a MDIContainer and have another form opened inside this. This form (FormXYZ) contains a DataGridView.
Form1 has a MenuStrip and a I am currently trying to write the code, when one of these menu items is selected, to populate the DGV from a CSV. At this stage I am only trying to read the data and then I will work on the code to split the strings up.
Screenshot
I have no problem with selecting a file to import and the streamreader appears to read the file, but no data makes it to the DGV.
When I tried putting the code on FormXYZ for a button click event, the DGV was populated. So I believe the error is due to the way I am referencing the DGV, as the code for the MenuStrip_Click event is on Form1, but the DGV is on FormXYZ.
I would appreciate if somebody could point out where I am going wrong. My code is shown below.
Thanks Tepede
Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim FormXYZ As New FormXYZ()
FormXYZ.MdiParent = Me 'Set the Parent Form of the Child window.
FormXYZ.Show() 'Display the XYZ form.
End Sub
'-------------------------------------
'StripMenu click command to import CSV
Public Sub TSMIFileImportCSV_Click(sender As Object, e As EventArgs) Handles TSMIFileImportCSV.Click
Dim Filename As String
Dim RowValue As String
Dim OpenFile As OpenFileDialog = New OpenFileDialog()
'Open file dialog
With OpenFile
.Filter = "CSV (*.CSV)|*.csv"
.FilterIndex = 1
.InitialDirectory = "C:\"
.Title = "Open File"
.CheckFileExists = False
End With
If OpenFile.ShowDialog() = DialogResult.OK Then
Filename = OpenFile.FileName
End If
'---------
' Read CSV file content
Dim objReader As StreamReader = New StreamReader(Filename)
While objReader.Peek() <> -1
RowValue = objReader.ReadLine()
'Fist column is Boolean, the second should have the data from the CSV file
FormXYZ.DataGridView1.Rows.Add(True, RowValue, "Test", "Test")
End While
objReader.Close()
End Sub
It looks like you are losing your instance to the FormXYZ that you displayed due to it's definition being in the Form Load. Expand the scope of that variable to be at the class level.
Public Class Form1
Private FormXYZ As FormXYZ
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
FormXYZ = New FormXYZ()
FormXYZ.MdiParent = Me 'Set the Parent Form of the Child window.
FormXYZ.Show() 'Display the XYZ form.
End Sub
iam using vb.net 2008 ,i have 2 listboxes, one is for listing all forms name in my project and works fine, but what i need is when i click in the forms list, the other listbox should show me all the controls in the selected form name in listbox1! i have been trying and no luck.please help. this the code for listing all forms
Dim myAssembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
Dim types As Type() = myAssembly.GetTypes()
For Each myType As Object In types
If myType.BaseType.FullName.ToString.ToUpper = "SYSTEM.WINDOWS.FORMS.FORM" Then
ListBox1.Items.Add(myType.Name)
End If
Next
and this what i was trying in list2
Dim f As New Form
f.Name = ListBox1.SelectedItem.ToString
For Each c As Control In f.Controls
ListBox2.Items.Add(c.Name)
Next
OK! After several hours hunting - I hope this works in vb2008. It works in vb2015, but let's hope! The code to get a list of all forms came from another source. My problem in finding an answer has always resulted in a error in runtime casting- but finally this seems to work. Hopefully, it works for you as well.
Public Class Form1
Dim allforms() As Form
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
allforms = (From t As Type In Me.GetType().Assembly.GetTypes()
Where t.BaseType Is GetType(Form)
Let f = DirectCast(Activator.CreateInstance(t), Form)
Select f).ToArray
For Each frm As Form In allForms
ListBox1.Items.Add(frm.Name)
Next
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
For Each c As Control In allforms(ListBox1.SelectedIndex).Controls
ListBox2.Items.Add(c.Name)
Next
End Sub
End Class
I want the listbox to only display the data from the most recent click of the run button, instead of adding to the list every time. Here is what I have:
Public Class Form1
Private Sub RunButton_Click(sender As Object, e As EventArgs) Handles RunButton.Click
Dim EndDecimal As Decimal
Dim StepDecimal As Decimal
EndDecimal = Decimal.Parse(EndTextBox.Text)
StepDecimal = Decimal.Parse(StepTextBox.Text)
For j As Decimal = 0 To EndDecimal Step StepDecimal
ListBox1.Items.Add(j)
Next
End Sub
End Class
To clear the list, use Listbox1.Items.Clear() at the start of the click method. This will purge all items and only add the new ones to the listbox.
Here I have my vb6 code that plays around with an excel file. (I took this code off some other website.) This code compiles and builds but when I hit f5 it does the form does not po up. If I remove all the code in the Class then it pops up.
How would I fix this?
Sorry I am new at vb6, but not coding in general.
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
Dim xl As New Excel.Application
Dim xlsheet As Excel.Worksheet
Dim xlwbook As Excel.Workbook
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'the benifit of placing numbers in (row, col) is that you
'can loop through different directions if required. I could
'have used column names like "A1" 'etc.
'Text1.Text = xlsheet.Cells(2, 1) ' row 2 col 1
'Text2.Text = xlsheet.Cells(2, 2) ' row 2 col 2
'don't forget to do this or you'll not be able to open
'book1.xls again, untill you restart you pc.
xl.ActiveWorkbook.Close(False, "f:\a.xls")
xl.Quit()
End Sub
Private Sub Form_Load()
xlwbook = xl.Workbooks.Open("f:\a.xls")
xlsheet = xlwbook.Sheets.Item(1)
End Sub
Private Sub Form_Unload(Cancel As Integer)
xlwbook = Nothing
xl = Nothing
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
xlsheet.Cells(2, 1) = "adsfasdfasdf"
xlsheet.Cells(2, 2) = "qwerqwer"
xlwbook.Save()
'don't forget to do this or you'll not be able to open
'book1.xls again, untill you restart you pc.
xl.ActiveWorkbook.Close(False, "f:\a.xls")
xl.Quit()
End Sub
End Class
Then it means that you are not adding reference to your project
follow the link to code and further details..
http://vb.net-informations.com/excel-2007/vb.net_excel_2007_open_file.htm
check file name, path and extension carefully.
("f:\a.xls")
extension may be xlsx.
Also check the name of sheet 1.
I am trying to plot a chart in excel sheet using VB.
So now I am following the instructions given here
1- I started a new VB project in VS2010, called Excelgraph.
2- By default I got Form1.vb[Design].
3- On this Form I created a button by dragging it from the toolbox.
4-I doubled clicked it and new Form1.vb opens.
5- I removed everything that was automatically generated in this file i,e Form1.vb file and pasted the following code:
Updated Code
This is another code and is a latest one, compatible with Visual Basic 6.0.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim oXL As Object ' Excel application
Dim oBook As Object ' Excel workbook
Dim oSheet As Object ' Excel Worksheet
Dim oChart As Object ' Excel Chart
Dim iRow As Integer ' Index variable for the current Row
Dim iCol As Integer ' Index variable for the current Row
Const cNumCols = 10 ' Number of points in each Series
Const cNumRows = 2 ' Number of Series
ReDim aTemp(0 To cNumRows, 0 To cNumCols)
'Start Excel and create a new workbook
oXL = CreateObject("Excel.application")
oBook = oXL.Workbooks.Add
oSheet = oBook.Worksheets.Item(1)
' Insert Random data into Cells for the two Series:
Randomize(Now().ToOADate())
For iRow = 1 To cNumRows
For iCol = 1 To cNumCols
aTemp(iRow, iCol) = Int(Rnd() * 50) + 1
Next iCol
Next iRow
oSheet.Range("A1").Resize(cNumRows, cNumCols).Value = aTemp
'Add a chart object to the first worksheet
oChart = oSheet.ChartObjects.Add(50, 40, 300, 200).Chart
oChart.SetSourceData(Source:=oSheet.Range("A1").Resize(cNumRows, cNumCols))
' Make Excel Visible:
oXL.Visible = True
oXL.UserControl = True
End Sub
End Class
Update
I updated the code as shown above.
Error
'aTemp' is not declared. It may be inaccessible due to its protection level.
c:\users\ybf4 \documents\visual studio 2010\Projects\Excelgraph2
\Excelgraph2\Form1.vb
There were two more errors which I managed to remove. How do I remove this error?
I am compiling the above code on visual studio 2010 and Office is Office 2007.
A simple, trivial program reveals the error to be as I suspected - you can't change the size of something that doesn't exist!
As Derek said, you need to change the ReDim to Dim - OR you need to declare it first.
FAILS:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Const cNumCols = 10 ' Number of points in each Series
Const cNumRows = 2 ' Number of Series
ReDim aTemp(0 To cNumRows, 0 To cNumCols)
End Sub
PASSES:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Const cNumCols = 10 ' Number of points in each Series
Const cNumRows = 2 ' Number of Series
Dim aTemp
ReDim aTemp(0 To cNumRows, 0 To cNumCols)
End Sub
PASSES:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Const cNumCols = 10 ' Number of points in each Series
Const cNumRows = 2 ' Number of Series
Dim aTemp(0 To cNumRows, 0 To cNumCols)
End Sub
Hovering over aTemp should have told you this - it should also be underlined by a blue squiggly line to indicate a problem.
It's been a long time since I did this, but just looking at the code I suspect you need to change:
ReDim aTemp(0 To cNumRows, 0 To cNumCols)
To:
Dim aTemp(0 To cNumRows, 0 To cNumCols)
ReDim is used to re-dimension an array after it has been dimensioned (using the Dim statement)
That's remarkably old code (it's for VB3, so 4 generations before the first VB.Net, and Excel 5)
However, I believe your code should run fine if you just comment out the two lines indicated.
The randomness of the entries added to the worksheet may be different, but since you'll be replacing that code anyway (where Rnd() is used), it shouldn't matter. (I'm assuming your purpose isn't to generate random graphs)
As to DoEvents, I'm not sure that was ever necessary.