Archive for July, 2011

Tag Generator for Live Writer: Problems and Workarounds

One topic of emails and questions I receive are in relation to the Tag Generator, a plugin available for download, for Live Writer, a blog writing tool.

First some history. The Tag Generator was originally written over two years ago to provide myself a way to automatically generate tags for my blog posts. I got tired of doing it manually. It worked well for me so I decided to create a plugin that others could use. It uses the Microsoft Word thesaurus to verify words in the blog and their meanings. It isn’t perfect but its better than nothing. Obviously some of you agreed as over 14,000 downloads took place. However over the last couple of years, for various reasons, no work was done on this plugin and a new version of Live Writer was released. While the Tag Generator plugin still works, it does not support the options feature for plugins that came out with this version of Live Writer. In addition, as you all know, Windows Live Spaces no longer supports blogging, instead choosing to support the Word Press blogging resources. Generating these tags are obviously pointless.

You might wonder to yourself why I just don’t sit down and rewrite the plugin and be done with it. Basically it is because I am too busy at the moment to do so. I will at some point I promise. I apologize to those of you who have been frustrated. But this blog post today is to tell you how I still make use of this plugin and how  you can too.

First after you are done writing your post, right click on your document and click select all.

image

Then click “Insert” in the Live Writer toolbar and click “Generate Tags”.

image

Click the option or options you wish to generate the tags for and click the button “Generate”

image

Add, modify or delete the tags you wish…..but do not click the Insert button at the bottom! Instead select all of the words in the “Tags” text box and copy them. Then in the Set tags text box at the top of Live Writer, paste these words in their textbox.

image

Now your post will be tag ready for publication whenever you choose!

I am sorry for the hassle. I still find this plugin extremely useful for me and I promise I will get around to redoing it for you at some point. But this works fine for me for now and hopefully will for you as well.

Facebook

, , , , , , , , , , , , , , , , , , , , , , , , , , , ,

5 Comments

Write Microsoft Word Document Header and Footer with C#

Again I am rewriting some of the more popular entries in this blog as C#. It’s good exercise for me and good for you because you get the code another way. Well at least that’s the theory! This entry writes to the header and footer of a document in Microsoft Word The vb.net version was here. The only downside to all of this is that this is only tested against Office 2010. The old version I had Office 2000, XP and 2003 on my machine. Essentially however it is the same code. If you have any questions of course please feel free to leave a comment or email me.

public void WriteHeaderandFooterinWordDocument()
{
Word.Document oDoc = new Word.Document();
oDoc.Application.Visible = true;
oDoc.Content.Application.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageHeader;// = Convert.ToInt32(Word.WdSeekView.wdSeekCurrentPageHeader);
oDoc.Content.Application.Selection.TypeText(“Martens “);
oDoc.Content.Application.Selection.Fields.Add(oDoc.Content.Application.Selection.Range, Word.WdFieldType.wdFieldEmpty, “PAGE”);
oDoc.Content.Application.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument;
oDoc.Content.Application.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageFooter;
oDoc.Content.Application.Selection.TypeText(“Martens”);
oDoc.Content.Application.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument;
}

I’m on Facebook

, , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

3 Comments

Write Data From DataTable To Excel With an Array Using C#

This is actually a redo of code written in vb.net a while back that dumps data from a datatable in a dataset into an excel spreadsheet in an effective and fast manner. I am always getting asked how to do this in C# so I decided to put that up today. Yes, I do C# too! Just didn’t want to admit it….

It’s pretty self explanatory. Don’t forget to clean up your excel instance when done. If you have any questions please feel free to send me an email.

Import an excel reference and at the top of your code you need your import statements. Then simply pass your dataset and datatable (granted you could just pass the datatable – I had my reasons for passing the dataset too at the time) to the method. The array is declared as an object because sometimes the compiler has to deal with unforeseen data issues that might arise and it has been effective for me to do let it handle those situations as they arise. I hope you find this useful.

using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;

