i open an existing xlsx file and add a new worksheet. Then i simply want to save the file.
Dim wb As Workbook
Dim ws As Worksheet
Set wb = Workbooks.Open(fileName:=pathToFile, Editable:=True, ReadOnly:=False)
Set ws = wb.Worksheets.Add()
ws.Name = "newSheet"
ws.range("A1").CopyFromRecordset rs
wb.Save
wb.Close
But wb.Save saves the file to $USERS\Documents although it is not the source directory from where i open the file.
Any ideas why vba is doing this?
I've also tried wb.SaveAs pathToFile but this caused an error. And wb.Close SaveChanges:=True is not working too.
I need to save this file to the same path and name.
Of course i could do wb.SaveAs pathToFile & "_tmp", delete the old and rename the new file. But why is Save not working?
EDIT
Dim strCon As String: strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=path\file.xlsx;Mode=Read;Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1"";"
Dim sql as String
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
cn.Open strCon
rs.Open Source:=sql, ActiveConnection:=cn
Dim wb As Workbook
Dim ws As Worksheet
Set wb = Workbooks.Open(fileName:=path\file.xlsx, ReadOnly:=False)
Set ws = wb.Worksheets.Add()
ws.Range("A1").CopyFromRecordset rs
rs.Close
cn.Close
wb.Save
wb.Close