Solved

print form5 with specific margins

Posted on 2011-09-10
22
683 Views
Last Modified: 2012-05-12
Hello all

I have in a form5, on the left part of the form, multiple text box, labels etc. that i want to print on a full normal page.

but i think i have a problem with the margins.

I'm trying multiple thing but i'n having problem with that.

This is where i am for now:

Option Explicit On

Public Class Form5

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'PrintForm1.Print()
        'PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.FullWindow)
        PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.Scrollable)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
        PrintForm1.Print()
    End Sub

    Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        
    End Sub
End Class

Open in new window


You will see the zone that i need to print but look at the print preview, the margins are not good, i want to have a full page.

How can i fix this please?

Thanks again


print-zone.jpg
print-preview.jpg
0
Comment
Question by:Wilder1626
  • 10
  • 7
  • 4
  • +1
22 Comments
 
LVL 40
ID: 36517485
First, you do not need to react to the PrintDocument when printing with PrintForm. They are two different things. PrintForm is provided in a PowerPack to bring to VB.NET features that were available in VB6 but left out of the framework. Stuff in the PowerPack is specific to VB and does not interact with other classes in the framework.

You can set the margins of the PrintForm component with code similar to the following:
PrintForm1.PrinterSettings.DefaultPageSettings.Margins.Top = 100
PrintForm1.PrinterSettings.DefaultPageSettings.Margins.Left = 100
PrintForm1.PrinterSettings.DefaultPageSettings.Margins.Right = 100

Open in new window

The value is in 1/100 of an inch, so adjust as needed.

Belle journée dans notre coin aujourd'hui, n'est-ce pas?
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 36517542
why don't you use a real report engine like crystal reports?

>>Belle journée dans notre coin aujourd'hui, n'est-ce pas?

Le ciel est tout bleu chez nous! Belle journée d'automne.
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 36517580
Bonjour!!!! :)

Actually, i just want to be able to have 1 form, but with 2 editable fields.

But know, the form is not height enought for all the detail from the official form.

Is there a way to have something with a scroll and that when i print that form, it will feet a 8 1/2 X 11 sheet?
0
 
LVL 40
ID: 36517851
I do not understand the "1 form, but with 2 editable fields" while we see a form that seems to have 6 TextBoxes in it.

So I think that we should start from the start. We need to understand what you exactly want to do.

Do you want to print the form so that you end up with a paper form that the user will fill with pen and pencil?

Do you want to print the form as is so that when it has been filled on screen, you print the screen as it is?

Do you want to print a contract from what was written in the form?

Do you need something else.

These would require different approaches, and there might be many solutions for each of those.

0
 

Expert Comment

by:crafuse
ID: 36518172
Wilder - like james, not sure i understand what you want to do.

in order to print a Completed version of your form, but as a report, as suggested you could use Crystal, or as james suggested you could approach it a number of other ways. for example, you could use a custom formatted & filled rich text control.

myself, i prefer to use custom word templates wherein i use bookmarks that i subsequently replace with whatever data is at hand. this offers all the formatting and customization you would require under any circumstances.

let me know if i can help you with the rich text stuff or word option, and i can provide you with code.

crafuse
0
 

Assisted Solution

by:crafuse
crafuse earned 150 total points
ID: 36518312
Wilder, here's some simple code to generate a custom report in Word. I realize it's not all what you asked for, but it might be helpful to you somewhere down the line. It really amounts to a useful interoperability with Word, and I use it a lot...  ;-)

Create a windows Form app, put a textbox called txtClientName, and a button called Button1. Add a reference to Microsoft Word 11 object library.

Create folder C:\TestVB and in there create a Word template called RangeReplace.dot

In that template create a bookmark called ClientName. (simply type strClientName, highlite it, go to Insert, select Bookmark, type ClientName, then press Add). Save.

Copy and paste the following into your form code.

