Solved

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

Posted on 2011-09-26
11
179 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:Millkind
  • 5
  • 4
  • 2
11 Comments
 
LVL 40
ID: 36599841
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?
0
 

Author Comment

by:Millkind
ID: 36599865
Both
0
 
LVL 40
ID: 36599998
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.
0
 

Author Comment

by:Millkind
ID: 36600081
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.  
0
 
LVL 40
ID: 36600320
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?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:Millkind
ID: 36600421
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.

0
 
LVL 40
ID: 36601445
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.



0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36602067
Does it work if you move the


            Dim ExApp As New Application

to the inside of if condition?
0
 

Author Comment

by:Millkind
ID: 36709586
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

0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 36712049
So the conclusion is that the error comes up whenever you try to show the Save Dialog. I suspect, as mentioned before that Save Dialog is trying to enumerate locations and encountering an error in that process. Try playing with different properties of the dialog, such as Initial Directory, to see if it works.
0
 
LVL 40
ID: 36712531
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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now