Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
186 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 2000 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

719 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