Visual Basic: Save in same folder as Excel Sheet - vba

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.

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.

Related

Microsoft Word Save As Macro Returning Corrupt File

I have a word document which I am using as a template for other projects. The file is saved as an .docx and includes a Macro to save the document with a file name dependent on content control box titled "STATE". I would prefer the macro save the document without the macro included. When I run the macro, the document saves to the correct file location however when opened I get an error that reads:
We're Sorry. We can't open FILE LOCATION because we found a problem with its contents. No error details available.
I assume there is an error somewhere in my code. I am using Word 2013. Any help would be appreciated!
Sub Silent_save_to_DOC()
Dim strText As String, strDate As String, strDrop As String
strText = ActiveDocument.SelectContentControlsByTitle("STATE")(1).Range.Text
Dim strFilename As String
strFilename = "C:\Users\Tom\Desktop\Quote - " & strText & ".docx"
ThisDocument.SaveAs strFilename
End Sub
The problem is that a *.docx file may not contain macros, only *.docm files may contain macros. Your code is forcing Word to save the document with macros and an incompatible file extension. As a security measure Word will not open a *.docx file that contains macros - as the message is informing you.
Use a true template file - *.dotm - to create your documents. The template can contain the macros, which will not be copied to the document.
If you want to unlink the template (with the macros) from the document you can attach the Normal template to the document (ActiveDocument.AttachedTemplate = NormalTemplate).
When you use SaveAs be sure to also specify the file format using the FileFormat parameter, not just the file name. For a docx file that would be ^wdFormatXMLDocument`.
Do not use ThisDocument.SaveAs as ThisDocument refers to the document containing the macro. Instead, use ActiveDocument if you cannot otherwise use a specific Document object.
It's also helpful if you specify the file format should there be any possibility that the save format isn't the same as that of the file that was opened. Hence:
Dim strText As String, strFilename As String
With ActiveDocument
strText = .SelectContentControlsByTitle("STATE")(1).Range.Text
strFilename = "C:\Users\Tom\Desktop\Quote - " & strText & ".docx"
.SaveAs2 FileName:=strFilename, FileFormat:=wdFormatXMLDocument
End With

Why is VBA creating a folder on ActiveWorkbook.SaveAs

I have this VBA:
ActiveWorkbook.SaveAs Filename:=Path & Filename & ".xls"
ActiveWorkbook.Close
This is supposed to save the currently active workbook or worksheet to a path and file name provided. The extension ".xls" is used so that the exported file is a Excel 97-2003 Workbook.
It saves the Workbook or Worksheet but has a folder included in them. Let's say the file name was "Master.xls", ActiveWorkbook.SaveAs works but the VBA seem to include a folder named "Master_files" next to it as if it were saved as a web file. How can I disable this?
Note that the sheet came from an online database, when I click the link to export the records into an Excel sheet it doesn't download it but instead opens it straight from web to my Excel application which is why I created this SaveAs code.
I believe you need to expressly specify the format:
ActiveWorkbook.SaveAs Filename:=Path & Filename & ".xls", FileFormat:=xlExcel8
Otherwise, it is saving as HTML, along with the _files, because you got it from the Web (I think). MSDN says:
For an existing file, the default format is the last file format specified
which I am guessing is HTML for your situation.
xlExcel8 is the constant to use for .xls files per Ron de Bruin's reference.

Save Excel table in current folder

I want to save my current table as a text file in the same folder as the current workbook (that is open).
I use this code:
ActiveWorkbook.SaveAs ThisWorkbook.path & "\" & filename
For some reason it save the text file in the same folder as my personal.xlsb.
I use Office 2010
If this is not possible to do easily then maybe one can force Excel to open a browse window where I can pick where I want the file saved.
ThisWorkbook points to the location where the code is written. In your case personal.xlsb.
If you want to save the table in the same directory as the active workbook, use ActiveWorkbook.path instead.
You may try this
ChDir "C:\yourfolder"
ActiveWorkbook.SaveAs Filename:= _
"c:\yourfolder" & filename & ".txt", FileFormat:=xlText, _
In this example I have created a folder name "yourfolder" in my C drive. The created text file will save in C\yourfolder\filenane.txt
Hope this is helpful

Excel VBA code is causing my worksheets to appear as jargon

I currently am using a VBA macro that I found here on stack over flow. The problem is when I run it it saves all the data into a separate excel sheet but when i open it it appears as "jargon" in other words unreadable type. This is the "Save code"
'Save the new workbook, and close it
wb.SaveAs ThisWorkbook.Path & "\test" & WorkbookCounter
wb.Close
The way I am currently running the code is that it separates my excel sheets into different spread sheets by rows of 250. Everything works but when I open the saved documents it says that this file format is unacceptable to Excel. Then I try importing it and I get an error. Here is a snap shot of the way it appears in my screen. Also here is the file name: built.list\test.xls39
Your workbook counter always ends in a number, Windows and Excel use the file extension to determine file-type, so an '.xls39' file is unrecognisable. Try:
wb.SaveAs _
Filename:=ThisWorkbook.Path & "\test" & WorkbookCounter & ".xls" _
FileFormat:=XlFileFormat.xlExcel8
'Use xlOpenXMLWorkbook for the .xlsx format
(Use space followed by underscore to separate lines in VBA)
Or make sure WorkbookCounter ends in .xls and not a number.
(Edit: For other formats, please look in the References dialog in Excel VBA Editor)

Saving an Excel sheet in a current directory with VBA

I have created a sheet in vba Excel. I would like to save it the current directory, but not in absolute path, then, when this is executed somewhere else, there won't be problem.
Can somebody help ?
I am not clear exactly what your situation requires but the following may get you started. The key here is using ThisWorkbook.Path to get a relative file path:
Sub SaveToRelativePath()
Dim relativePath As String
relativePath = ThisWorkbook.Path & Application.PathSeparator & ActiveWorkbook.Name
ActiveWorkbook.SaveAs Filename:=relativePath
End Sub
VBA has a CurDir keyword that will return the "current directory" as stored in Excel. I'm not sure all the things that affect the current directory, but definitely opening or saving a workbook will change it.
MyWorkbook.SaveAs CurDir & Application.PathSeparator & "MySavedWorkbook.xls"
This assumes that the sheet you want to save has never been saved and you want to define the file name in code.
If the Path is omitted the file will be saved automaticaly in the current directory.
Try something like this:
ActiveWorkbook.SaveAs "Filename.xslx"
Taking this one step further, to save a file to a relative directory, you can use the replace function. Say you have your workbook saved in: c:\property\california\sacramento\workbook.xlsx, use this to move the property to berkley:
workBookPath = Replace(ActiveWorkBook.path, "sacramento", "berkley")
myWorkbook.SaveAs(workBookPath & "\" & "newFileName.xlsx"
Only works if your file structure contains one instance of the text used to replace. YMMV.