Dan Rayner
Dan Rayner

Reputation: 3

Visual Basic: Save in same folder as Excel Sheet

I'm very new to VB, so this is probably a very easy one. I'm creating a Word document from an Excel spreadsheet, and would like the Word doc to save in the same folder location as the spreadsheet.

I'm using the code: .SaveAs Filename:=ThisWorkbook.Path & Range("C8").Text & ".docx" Which I though would work, but it saves it in teh directory up from the location.

I.e. the spreadsheet is in C:/User/Documents/MySpreadsheet. But the Word doc would be saved in C:/User/Documents.

I also made a message popup to display ThisWorkbook.Path which comes up with the Spreadsheet path, so I know that's right!

I also don't think I've done the naming right, as I would like it to be named the text in cell C8. But it's actually the 'Documents' folder name with the text in C8 added on.

Thanks in advance.

Upvotes: 0

Views: 998

Answers (1)

MarcinSzaleniec
MarcinSzaleniec

Reputation: 2256

Activate Immediate Window (Ctrl+G) and and add this line to your code:

debug.print ThisWorkbook.Path & Range("C8").Text & ".docx"

You will see if your path is correct. In particular, if you have "\" between folder path and filename.

Upvotes: 2

Related Questions