public void CreateSpreadSheetFromDataSet(DataSet ds, DataTable dt)
        {

            Excel.Application Excel = new Excel.Application();
            Excel.Visible = true;
            Excel.Worksheet WSheet = new Excel.Worksheet();
            Excel.Workbooks.Add();
            WSheet = Excel.ActiveWorkbook.ActiveSheet;
            int rows = ds.Tables[dt.TableName].Rows.Count;
            int columns = ds.Tables[dt.TableName].Columns.Count;
            int r = 0; int c = 0;
            object[,] DataArray = new object[rows + 1, columns + 1];
            for (c = 0; c <= columns – 1; c++)
            {
                DataArray[r, c] = ds.Tables[dt.TableName].Columns[c].ColumnName;
                for (r = 0; r <= rows – 1; r++)
                {
                    DataArray[r, c] = ds.Tables[dt.TableName].Rows[r][c];
                } //end row loop
            } //end column loop

//actually write array to Excel Spreadsheet
            WSheet.Range[“A2”].Resize[rows, columns].Value = DataArray;

            //write header row to spreadsheet
            int DataTableColumnCounter;
            int ExcelColumnCounter = 1; //excel spreadsheets start at 1 when counting columns not zero!
            for (DataTableColumnCounter = 0; DataTableColumnCounter <= ds.Tables[dt.TableName].Columns.Count – 1; DataTableColumnCounter++)
            {
                WSheet.Cells[1, ExcelColumnCounter].Value = ds.Tables[dt.TableName].Columns[DataTableColumnCounter].ColumnName;
                ExcelColumnCounter = ExcelColumnCounter + 1; //moving to next column
            }
        }

My Facebook Link

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

1 Comment

Create a Share on Remote Machine using WMI with vb.net

This code was used to create a share on a remote machine a while back for the purpose of automating a function that had previously required a network guy at the company to actually touch the machine which of course they didn’t like to do (it required work on their part). But it was ok. I didn’t mind as it wasn’t that hard. WMI makes it easy to do.

I have been involved with a series of technical discussions with some folks at various companies lately. My most embarrassing moment yesterday was when I stumbled on my words when asked about polymorphism. I actually used the words “parallel universe” in conjunction with this! Can you imagine? I got to think better on my feet. But its all good. Have a great day!