Const wordWindowStateMaximize = 1
    Const wordDeleteCellsEntireRow = 2
    Const wordUndefined = 9999999
    'end word constants

    Private docClient As Object
    Private wdApp As Object


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim strSourcePath As String
        Dim strTemplateName As String
        Dim strClientname As String

        strSourcePath = "C:\TestVB"
        strTemplateName = "RangeReplace"


        wdApp = CreateObject("Word.Application")
        Application.DoEvents()
        docClient = wdApp.Documents.Add(strSourcePath & "\" & strTemplateName & ".dot")


        'assign values to whatever variables you want, example:
        strClientname = Me.txtClientName.Text

        RangeReplace(docClient, "ClientName", strClientName)

        Application.DoEvents()

        dgFinish(True)

    End Sub

    Private Sub RangeReplace(ByRef doc As Object, ByVal strBookmark As String, ByVal strValue As String, Optional ByVal blnMakeBold As Boolean = False)

        Dim wordRange As Object
        Dim wordBookmark As Object
        wordBookmark = doc.Bookmarks(strBookmark)
        wordRange = wordBookmark.Range
        wordRange.Text = strValue
        If blnMakeBold Then wordRange.Bold = True
        wordRange.Bookmarks.Add(strBookmark)
        wordRange = Nothing
        wordBookmark = Nothing
        Application.DoEvents()

    End Sub

    Public Sub dgFinish(ByVal blnSuccessful As Boolean)

        On Error GoTo Err_Handler

        Dim intResponse As Integer
        Dim blnInCleanUp As Boolean

        If blnSuccessful Then
            If Not wdApp.Visible Then wdApp.Visible = True
            wdApp.Activate()
            wdApp.windowstate = wordWindowStateMaximize
            docClient.Activate()
            GoTo Clean_Up
        Else 'Unsuccessful
            GoTo Clean_Up
        End If

        GoTo Exit_Sub

Clean_Up:
        blnInCleanUp = True
        If wdApp.Visible = False Then
            docClient.Close()
            Application.DoEvents()
            wdApp.Quit()
            Application.DoEvents()
        End If

        'delete the temporary copies
        'Kill(strTempPath & "\" & strDocPOLetterheadTemp & ".*")
        Application.DoEvents()

        blnInCleanUp = False

Exit_Sub:
        docClient = Nothing
        wdApp = Nothing
        Application.Exit()
        Exit Sub

Err_Handler:
        If Err.Number = 91 Then 'object var not set
            Resume Next
        ElseIf Err.Number = 53 Then 'file not found (kill statement)
            Resume Next
        ElseIf Err.Number = 5356 Then 'the file is already open
            'display default message
        End If
        MessageBox.Show(Err.Number & " " & Err.Description, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        If blnInCleanUp Then
            Resume Next
        Else
            Resume Clean_Up
        End If


    End Sub
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 36518789
Hello all

Just to explain more what i need:

I have a full paper form that i want to transfer in my .net project.

What you have seen in my previous post was only the beginning if the construction.

So I'm doing a reproduction of the paper form in a .NET form.

But there is so many lines on the paper form that i don't have enough space in my .net form. it is not height enough.

So I'm trying to see if there is another way of building my form, and that it would fit on a sheet 8 1/2 per 11 when i print it.

I did try it also with using a panel with a scroll bar but still hard to do.

I like the idea of using Microsoft Work option that my .net project will transfer the information to that word document, already build.

So i guess we can also do the same with excel.


But question, can we do it also with a PDF document where i have text box fields and that my project will open that PDF document and enter the information in it?

Thanks again  for all your help
0
 

Expert Comment

by:crafuse
ID: 36518798

Haven't used any PDF plugins, but I'm sure it's possible.

Let me know if you'de like similar code for .NET to Excel, I have some cool stuff.

Sorry I can't help with yer specific problem.

crafuse
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 36518800
For these questions:

Do you want to print the form so that you end up with a paper form that the user will fill with pen and pencil?
Answer: Yes but there will also be able to add some information directly in the form since there will be textbox in it also.

Do you want to print the form as is so that when it has been filled on screen, you print the screen as it is?
Answer: Yes since i will have some textbox in it so that the user will put there information.

Do you want to print a contract from what was written in the form?
Answer: Yes
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 36518803
Oh, i would love to do it in excel.

I'm better in  excel to create the formso that the project would send the information to the excel spreadsheet.

Thank you so much.
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 36518811
I would say that in excel, i will send the project textbox.value in a range cell in excel?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 11

Author Comment

by:Wilder1626
ID: 36518819
Sorry i wanted to say that I would say that i'm better in excel. Will I send the project textbox.value in a range cell in excel?
0
 
LVL 40
ID: 36519134
Hi.

Printing scrollable forms is a pain, so the idea of using an alternative piece of software is probably the best solution.

As has been suggested, the usual way of doing so it through a report generator. Personnaly, I do not like them.

Word is usually easier to use with that type of things, because you have more latitude that you have in Excel. If the information to print comes from a database, Access would also be a good choice if your users have it already installed on their computer. Simply prepare a report in Access and print it from your application once you have send the information from your TextBoxes to the database.

But since Access is not always installed, and since as for anything you do of that type, you need to know the software well in order to program. So here comes a sample that will give you the big guidelines in Excel. If you have programmed in VBA Excel before, you already have a start, but there are some adjustments to do since there is no ActiveSheet or ActiveCell when you control Excel from code.

But before that, about the PDF. I suppose that you have seen those PDF forms. Many documents from the canadian government can be filled that way, with screen forms that are just like the original. This could be an option, but in order to create them, you need to use Acrobat Writer, the official application to create PDF documents. Some of the Acrobat Writer editions have a tool to create those reports. There are free or cheap tools that enable you to create a PDF file from anything that can be printed, but the only one I know that can create PDF forms is the one of Adobe. It's costly if you use it only once, but could be worth it if you regularly have a need for that kind of stuff.

Now for Excel, here is an adaptation of something that comes from my current project. I think it gives you the basics. After adding a reference to Excel in your project:
Imports Microsoft.Office.Interop

Module Module1

  Sub GoForExcel()

    Dim excelApp As Excel.Application
    Dim excelSheet As Excel.Worksheet

    Try

      excelApp = New Excel.Application
      excelSheet = DirectCast(excelApp.Workbooks.Open("Template.xlt").Worksheets(1), Excel.Worksheet)

    Catch ex As Runtime.InteropServices.COMException
      'React to an error
      If excelApp IsNot Nothing Then
        excelApp.Quit()
      End If
      Exit Sub

    Catch
      If excelApp IsNot Nothing Then
        excelApp.Quit()
      End If
      Throw
      Exit Sub
    End Try

    'You should trap all the errors after that, but for the sake of simplicity, lets forget
    'the error trapping for the remaining of the document. Just build the thing so that you are sure that the Excel instance
    'is closed no matter what happens. If you run it invisible, the user won't be able to close it, and you might end up
    'with 35 copies of Excel running in the background.

#If DEBUG Then
    'Very useful during debugging. In release, it slows down things a lot, so usually you wait until the sheet has been filled in before displaying it.
    excelApp.Visible = True
#End If

    'Here, you do the replacement for your named ranges (plages nommées)
    With excelSheet.Cells
      .Replace("Plage1", TextBox1.text)
      .Replace("Plage2", TextBox2.text)
      .Replace("Plage3", TextBox3.text)
      '..............
    End With

    'If you want the user to continue entering stuff in the sheet afterward
    excelApp.Visible = True

    'If you want to print the thing
    excelSheet.PrintOut()

    'If you want to save it in a file
    excelSheet.SaveAs("yourFile.xls")

    'In all of the above cases
    excelApp.Quit()

  End Sub

End Module

Open in new window

Lot of stuff that might be new for you in there. Just ask. I have to go out later in the day. Might be back only in the eevening.

0
 
LVL 11

Author Comment

by:Wilder1626
ID: 36519213
Hello JamesBurger

Thanks again for your explanation.

I'm learning so much these days.

I have  Adobe LiveCycle designer. My company have build many form with it before. And still do.

Would that be the same thing as Adobe writer?

0
 
LVL 40
ID: 36519237
You are learning these days, and if you go on with .NET, you will be learning every day until you die.

This thing is so huge and offers so many possibilities that you do not stop learning. That's one of the many reasons .NET is fun to use. Tu ne t'ennuies jamais.

As for LiveCycle designer, I do not know about it, but you could try it.

One thing I forgot however when talking about Adobe Writer before, and this could be true of LiveCycle too. I am not sure it would be easy to feed the form with data from your application.
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 36519250
Ok. So i wont  go for Adobe then.

I will try with excel and word also, see what will be my best solution.

In word, it's already done 3/4 of it.

But i like Excel also.

I will try the code you gave me and i will give you new.

Thanks again and have a good afternoon. It's so nice outside today. I will go for my 10k jogging now. That will inspire me. :)
0
 
LVL 40
ID: 36519309
Correction for Adobe.

I went to my company account at the government, and they fill in some of the fields for me on some of their forms. So there is probably a way to send the information to the PDF form. I just don't know how.
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 36519510
Hello again

I have now try to use the excel code.

I have created a form called:
 excelSheet = DirectCast(excelApp.Workbooks.Open("C:\Users\Jean-Marc\Desktop\Template.xlt").Worksheets(1), Excel.Worksheet)

Open in new window


Then, i have changed these also:
   'Here, you do the replacement for your named ranges (plages nommées)
        With excelSheet.Cells
            .Replace("A1", client.Text)
            .Replace("B2", telephone.Text)
            .Replace("C3", courriel.Text)

Open in new window


Now, when i click on the button, i have this error in excel when it says: "Excel cannot find any data to replace". no cells from selection have.....

Do you know what is causing this?

Full code:

 Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim excelApp As Excel.Application
        Dim excelSheet As Excel.Worksheet

        Try

            excelApp = New Excel.Application
            excelSheet = DirectCast(excelApp.Workbooks.Open("C:\Users\Jean-Marc\Desktop\Template.xlt").Worksheets(1), Excel.Worksheet)

        Catch ex As Runtime.InteropServices.COMException
            'React to an error
            If excelApp IsNot Nothing Then
                excelApp.Quit()
            End If
            Exit Sub

        Catch
            If excelApp IsNot Nothing Then
                excelApp.Quit()
            End If
            Throw
            Exit Sub
        End Try

        'You should trap all the errors after that, but for the sake of simplicity, lets forget
        'the error trapping for the remaining of the document. Just build the thing so that you are sure that the Excel instance
        'is closed no matter what happens. If you run it invisible, the user won't be able to close it, and you might end up
        'with 35 copies of Excel running in the background.

#If DEBUG Then
        'Very useful during debugging. In release, it slows down things a lot, so usually you wait until the sheet has been filled in before displaying it.
        excelApp.Visible = True
#End If

        'Here, you do the replacement for your named ranges (plages nommées)
        With excelSheet.Cells
            .Replace("A1", client.Text)
            .Replace("B2", telephone.Text)
            .Replace("C3", courriel.Text)
            '..............
        End With

        'If you want the user to continue entering stuff in the sheet afterward
        excelApp.Visible = True

        'If you want to print the thing
        ' excelSheet.PrintOut()

        'If you want to save it in a file
        ' excelSheet.SaveAs("yourFile.xls")

        'In all of the above cases
        'excelApp.Quit()


    End Sub

Open in new window

0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 350 total points
ID: 36519682
A1,B1 and C1 are not named range, they are cell references.

If you wanted to work directly in a standard cell, you would use this:
excelSheet.Cells(1,1).Value = client.Text

Open in new window

and so on for the other cells. But it is generally not a good idea anyway.

This would be OK if you were starting from a blank Excel sheet, but in your case that would not be a good idea. Since you have some formatting and some text that is the same in all the contracts, starting from a template is a better idea. This is what I did in my sample, and you seem to understand that part, since this is also what you appear to be doing. Just yell if you do not know how to work with templates :-).

When working with templates, there are 2 ways to put something into the sheet through programmation.

The way I did in my previous piece of code.
With excelSheet.Cells
   .Replace("Plage1", TextBox1.text)
   .Replace("Plage2", TextBox2.text)
   .Replace("Plage3", TextBox3.text)
   '..............
 End With

Open in new window

"Plage1", "Plage2" and "Plage3" would be words that are actually written as such in different cells in the template. In real world, I use words such as "<ProductNo>", <"ProductName>". The brackets help me to identify those as markers in the template. Look at the enclose file, you will see them at the top of the sheet.

The Replace command simply replace those words with that I want cells that contains those words. I use Replace because in some of the templates I use in that project, the same data may appear in many places in the sheet.

When the data is used in only one place, I would rather go for a named range (plage nommée). You create one by clicking in the cell in typing a name in the TextBox in the upper left corner of the sheet. When you have a named range on a cell, you can change its content with:
excelSheet.Range("NameOfTheRange").Value = client.Text

Open in new window

This method is preferred to any other methods. It is better than a cell reference such as (5,1) or E1, because if you ever make changes to the sheet, by adding or removing rows or columns, the name will still reference the same cell, no matter at what coordinate it ends up. You thus do not have to go back in your code each time you make changes to the template. As long as the cell with a named range is not deleted, your code will find it.

I hope it's clear.






Projet--b-nisterie.xlt
0
 
LVL 11

Author Closing Comment

by:Wilder1626
ID: 36519698
This is very clear and now it works.

Thanks you so much again, i will be able to go forward with the rest of the project.

I will also take the work one also. it's good also.
0
 

Expert Comment

by:crafuse
ID: 36520590
Wilder -

very nice excel stuff from James. I would have posted a little earlier, but I live on the other side of the world, so when you guys are awake I am asleep...

just to add a bit to the excel stuff, here is a small sample of code from an old project that grabs an indeterminate and undefined amount of data from a table and pushes it into a spreadsheet. it then does a little formatting, sets up a few specific settings (freeze panes, for instance), goes through a brief bit of code that saves the spreadsheet, and then finally displays the completed spreadsheet to the user. compared to James' example, this one simply deals with dynamically generated data...

best,

crafuse


endSQL = "select * from tbl_client;"

        Dim strConn As String = "Data Source=someSQLbox;Initial Catalog=db_MyBusiness;Integrated Security=SSPI"

        Dim da As New SqlClient.SqlDataAdapter
        Dim ds As New DataSet

        Dim cn As New SqlConnection(strConn)

        da.SelectCommand = New SqlClient.SqlCommand(endSQL, cn)
        da.SelectCommand.CommandTimeout = 0
        da.Fill(ds, "Qry")


        Dim iCountRows As Integer
        iCountRows = ds.Tables(0).Rows.Count
        If iCountRows = 0 Then
            MessageBox.Show("Do something here when no data is returned.", "No Data Returned", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Sub
        End If


        Dim icountrow2 As Integer
        icountrow2 = ds.Tables(0).Rows.Count

        '------------------------------------------------------
        'here we start our excel code

        Dim oXL As Object
        Dim oBook As Object
        Dim oSheet As Object
        Dim iRow As Integer
        Dim iCol As Integer

        oXL = CreateObject("Excel.Application")
        oBook = oXL.Workbooks.Add
        oSheet = oBook.Worksheets(1)


       
              With oSheet
                
                'enter column headers into XL
                
		For iCol = 0 To ds.Tables(0).Columns.Count - 1
                    .Cells(1, iCol + 1) = ds.Tables(0).Columns(iCol).ToString
                    .Cells(1, iCol + 1).interior.colorindex = 44
                    .Cells(1, iCol + 1).Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom).Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin
                    .Cells(1, iCol + 1).Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeLeft).Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin
                    .Cells(1, iCol + 1).Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeRight).Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin
                    .Cells(1, iCol + 1).Borders(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeTop).Weight = Microsoft.Office.Interop.Excel.XlBorderWeight.xlThin
                Next

                'enter data to XL

                Dim strColval As String
                For iRow = 0 To ds.Tables(0).Rows.Count - 1
                    For iCol = 0 To ds.Tables(0).Columns.Count - 1

                        strColval = Trim(ds.Tables(0).Rows(iRow).ItemArray(iCol).ToString)
                        .Cells(iRow + 2, iCol + 1).Value = strColval.TrimStart("-")
                    Next iCol
                Next iRow
            End With

            
            'format the cells
                With oSheet.Cells
                    oSheet.Columns("A:A").ColumnWidth = 25
                    .Font.Name = "Arial"
                    .Font.Size = 8
                    .Font.ColorIndex = 1
                    .HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft
                    .VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignTop
                    .WrapText = False
                End With


