为什么我会收到错误:System.Runtime.InteropServices.COMException?

时间:2022-08-22 23:14:06

I am using VB.NET to Create Labels in Microsoft. This is my code:

我正在使用VB.NET在Microsoft中创建标签。这是我的代码:

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 program 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.

System.Runtime.InteropServices.COMException - >水平和垂直间距必须分别大于或等于标签宽度和高度。

Even though I tried to set the Horizontal and vertical pitch programatically it gives same error.

即使我试图以编程方式设置水平和垂直间距,它也会产生相同的误差。

2 个解决方案

#1


Make sure that you have a default printer set up for the user account that the application runs under. Might not be relevant but I have had various unusual problems following that omission.

确保为应用程序运行的用户帐户设置了默认打印机。可能不相关,但在遗漏之后我遇到了各种不寻常的问题。

#2


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

您是否安装了Office主互操作程序集? (适当缩写为PIA)

Try this

#1


Make sure that you have a default printer set up for the user account that the application runs under. Might not be relevant but I have had various unusual problems following that omission.

确保为应用程序运行的用户帐户设置了默认打印机。可能不相关,但在遗漏之后我遇到了各种不寻常的问题。

#2


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

您是否安装了Office主互操作程序集? (适当缩写为PIA)

Try this