We help IT Professionals succeed at work.

Help with accessing excel file

247 Views
Last Modified: 2016-03-20
Hello,

When attempting to access an excel file I receive the following error evnthough it is not being used by another application:

The process cannot access the file 'parh\Debug\DataA.xls' because it is being used by another process.

on line:
  IO.File.Copy(TextBox2.Text, destinationPathWithSourceName, True)

How do I avoid this error?

 If IO.File.Exists(destinationPathWithSourceName) Then
            If MessageBox.Show("Destination already exists!" & vbCrLf & vbCrLf & "File: " & destinationPathWithSourceName, "Replace File?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                IO.File.Copy(TextBox2.Text, destinationPathWithSourceName, True)
                If File.Exists(destinationPathWithDestName) Then    'Delete Data.xls if it already exists
                    My.Computer.FileSystem.DeleteFile(destinationPathWithDestName)
                End If
                My.Computer.FileSystem.RenameFile(destinationPathWithSourceName, "Data.xls")
                ' namesheet()
                makeDataTableFromSheetNameUpload()
                loadgrid()
            End If
        Else
            IO.File.Copy(TextBox2.Text, destinationPathWithSourceName)
            If File.Exists(destinationPathWithDestName) Then    'Delete Data.xls if it already exists
                My.Computer.FileSystem.DeleteFile(destinationPathWithDestName)
            End If
            My.Computer.FileSystem.RenameFile(destinationPathWithSourceName, "Data.xls")
            'namesheet()
            makeDataTableFromSheetNameUpload()
            loadgrid()
            Label3.Visible = True
            ComboBox2.Visible = True
        End If

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Hi,

It's certainly not the code because the error doesn't occur when  publish the application. The file doesn't show on the task, must be something going on with my latop.

Thanks,

V.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.