Share via


Error : Reference to class 'ApplicationClass' is not allowed when its assembly is linked using No-PIA mode.

Question

Friday, June 8, 2012 7:02 AM

I got this Error : Reference to class 'ApplicationClass' is not allowed when its assembly is linked using No-PIA mode.

This Is My Code:

Imports System
Imports System.Collections.Generic
Imports System.Drawing
Imports System.Reflection
Imports System.Threading
Imports System.Windows.Forms
Imports FastColoredTextBoxNS
Imports Microsoft.Office.Interop.Word
Partial Public Class Form1
    Inherits Form
    Private wavyStyle As FastColoredTextBoxNS.Style = New WavyLineStyle(255, Color.Red)
    Public Sub New()
        InitializeComponent()
        Dim fctb As New FastColoredTextBox()
        fctb.Parent = Me
        fctb.Dock = DockStyle.Fill

        SpellChecker.Init()
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        SpellChecker.Dispose()
    End Sub

    Private Sub fctb_TextChangedDelayed(ByVal sender As Object, ByVal e As TextChangedEventArgs)
        ThreadPool.QueueUserWorkItem(Function(s)
                                         Dim list As Object = New List(Of FastColoredTextBoxNS.Range)()
                                         For Each word As Object In e.ChangedRange.GetRanges("[\w']+")
                                             If SpellChecker.GetSuggest(word.Text) IsNot Nothing Then
                                                 list.Add(word)
                                             End If
                                         Next
                                         e.ChangedRange.ClearStyle(wavyStyle)
                                         For Each word As Object In list
                                             word.SetStyle(wavyStyle)
                                         Next

                                     End Function)
    End Sub

    Friend WithEvents FastColoredTextBox1 As FastColoredTextBoxNS.FastColoredTextBox
End Class

Module SpellChecker
    Dim App As ApplicationClass
    Public Sub Init()
        Dim template As Object = Type.Missing
        Dim newtemplate As Object = False
        Dim doctype As Object = WdNewDocumentType.wdNewBlankDocument
        Dim visible As Object = False
        App.DisplayAlerts = WdAlertLevel.wdAlertsNone
        App.Visible = False
        App.Options.SuggestSpellingCorrections = True
        Dim document As Document = App.Documents.Add(template, newtemplate, doctype, visible)
        document.Activate()
    End Sub

    Public Function GetSuggest(ByVal word As String) As String()
        Dim [nothing] As Object = Missing.Value
        Dim spelledright As Boolean = App.CheckSpelling(word, [nothing], [nothing], [nothing], [nothing], [nothing], _
         [nothing], [nothing], [nothing], [nothing], [nothing], [nothing], _
         [nothing])
        If spelledright Then
            Return Nothing
        End If
        Dim suggestions As Object = App.GetSpellingSuggestions(word, [nothing], [nothing], [nothing], [nothing], [nothing], _
         [nothing], [nothing], [nothing], [nothing], [nothing], [nothing], _
         [nothing], [nothing])
        Dim words As Object = New List(Of String)()
        For Each suggestion As SpellingSuggestion In suggestions
            words.Add(suggestion.Name)
        Next
        suggestions = Nothing
        Return words.ToArray()
    End Function

    Public Sub Dispose()
        Dim savenochanges As Object = WdSaveOptions.wdDoNotSaveChanges
        Dim [nothing] As Object = Missing.Value
        If App IsNot Nothing Then
            Try
                App.Quit(savenochanges, [nothing], [nothing])
            Catch
            End Try
        End If
        App = Nothing
    End Sub

End Module

All replies (3)

Monday, June 11, 2012 3:02 AM ✅Answered

Hi AlSayed,

Thanks to your post.

It seems that this part of code caused this issue:

Dim App As ApplicationClass

Here is some information about this error, for more information, please check” Cannot Reference Office PIA Classes in Projects that Target the .NET Framework 4” part of Troubleshooting Design Time Errors in Office Solutions.

In projects that target the .NET Framework 4, code that references a class that is defined in an Office PIA will not compile by default. Classes in the PIAs use the naming convention objectnameClass, such as DocumentClass and WorkbookClass. 

To resolve this error, modify the code to reference the corresponding interface instead. For example, rather than reference aDocumentClass* object, reference an instance of the* Document* interface instead.*

As I read your code, it seems that the code is converted from the C# code. The syntax of the code using in Office is different between C# and VB. VB doesn’t need to add all of the parameters to the office method.

Hope this helps.

Mark Liu-lxf [MSFT]
MSDN Community Support | Feedback to us


Monday, June 11, 2012 3:13 AM ✅Answered

As I read your code, it seems that the code is converted from the C# code. The syntax of the code using in Office is different between C# and VB. VB doesn’t need to add all of the parameters to the office method.

In addition to that observation it would appear that the C# example used "var" types everywhere that got translated to type Object in VB.  When possible change the types of your variables to be the most explicit for the assignment.  For example try  changing this

Dim words As Object = New List(Of String)()

to this

Dim words As List(Of String) = New List(Of String)()

This would be true of the Word objects as well. If you are going to take the effort to import the interop library you may as well use the strongly typed classes it offers, right?  Exactly what those types are will have to be your job because I do not work with Word. Sorry.


Tuesday, June 19, 2012 8:43 AM

Hi AlSayed,

We haven’t heard from you for several days. I’d like to mark the helpful replies as answer firstly. If you have any additional questions, you also can unmark the replay and post your question here. 

Sorry for any inconvenience and have a nice day.

Mark Liu-lxf [MSFT]
MSDN Community Support | Feedback to us