'do a bunch more formatting here depending on your needs


        'deal with freeze panes with column headers
        oXL.ActiveWindow.FreezePanes = False
        oSheet.Cells(2, 1).Select()
        oXL.ActiveWindow.FreezePanes = True
        oXL.DisplayAlerts = False

        'format printing to repeat column headers
        With oSheet.PageSetup
            .PrintTitleRows = "$1:$1"
        End With

        Dim columncount As Long
        columncount = oSheet.UsedRange.Columns.Count
        oSheet.Range(oXL.Cells(1, 1), oXL.Cells(1, columncount)).WrapText = True

                Dim varVersion As Integer

        'save the actual document\spreadsheet
        Try
            oXL.DisplayAlerts = False
            'change this to put in the string as part of the title

            strTitle = Me.txtReportTitle.Text & "_" & Format(Now, ("yyyyMMdd_HHmmss"))
            oXL.Workbooks(1).SaveAs("H:\ReportBuilder\" & strTitle & ".xls")
            
	    'stuff to print\save query
            Dim oFile As System.IO.File
            Dim oWrite As System.IO.StreamWriter
            oWrite = File.CreateText("H:\ReportQueries\" & strTitle & ".txt")
            oWrite.Write(endSQL)
            oWrite.Close()

        Catch
        End Try

        oXL.Workbooks(1).Activate()
        oXL.Visible = True

        'oXL.Workbooks.Close()
        'oXL.Quit()
        oXL = Nothing

Open in new window

0
 
LVL 40
ID: 36520637
Good extension from crafuse, but I would change a few things in his code.

First, add an Import for the namespace at the top of the file, before the declaration of the module or class:
Imports Microsoft.Office.Interop.Excel

Open in new window

This way, you won't have to type the namespace each time you want to reference an Excel constant, which would make the code a lot easier to write an read.

Then, something that we often see in sample code because a lot of the Excel code that you still find everywhere is actually VBScript code. Although VBScript is still in use, it uses a VB3 approach... while we are now on VB10.

Instead of using CreateObject to instantiate the variables, use New. For instance, I would replace
Dim oXL As Object
oXL = CreateObject("Excel.Application")

Open in new window

by
Dim oXL As Excel.Application
oXL = New Excel.Application

Open in new window

Do the same for most variables.

Working with Object variables is a pain. Not only do you lose a lot of performance (and you will need some if you do a lot of operations between .NET and Excel), not only is the compiler unable to check your code for mistakes, but you also do not have IntelliSense to help you with those lists of properties and methods that make working in VB so easy.

Working with specifically typed variables is almost always easier.

The only advantage of working with CreateObject compared to New is that it is not as dependant on the version number in you reference to Excel.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now