Getting Error System.Runtime.InteropServices.COMException

Posted by Savan Parmar on Stack Overflow See other posts from Stack Overflow or by Savan Parmar
Published on 2009-01-21T09:51:11Z Indexed on 2010/03/16 0:19 UTC
Read the original article Hit count: 711

Filed under:
|

Hey All,

I am using Vb.Net to Create Labels in Microsoft. For that i am using below mantioend Code.

Public Sub CreateLabel(ByVal StrFilter As String, ByVal Path As String)
    WordApp = CreateObject("Word.Application")
    ''Add a new document.
    WordDoc = WordApp.Documents.Add()
    Dim oConn As SqlConnection = New SqlConnection(connSTR)
    oConn.Open()

    Dim oCmd As SqlCommand
    Dim oDR As SqlDataReader
    oCmd = New SqlCommand(StrFilter, oConn)
    oDR = oCmd.ExecuteReader

    Dim intI As Integer
    Dim FilePath As String = ""
    With WordDoc.MailMerge
        With .Fields
            Do While oDR.Read
                For intI = 0 To oDR.FieldCount - 1
                    .Add(WordApp.Selection.Range, oDR.Item(intI))
                Next
            Loop
        End With
        Dim objAutoText As Word.AutoTextEntry = WordApp.NormalTemplate.AutoTextEntries.Add("MyLabelLayout", WordDoc.Content)
        WordDoc.Content.Delete()
        .MainDocumentType = Word.WdMailMergeMainDocType.wdMailingLabels
        FilePath = CreateSource(StrFilter)
        .OpenDataSource(FilePath)
        Dim NewLabel As Word.CustomLabel = WordApp.MailingLabel.CustomLabels.Add("MyLabel", False)
        WordApp.MailingLabel.CreateNewDocument(Name:="MyLabel", Address:="", AutoText:="MyLabelLayout")
        objAutoText.Delete()

        .Destination = Word.WdMailMergeDestination.wdSendToNewDocument
        WordApp.Visible = True
        .Execute()
    End With
    oConn.Close()
    WordDoc.Close()
End Sub


Private Function CreateSource(ByVal StrFilter As String) As String
    Dim CnnUser As SqlConnection = New SqlConnection(connSTR)
    Dim sw As StreamWriter = File.CreateText("C:\Mail.Txt")
    Dim Path As String = "C:\Mail.Txt"
    Dim StrHeader As String = ""
    Try
        Dim SelectCMD As SqlCommand = New SqlCommand(StrFilter, CnnUser)
        Dim oDR As SqlDataReader
        Dim IntI As Integer
        SelectCMD.CommandType = CommandType.Text
        CnnUser.Open()
        oDR = SelectCMD.ExecuteReader
        For IntI = 0 To oDR.FieldCount - 1
            StrHeader &= oDR.GetName(IntI) & " ,"
        Next
        StrHeader = Mid(StrHeader, 1, Len(StrHeader) - 2)
        sw.WriteLine(StrHeader)
        sw.Flush()
        sw.Close()
        StrHeader = ""
        Do While oDR.Read
            For IntJ As Integer = 0 To oDR.FieldCount - 1
                StrHeader &= oDR.GetString(IntJ) & " ,"
            Next
        Loop
        StrHeader = Mid(StrHeader, 1, Len(StrHeader) - 2)
        sw = File.AppendText(Path)
        sw.WriteLine(StrHeader)
        CnnUser.Close()
        sw.Flush()
        sw.Close()
    Catch ex As Exception
        MessageBox.Show(ex.Message, "TempID", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
    Return Path
End Function

Now when i am running the programm i am getting this error.I tried hard but not able to locate what could be the problem the error is:- System.Runtime.InteropServices.COMException -->Horizontal and vertical pitch must be greater than or equal to the label width and height, respectively.

Even though i tried to set the Horizontal and vertical pitch programatically but it gives same err.

Plz if any one can help

© Stack Overflow or respective owner

Related posts about vb.net

Related posts about office-interop