Results 1 to 4 of 4

Thread: Excel QueryInterface problem

  1. #1

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    Excel QueryInterface problem

    Guys,

    I am getting this error when trying to set DisplayAlerts in excel to false. This has been working fine for months, why would it suddenly stop working ? It still creates the excel document OK after this error.

    Thanks
    Bob

    Error: QueryInterface for interface Microsoft.Office.Interop.Excel._Application failed.
    Err No:13Stack Trace:
    at Microsoft.Office.Interop.Excel.ApplicationClass.set_DisplayAlerts(Boolean RHS)

    This is my code that gets Excel....

    VB Code:
    1. Dim xl As Excel.Application
    2.             Try
    3.  
    4.                 Try
    5.                     xl = GetObject(, "Excel.Application")
    6.                 Catch ex As Exception
    7.                     xl = New Excel.Application
    8.                 End Try
    9.  
    10.                 Dim wkbk As Excel.Workbook
    11.                 Dim wkst As Excel.Worksheet
    12.                 xl.DisplayAlerts = False 'FAILS HERE ####
    13.                 xl.Visible = False
    14.                 Dim docpath As String
    15.  
    16.                 wkbk = xl.Workbooks.Open(TemplateLocation)
    17.                 wkst = wkbk.Sheets(1)
    "I dislike 7 am. If 7 am were a person, I would punch 7 am in the biscuits." - Paul Ryan, DailyRamblings

  2. #2
    Addicted Member bgard68's Avatar
    Join Date
    Mar 2006
    Location
    Arkansas
    Posts
    164

    Re: Excel QueryInterface problem

    1. Make sure you have a reference to the Microsoft Office/Excel dll inside
    of your solution.

    2. Im not sure how your telling what excel document to get, but
    make sure its there.
    Using Framework 1.1, VB.Net 2003 unless I
    state otherwise

  3. #3
    Addicted Member bgard68's Avatar
    Join Date
    Mar 2006
    Location
    Arkansas
    Posts
    164

    Re: Excel QueryInterface problem

    Private _strFile As String = String.Empty

    Public Property strFile() As String
    Get
    Return _strFile
    End Get
    Set(ByVal Value As String)
    _strFile = Value
    End Set
    End Property

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
    'close/exit the main form
    Me.Close()
    End Sub

    Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click

    Try
    'user wants to open a excel file
    subOpenExcel()
    Catch ex As Exception
    'notify user if error
    MessageBox.Show(ex.Message.ToString.Trim)
    End Try

    End Sub

    Private Sub subOpenExcel()
    Try

    'Set the file name
    Me.subSetFileName()

    'Open the excel document
    If Me.strFile.ToString.Trim.Length > 2 Then
    subOpenFile()
    Else
    'select a file
    MessageBox.Show("You need to select an Excel file.")
    End If

    Catch ex As Exception
    'if error notify the user
    MessageBox.Show(ex.Message)
    End Try
    End Sub

    Private Sub subSetFileName()

    Try
    'filter which files to open
    Me.OpenFileDialog1.Filter = "Excel Docs (*.xls)|*.xls|All files|*.*"

    'if user selects a file then set the selected filename
    If Me.OpenFileDialog1.ShowDialog = DialogResult.OK Then
    Me.strFile = Me.OpenFileDialog1.FileName.ToString.Trim
    End If
    Catch ex As Exception
    'if error notify the user
    MessageBox.Show(ex.Message)
    End Try

    End Sub

    Private Sub subOpenFile()

    Try
    Dim oExcel As New Excel.Application
    Dim oWorkBook As Excel.Workbook
    Dim oWorkSheet As Excel.Worksheet

    oExcel.DisplayAlerts = True
    oExcel.Visible = True
    oWorkBook = oExcel.Workbooks.Open(Me.strFile)

    'oWorkSheet = oWorkBook.Worksheets("myWorkSheet")
    'oWorkSheet.Activate()

    Catch ex As Exception
    'if error notify the user
    MessageBox.Show(ex.Message)
    End Try

    End Sub
    Using Framework 1.1, VB.Net 2003 unless I
    state otherwise

  4. #4

    Thread Starter
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    Re: Excel QueryInterface problem

    Thanks Bgard,

    1 - I have got a reference set correctly, if it wasn't there, the code would fail on xl = GetObject(, "Excel.Application")
    2 - Yup, the document is there, always has been.

    Bob
    "I dislike 7 am. If 7 am were a person, I would punch 7 am in the biscuits." - Paul Ryan, DailyRamblings

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width