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
182 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 

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
 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual Studios 1 77
SSIS Feed MS SQL Server Stored procedure with loop 4 37
Finding Events logs for IIS website that restarts 2 14
abstract class C# 1 31
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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

830 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