Click here to Skip to main content
14,486,686 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi all

I have a that is to save all the listbox values to a specific path and file.
When I click the button the listbox values do get saved to specific file, but a duplicate file, with the same name, gets saved to a different path.
The code works 100%, it is just the duplication of the files.

FileP = ("C:\Users\Me\Documents\Stuff\")
        xStrDate = Format(Now, "yyyyMMdd_hhmmss")
        xlWorkBook = xlApp.Workbooks.Add(misValue)
        xlWorkSheet = xlWorkBook.Sheets("sheet1")
        xlWorkSheet.Cells(1, 1) = "Barcodes"
        xlWorkBook.SaveAs(FileP & xStrDate & ".xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue,
         Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue)
        xlWorkBook.Close(True, misValue, misValue)
        Label3.Text = FileP & xStrDate & ".xls"

What I have tried:

I have been struggling with this for 2 days now.
Updated 24-Mar-20 8:55am
Richard MacCutchan 24-Mar-20 10:24am
There is only one SaveAs in that code. So where else is the file getting saved?
Dave Kreskowiak 24-Mar-20 10:49am
I'm wondering what you did you get it to happen. The code you posted will not "save it twice".
ZurdoDev 24-Mar-20 12:43pm
Nope. Not happening from that.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

The duplicate file gets saved under Documents, and the one, as per code is under Stuff.

I have replaced the code with the following, and this works.

Dim so As New System.Text.StringBuilder
Dim i As Integer
For i = 0 To ListBox1.Items.Count - 1
ListBox1.SelectedIndex = i
so = so.Append(Chr(34) & ListBox1.SelectedItem.ToString() & Chr(34) & vbCrLf)
Dim sw As New StreamWriter(Label3.Text)
Dave Kreskowiak 24-Mar-20 14:59pm
First, this isn't an answer.

Second, this code you posted in the question will not save the sheet twice and in two different folders.

Lastly, the code you posts in this "answer" will not save the Excel workbook at all. It will just write a list of strings to a file you specified in a textbox.
ZurdoDev 24-Mar-20 15:02pm
LOL! But at least it looks like the OP is able to move on.

face palm.
Richard MacCutchan 5 days ago
Better still, the Question has been answered, so we can move on. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100