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

时间:2009-01-21 09:51:11

标签: vb.net office-interop

我正在使用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

现在,当我运行程序时,我收到此错误。我努力但却无法找到错误可能出现的问题:

  

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

即使我尝试以编程方式设置水平和垂直间距,也会出现相同的错误。

2 个答案:

答案 0 :(得分:0)

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

尝试this

答案 1 :(得分:0)

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