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
178 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 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

22 Experts available now in Live!

Get 1:1 Help Now