#vb.net
#vb.net
Вопрос:
Я выполняю проект по созданию документа Microsoft word. Мой код выглядит следующим образом. Я получаю исключение как «System. _ _ ComObject».Исключение InvalidCastException: не удается преобразовать COM-объект типа ‘System. _ _ ComObject’.__ComObject’ в класс типа ‘System.Строка’. Экземпляры типов, представляющих COM-компоненты, не могут быть преобразованы в типы, которые не представляют COM-компоненты; однако они могут быть преобразованы в интерфейсы, если базовый COM-компонент поддерживает вызовы QueryInterface для IID интерфейса. в примере отчета.Form1.GetNewResult(поле wField, документ wordDoc) в F:reportexamplereportexampleForm1.vb:line 114″
Private Function GetNewResult(wField As Word.Field, WordDoc As Word.Document) As String
Dim StopPos As Long
Dim Variable As String = ""
Dim UsedVariable As String
Dim VariableValue As String
Dim wRange As Word.Range
Try
Debug.Print(wField.Code)
' These three lines strip down the field code to find
' out it's name
StopPos = InStrRev(wField.Code, "*")
Variable = Microsoft.VisualBasic.Left(wField.Code, StopPos - 3)
Variable = Microsoft.VisualBasic.Right(Variable, Len(Variable) - 14)
Catch ex As Exception
TextBox1.Text = ex.ToString
End Try
' Check this field hasn't already appeared in this
' document.
If CheckUsedVariable(Variable) Then
VariableValue = GetVariableValue(Variable)
Else
Select Case UCase(Variable)
' I don't simply want to insert a string -
' I wish to insert a table at the Product Field.
Case "PRODUCT"
' Get the range (location) of the product field
wRange = wField.Code
' Delete the field, as any text will be inserted into the
' {} of the existing field.
wField.Delete()
' Enter our table information including headers.
' Ideally, I would get this data from an ADO recordset
' using GetString().
With wRange
.Text = "PRODUCT" amp; vbTab amp; "CTSBATCHNO" amp; vbTab amp; "SUPP REF" amp; vbTab amp; "PACKNO" amp; vbTab amp; "STORAGE" amp; vbTab amp; "QTY UNITS" amp; vbCrLf amp; _
"989797897" amp; vbTab amp; "hjhkhk" amp; vbTab amp; "kjhkjhkh" amp; vbTab amp; "kjhkjh" amp; vbTab amp; "Frozen" amp; vbTab amp; "3" amp; vbCrLf amp; _
"989797897" amp; vbTab amp; "hjhkhk" amp; vbTab amp; "kjhkjhkh" amp; vbTab amp; "kjhkjh" amp; vbTab amp; "Frozen" amp; vbTab amp; "3" amp; vbCrLf amp; _
"989797897" amp; vbTab amp; "hjhkhk" amp; vbTab amp; "kjhkjhkh" amp; vbTab amp; "kjhkjh" amp; vbTab amp; "Frozen" amp; vbTab amp; "3" amp; vbCrLf amp; _
"989797897" amp; vbTab amp; "hjhkhk" amp; vbTab amp; "kjhkjhkh" amp; vbTab amp; "kjhkjh" amp; vbTab amp; "Frozen" amp; vbTab amp; "3" amp; vbCrLf amp; _
"989797897" amp; vbTab amp; "hjhkhk" amp; vbTab amp; "kjhkjhkh" amp; vbTab amp; "kjhkjh" amp; vbTab amp; "Frozen" amp; vbTab amp; "3" amp; vbCrLf amp; _
"989797897" amp; vbTab amp; "hjhkhk" amp; vbTab amp; "kjhkjhkh" amp; vbTab amp; "kjhkjh" amp; vbTab amp; "Frozen" amp; vbTab amp; "3" amp; vbCrLf amp; _
"989797897" amp; vbTab amp; "hjhkhk" amp; vbTab amp; "kjhkjhkh" amp; vbTab amp; "kjhkjh" amp; vbTab amp; "Frozen" amp; vbTab amp; "3" amp; vbCrLf amp; _
"989797897" amp; vbTab amp; "hjhkhk" amp; vbTab amp; "kjhkjhkh" amp; vbTab amp; "kjhkjh" amp; vbTab amp; "Frozen" amp; vbTab amp; "3" amp; vbCrLf amp; _
"989797897" amp; vbTab amp; "hjhkhk" amp; vbTab amp; "kjhkjhkh" amp; vbTab amp; "kjhkjh" amp; vbTab amp; "Frozen" amp; vbTab amp; "3" amp; vbCrLf amp; _
"989797897" amp; vbTab amp; "hjhkhk" amp; vbTab amp; "kjhkjhkh" amp; vbTab amp; "kjhkjh" amp; vbTab amp; "Frozen" amp; vbTab amp; "3" amp; vbCrLf amp; _
"989797897" amp; vbTab amp; "hjhkhk" amp; vbTab amp; "kjhkjhkh" amp; vbTab amp; "kjhkjh" amp; vbTab amp; "Frozen" amp; vbTab amp; "3" amp; vbCrLf amp; _
"989797897" amp; vbTab amp; "hjhkhk" amp; vbTab amp; "kjhkjhkh" amp; vbTab amp; "kjhkjh" amp; vbTab amp; "Frozen" amp; vbTab amp; "3" amp; vbCrLf amp; _
"989797897" amp; vbTab amp; "hjhkhk" amp; vbTab amp; "kjhkjhkh" amp; vbTab amp; "kjhkjh" amp; vbTab amp; "Frozen" amp; vbTab amp; "3" amp; vbCrLf amp; _
"989797897" amp; vbTab amp; "hjhkhk" amp; vbTab amp; "kjhkjhkh" amp; vbTab amp; "kjhkjh" amp; vbTab amp; "Frozen" amp; vbTab amp; "3" amp; vbCrLf amp; _
"989797897" amp; vbTab amp; "hjhkhk" amp; vbTab amp; "kjhkjhkh" amp; vbTab amp; "kjhkjh" amp; vbTab amp; "Frozen" amp; vbTab amp; "3" amp; vbCrLf amp; _
"989797897" amp; vbTab amp; "hjhkhk" amp; vbTab amp; "kjhkjhkh" amp; vbTab amp; "kjhkjh" amp; vbTab amp; "Frozen" amp; vbTab amp; "3" amp; vbCrLf amp; _
"989797897" amp; vbTab amp; "hjhkhk" amp; vbTab amp; "kjhkjhkh" amp; vbTab amp; "kjhkjh" amp; vbTab amp; "Frozen" amp; vbTab amp; "3" amp; vbCrLf amp; _
"989797897" amp; vbTab amp; "hjhkhk" amp; vbTab amp; "kjhkjhkh" amp; vbTab amp; "kjhkjh" amp; vbTab amp; "Frozen" amp; vbTab amp; "3" amp; vbCrLf amp; _
"989797897" amp; vbTab amp; "hjhkhk" amp; vbTab amp; "kjhkjhkh" amp; vbTab amp; "kjhkjh" amp; vbTab amp; "Frozen" amp; vbTab amp; "3" amp; vbCrLf amp; _
"989797897" amp; vbTab amp; "hjhkhk" amp; vbTab amp; "kjhkjhkh" amp; vbTab amp; "kjhkjh" amp; vbTab amp; "Frozen" amp; vbTab amp; "3" amp; vbCrLf amp; _
"989797897" amp; vbTab amp; "hjhkhk" amp; vbTab amp; "kjhkjhkh" amp; vbTab amp; "kjhkjh" amp; vbTab amp; "Frozen" amp; vbTab amp; "3" amp; vbCrLf amp; _
"989797897" amp; vbTab amp; "hjhkhk" amp; vbTab amp; "kjhkjhkh" amp; vbTab amp; "kjhkjh" amp; vbTab amp; "Frozen" amp; vbTab amp; "3" amp; vbCrLf amp; _
"989797897" amp; vbTab amp; "hjhkhk" amp; vbTab amp; "kjhkjhkh" amp; vbTab amp; "kjhkjh" amp; vbTab amp; "Frozen" amp; vbTab amp; "3" amp; vbCrLf amp; _
"989797897" amp; vbTab amp; "hjhkhk" amp; vbTab amp; "kjhkjhkh" amp; vbTab amp; "kjhkjh" amp; vbTab amp; "Frozen" amp; vbTab amp; "3"
.FormattedText.Font.Name = "Arial"
.FormattedText.Font.Size = "8"
' Once the data is there, we can convert it to a table
' structure and format it to look pretty!
.ConvertToTable(vbTab, , , , WdTableFormat.wdTableFormatColorful2)
End With
' Send back blank string as field does not exist anymore
VariableValue = ""
Case Else
' Get the value of the field from the user
VariableValue = InputBox("Enter value for: " amp; Variable, "Value not recognised for Despatch Note!")
AddNewVariable(Variable, VariableValue)
End Select
End If
GetNewResult = VariableValue
End Function
Ответ №1:
Word.Field.Code
является Range
объектом, который не может быть преобразован в строку.
Попробуй wField.Code.Text