dotnet thoughts 

a dotnet developer's technical blog

Calling word Macros from .Net

While using Word automation with .Net sometimes it is required ti call, word macro’s from C# or VB.Net.

Here is the code to call word macros from VB.Net.

Dim WordApp As New Microsoft.Office.Interop.Word.Application
CType(WordApp, Object).GetType().InvokeMember("Run", Reflection.BindingFlags.Default Or Reflection.BindingFlags.InvokeMethod, Nothing, CType(WordApp, Object), New Object() {"helloworld"})

In the code, helloworld is the macro name.Or you can simply call WordApp.Run("helloworld") will also works fine.

 

You can also refer link from Microsoft
http://support.microsoft.com/kb/306683

Databinding in ASP.Net dropdownlist

In many forums, I used to see people asking for code to bind the dropdown list from basic collections. Here is the code to bind a Hash Table to ASP:Dropdown list, and setting the value of the selected item to the Text box. This is in ASP.Net 1.1.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  If Not Page.IsPostBack Then
  Dim students As New Hashtable
  students.Add("1", "Student1")
  students.Add("2", "Student2")
  students.Add("3", "Student3")
  students.Add("4", "Student4")
  students.Add("5", "Student5")
  StudentDropdown.DataTextField = "Value"
  StudentDropdown.DataValueField = "key"
  StudentDropdown.DataSource = students
  StudentDropdown.DataBind()
  End If
End Sub

And set the Autopostback property of the StudentDropdown to True. And in the SelectedIndexChanged event, write the code to set the value of the Dropdown to the Textbox.

Protected Sub StudentDropdown_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles StudentDropdown.SelectedIndexChanged
        If uxSample.SelectedIndex  <> -1 Then
            StudentId.Text = StudentDropdown.Items(StudentDropdown.SelectedIndex).Value
        End If
End Sub

Getting File size in .net 2.0

Code to get size of a file in bytes

Public Shared Function getFileSize(ByVal Filename as string) as Integer
return IO.File.ReadAllBytes(Filename).Length
End Function

I got some additional comments from my friend, Prasanth, to Get the File Version, using the System.Diagnostics namespace.

System.Diagnostics.FileVersionInfo.GetVersionInfo(FileName.ToString).FileVersion.ToString

Thanks to Prasanth.

Regular expression to remove HTML tags in VB.Net

Some time we may need to strip all the HTML tags from HTML files, like if we are doing some web scrapping. Here is simple Regular expression which will remove all the HTML Tags.

Public Sub RemoveHTMLTags(ByVal expression as string) as string
Dim pattern As String = "<(.|\n)*?>;"
Return System.Text.RegularExpressions.Regex.Replace(strHtmlString, pattern, String.Empty).Trim()
End Sub

Font Enumeration in VB.Net

If you are developing some editors is .Net, you have to list all the Fonts in you system in a Font Combo. Here is a simple code snippet which will display all the Windows Font in a Dropdown list.

For Each MyFont As FontFamily In System.Drawing.FontFamily.Families
Dropdownlist.Items.Add(MyFont.Name)
Next

It is in C#

foreach (FontFamily item in System.Drawing.FontFamily.Families)
{
this.comboBox1.Items.Add(item.Name);
}

Here is an Update. You can render the Dropdown such way that is will display all the font name, in the Font.

ComboBox lst = (ComboBox)sender;
e.DrawBackground();
Font font = new Font(lst.Items[e.Index].ToString(), 12);
e.Graphics.DrawString(lst.Items[e.Index].ToString(), font, Brushes.Black, e.Bounds, StringFormat.GenericDefault);
e.DrawFocusRectangle();

You need to write this code in the DrawItem event of the Combo box. Also you need to change the DrawMode property of the Combo box.

« Newer PostsOlder Posts »