views:

411

answers:

2

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

A: 

hello,

i too have the same issue. i tried in several forums. any help on this will be useful. i get the same exception while calling

oWordApplication.Documents.Open(ref fileName, ref missing, ref readOnly,
                ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing);

/cethie

cethie
A: 

Did you install the Office Primary Interop Assemblies? (Appropriately abbreviated to PIA)

Try this... http://msdn.microsoft.com/en-us/library/kh3965hw%28VS.80%29.aspx

proudgeekdad