Link to home
Start Free TrialLog in
Avatar of Millkind
MillkindFlag for Afghanistan

asked on

Error while saving document using file dialog box "atttempted to read write protected memory" vb.net 2010 windows 7

I am trying to move a program from a win xp vb.net 2005 to win 7.  I am running the program as admin.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnreportssaveasexcel.Click
        Try
            Dim ExApp As New Application
            Dim sfd As New SaveFileDialog
            sfd.Filter = "MS Excel (*.xlsx)|*.xlsx"
            If sfd.ShowDialog = DialogResult.OK Then
                Try
                    With ExApp
                        .SheetsInNewWorkbook = 1
                        .Workbooks.Add()
                        .Sheets.Item("Sheet1").Name = reportfilterlb.SelectedItem
                        .Worksheets(1).Select()
                        For x As Integer = 1 To reportds.Tables(0).Columns.Count
                            .Cells(1, x).Value = reportds.Tables(0).Columns(x - 1).ColumnName()
                        Next
                        For r As Integer = 1 To reportds.Tables(0).Select("Convert([" & reportfilterlb.SelectedItem & "], System.String) LIKE '" & reporttn.Text & "*'").Length
                            For c As Integer = 1 To reportview.Table.Columns.Count
                                .Cells(r + 1, c).Value = "'" & reportgrid.Item(r - 1, c - 1)
                            Next
                        Next
                        .Rows(1).Font.Bold = True
                        .Range("A:zz").EntireColumn.AutoFit()
                        .Range("A1", "Z100").HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter
                        .ActiveWorkbook().SaveAs(sfd.FileName)
                        .ActiveWorkbook.Close()
                        ExApp.Quit()
                        ExApp = Nothing
                        GC.Collect()
                    End With
                Catch ex As Exception
                    mainform.errorwrite(ex.ToString & " Reports to Excel")
                Finally
                    Dim p() As Process = Process.GetProcessesByName("EXCEL")
                    Dim u As Integer
                    For u = 0 To p.Length - 1
                        p(u).Kill()
                    Next
                End Try
            Else
            End If
            mainform.runcommand("insert into actionszebra (actionzebra,userzebra,datezebra) values ('To Excel','" & mainform.curruser & "','" & Date.Now & "')")
        Catch ex As Exception
            mainform.errorwrite(ex.ToString & " To Excel")
        End Try
    End Sub

Open in new window

error.png
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

You have selected a directory that is not accessible to a standard user.

When you say that you are running as an administrator, is it simply that you logged into the computer as an administrator, or did you really set the Run as Administrator switch when you started Visual Studio?
Avatar of Millkind

ASKER

Both
Does it happen on any directory or only one? If so, could we get its fullname?

Did you ran an anti-virus check recently? Look at the message that points out to the fact that this is often caused by corrupted memory. Corrupted memory is not something we usually see in .NET, but the FileSaveDialog has to get out of managed memory to do its job.
THe dialog box never even opens so i can even try to select a directory.  There is a corporate anti virus that runs on the computer.  Also the computer is only a few weeks old.  
Strange one.

What is the directory defined in the InitialDirectory property of the FileSaveDialog?

Can you use the dialog with another directory or with no difectory specified?

Can you use the dialog in another application?
when i insert this

MsgBox(sfd.InitialDirectory.ToString)

I get an empty message box.

I tried to change it to c: with same error.

Yes it does work with antoher app i wrote.  I actually copied most of it from the working one.

Strange, Strange one.

Did a good search. As is too often the case, 200 different causes, 300 different solutions.

What I found in a few places however, is people having the same problem when they try to automate the application with Excel 2010. Is this you case our are you on 2007?

A possible solution then would be to wait after getting the answer to the dialog before starting Excel. That would be a better practice anyway, because the way you are handling it now, you start Excel (New Application actually launches Excel in the background) even if the user cancels the dialog.

So your code would look something like this:

Dim exApp As Application  'No New
Dim sfg As New SaveFileDialog
std.Filter = "blablabla"
If sdf.ShowDialog = DialogResult.OK Then
  exApp = New Application 'Call the constructor here

This way, if this is indeed the problem, Excel would not be there at the point where you show the dialog.



Avatar of Nasir Razzaq
Does it work if you move the


            Dim ExApp As New Application

to the inside of if condition?
First i seperated the file dialog and the excel code completely and used a boolean to make sure the user selected a path.

That didnt work.  Same error

Then i got rid of the the save fiel dialog completly and just used save.

That worked saved a book1 into my documents.

Now im trying to gegt it to save to the current users desktop to avoid the selector entirely so i can move on with the project.

Problem is that i have other parts of this project that will require a similar function of the user selecting a path.  

I want to keep this question open in hopes that an answer to the original question presents itself.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnreportssaveasexcel.Click
        Try
            ''Dim goodpath As Boolean = False
            ''Dim sfd As New SaveFileDialog
            ''sfd.Filter = "Excel Worksheet  (*.xlsx)|*.xlsx"
            ''If sfd.ShowDialog = DialogResult.OK Then
            ''    goodpath = True
            ''End If
            ''If goodpath Then
            Try
                Dim ExApp As New Application
                With ExApp
                    .SheetsInNewWorkbook = 1
                    .Workbooks.Add()
                    .Sheets.Item("Sheet1").Name = reportfilterlb.SelectedItem
                    .Worksheets(1).Select()
                    For x As Integer = 1 To reportds.Tables(0).Columns.Count
                        .Cells(1, x).Value = reportds.Tables(0).Columns(x - 1).ColumnName()
                    Next
                    For r As Integer = 1 To reportds.Tables(0).Select("Convert([" & reportfilterlb.SelectedItem & "], System.String) LIKE '" & reporttn.Text & "*'").Length
                        For c As Integer = 1 To reportview.Table.Columns.Count
                            .Cells(r + 1, c).Value = "'" & reportgrid.Item(r - 1, c - 1)
                        Next
                    Next
                    .Rows(1).Font.Bold = True
                    .Range("A:zz").EntireColumn.AutoFit()
                    .Range("A1", "Z100").HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignCenter
                    .ActiveWorkbook().SaveAs(Environment.UserName)
                    ''.ActiveWorkbook().Save()
                    .ActiveWorkbook.Close()
                    ExApp.Quit()
                    ExApp = Nothing
                    GC.Collect()
                End With
            Catch ex As Exception
                mainform.errorwrite(ex.ToString & " Reports to Excel")
            Finally
                Dim p() As Process = Process.GetProcessesByName("EXCEL")
                Dim u As Integer
                For u = 0 To p.Length - 1
                    p(u).Kill()
                Next
            End Try
            ''End If
            mainform.runcommand("insert into actionszebra (actionzebra,userzebra,datezebra) values ('To Excel','" & mainform.curruser & "','" & Date.Now & "')")
        Catch ex As Exception
            mainform.errorwrite(ex.ToString & " To Excel")
        End Try
    End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The last comment from CodeCruiser makes sense.

As I told you, I strangely got the same error late yesterday with the OpenFileDialog. I have been able to pinpoint it to the fact that one of the file in the directory I was trying to look into was locked by an application that did not close properly and was stuck in the Task Manager.

The dialog probably tried to retrieve info in the file and got the AccessDenied becaus of the lock.