2009-01-21 121 views
2

我正在使用VB.NET在Microsoft中创建标签。这是我的代码:为什么我得到错误:System.Runtime.InteropServices.COMException?

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 -->Horizontal and vertical pitch must be greater than or equal to the label width and height, respectively.

即使我试图以编程设定的水平和垂直间距它给了同样的错误。

+0

你能告诉我们哪一行导致异常吗? – Spence 2009-01-21 09:53:01

回答

0

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

尝试this

0

请确保您有设置为应用程序下运行的用户帐户默认打印机。可能没有关系,但在遗漏之后我有各种不寻常的问题。