Public Sub CreateShare(ByVal strShareName As String, ByVal strPath As String, ByVal computername As String, ByVal shareinfo As String)
        Try
            Dim objSWbemServices As Object
            Dim objSWbemObject As Object
            Dim colSWbemObject As Object
            Dim intRet As Integer
            Dim blnExists As Boolean
            Dim objSWbem As Object

            objSWbemServices = GetObject(“winmgmts://” + computername + “/root/cimv2”)

            colSWbemObject = objSWbemServices.InstancesOf(“Win32_Share”)

            For Each objSWbem In colSWbemObject
                If (objSWbem.name = strShareName) Then
                    blnExists = True
                    Exit For
                Else
                    blnExists = False
                End If
            Next

            If (blnExists = False) Then
                objSWbemObject = objSWbemServices.Get(“Win32_Share”)
                intRet = objSWbemObject.Create(strPath, strShareName, 0, 25, shareinfo)
                System.Console.WriteLine(“Share has been created on : ” + computername)
                System.Console.WriteLine(“ShareName : ” + ShareName + ”   SharePath : ” + SharePath + ”   ShareInfo : ” + shareinfo)
            Else
                System.Console.WriteLine(“Share is already present on computer : ” + computername)
                System.Console.WriteLine(“ShareName : ” + ShareName + ”   SharePath : ” + SharePath + ”   ShareInfo : ” + shareinfo)
            End If
        Catch ex As Exception
            System.Console.Write(“Error occured while trying to create shares on remote pc : ” + computername + vbCrLf + “Check if you have the necessary rights and/or that the pc is turned on.” + vbCrLf + ex.ToString)
            System.Console.WriteLine(“ShareName : ” + ShareName + ”   SharePath : ” + SharePath + ”   ShareInfo : ” + shareinfo)
        End Try

    End Sub

My Facebook Page

, , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Leave a comment

Getting Images from Microsoft Office Document Imaging (MODI) with vb.net

Hello! It has been a long time since my last blog post and I am happy to be getting back to this. I have missed writing this blog more than you have missed me I guarantee it!

That being said, now that this blog has resumed there will be a few changes. The landscape has changed a bit since my last blog entry. While I will still focus primarily on vb.net code I will also be including what I am working on that is WPF and C# related as well as other .NET related topics. Why? Simply because as my skillset has expanded I wanted to share what I have learned and am learning with you as well. I hope you won’t mind!

Today’s topic revolves around getting images from Microsoft Office Document Imaging (MODI). As you recall I have done a lot of Office automation over the years and I was asked to do this for a friend. As I said, it is good to be back!

add a reference -> Com -> Microsoft Office Document Imaging

Private Sub ConvertMdi2Tif(ByVal ModiFilePath As String)
Dim miDoc As New MODI.Document
Dim myViewer As New MODI.MiDocView
Dim myImg As MODI.Image
Try
Dim Folder As New DirectoryInfo(ModiFilePath)
pbReportGenerate.Minimum = 0
pbReportGenerate.Maximum = Folder.GetFiles(“*.MDI”).Length
pbReportGenerate.Step = 1
pbReportGenerate.Value = pbReportGenerate.Minimum
For Each File As FileInfo In Folder.GetFiles(“*.MDI”)
miDoc.Create(File.FullName)
myViewer.Document = miDoc
For i As Long = 0 To miDoc.Images.Count – 1
Dim tempDoc As New MODI.Document
myImg = miDoc.Images(i)
tempDoc.Create()
tempDoc.Images.Add(myImg, Nothing)
tempDoc.SaveAs(TIFPath & “\” + GetFileNameWithoutExtension(File.FullName) & i & “.Tif”, MiFILE_FORMAT.miFILE_FORMAT_TIFF_LOSSLESS, MiCOMP_LEVEL.miCOMP_LEVEL_HIGH)
tempDoc.Close()
tempDoc = Nothing
Exit For
Next
pbReportGenerate.PerformStep()
Application.DoEvents()
Next
miDoc.Close()
miDoc = Nothing
myViewer.Document = Nothing
myViewer = Nothing
Catch ex As Exception
MsgBox(ex.Message)
End Try
GC.Collect()
End Sub

Private Sub ConvertTif2Bmp(ByVal TIFFilePath As String)
Dim Folder As New DirectoryInfo(TIFFilePath)
Dim index As Long = 0
pbReportGenerate.Minimum = 0
pbReportGenerate.Maximum = Folder.GetFiles(“*.TIF”).Length
pbReportGenerate.Step = 1
pbReportGenerate.Value = pbReportGenerate.Minimum
For Each File As FileInfo In Folder.GetFiles(“*.TIF”)
GC.WaitForPendingFinalizers()
Dim streamBinary As New FileStream(File.FullName, FileMode.Open)
Dim g As System.Drawing.Image = System.Drawing.Image.FromStream(streamBinary)
Dim imgOutput As New Bitmap(g, g.Width, g.Height)

Dim qualityEncoder As Encoder = Encoder.Quality
Dim ratio As EncoderParameter = New EncoderParameter(qualityEncoder, 40)
Dim codecParams As New EncoderParameters(1)
codecParams.Param(0) = ratio
‘bmp.Save(fileName, jpegCodecInfo, codecParams)
Dim encoder1 As ImageCodecInfo = GetEncoderInfo(“image/bmp”)
imgOutput.Save(BMPPath + “\” + GetFileNameWithoutExtension(File.FullName) + “.” & System.Drawing.Imaging.ImageFormat.Bmp.ToString, encoder1, codecParams)
streamBinary.Close()
streamBinary = Nothing
g.Dispose()
g = Nothing
imgOutput.Dispose()
imgOutput = Nothing
pbReportGenerate.PerformStep()
Application.DoEvents()
Next
GC.Collect()
End Sub


My Facebook

WordPress Tags: vb.net,Images,Microsoft,Office,Document,MODI,automation,friend,reference,MiDocView,Image,Folder,DirectoryInfo,Minimum,Maximum,GetFiles,Length,Step,Value,File,FileInfo,Create,FullName,Long,Count,SaveAs,TIFPath,GetFileNameWithoutExtension,MiFILE_FORMAT,MiCOMP_LEVEL,Close,Exit,PerformStep,Application,DoEvents,Catch,Exception,MsgBox,Message,Collect,TIFFilePath,index,WaitForPendingFinalizers,FileStream,FileMode,Open,System,FromStream,Bitmap,Width,Encoder,ratio,EncoderParameter,EncoderParameters,Param,Save,ImageCodecInfo,GetEncoderInfo,BMPPath,ImageFormat,Dispose,blog,miDoc,myViewer,myImg,pbReportGenerate,tempDoc,streamBinary,imgOutput,qualityEncoder,codecParams

, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

2 Comments