VBA not working in 2016 as it did in 2013: suggested file name (InitialFileName) not showing up - vba

I have a code to export an Excel tab and save it as a new file with a preset file name. The user has the option to review the file name before saving. This is what I have been using:
InitialName = SaveString & UniqueString
fileSaveName = Application.GetSaveAsFilename(InitialFileName:=InitialName)
If fileSaveName <> False Then
Export.SaveAs (fileSavename)
End If
SaveString is the save folder, and UniqueString is the specific file name, which changes each month. The user clicks Export, the tab is prepared, and the Save As folder pops up in the correct folder with the suggested file name. As long as the user hits "Save," then the exported tab is saved in the SaveString folder with the UniqueString name (with .xlsx already included in UniqueString).
Since upgrading to Office 2016, the UniqueString suggested file name no longer shows up. The Save As pop-up still opens in the SaveString folder, but there is no suggested file name. If the user isn't careful to manually add .xlsx to the end of the file name, then the file type is an unusable "File."
I've opened Excel 2013 in a virtual setting and run the code side-by-side, and it works perfectly in the older version. Does anyone have insight as to why this change happened, and how to correct it?

It appears that you now need to include a file filter that matches the initial name you provide, so the following will possibly work:
fileSaveName = Application.GetSaveAsFilename(InitialFileName:=InitialName, _
FileFilter:="Excel Files (*.xlsx),*.xlsx")

Related

MS Project can't change filname bevor save

I have the following question:
It seems that I cannot change the name of a project file before it is saved. The following code does not work. Does anyone have a better idea? I am using Project 2016
DateToday = Format(Now, "YYYY-MM-DD")
Set Test = Application.Projects.Add(False, PTP_Template, False)
Test.Name = DateToday & " SomeMoreTest"
Test.SaveAs
I would like to change the file name so that users already have the correct file name in the file save dialog.
Thanks for any suggestion
It seems that I cannot change the name of a project file before it is
saved.
That is correct. The Project Name property is read-only just as it is with Excel, Word, etc.
The way around this would be to create a macro to save the file for the user. So instead of the user going to File: Save As, they would need to run the macro.

Word VBA Documents.Open deleting files in folder when falsely passing that folder's path instead of file path

I encountered the following issue:
When accidentally passing a folder path to the Documents.Open function in VBA Word I get the runtime error 5174 as one would expect.
However all files with names that begin with an underscore get deleted in that moment from that folder.
To replicate:
Assume folder C:/Test/
In said folder have two files:
test.txt
_test.txt
In Word VBA execute the command:
Documents.Open("C:/Test/")
(As part of a subroutine or in the immediate window.)
Result: Runtime Error 5174 and _test.txt is now missing.
Note: Passing a nonexisting file like "C:/Test/abc.txt" or a wrong folder path like "C:/Test" (without the last slash) will not have this effect and won't cause the runtime error.
I have only tested this on one system on a network drive under windows 10 with Microsoft Professional Plus 2019. It's entirely possible that it's an issue with the file system. Does anyone have any suggestions as to why is this happening? I now included the workaround to check if I'm passing a folder, but it's still unnerving.
The Documents.Open method opens the specified document and adds it to the Documents collection. It is designed to take the file path, not a folder. If you want to allow users to pick file(s) you may consider using the file open dialog. The FileOpenDialog triggered by your code which opens a folder for picking files allows specifying file formats that should be supported and visible via the dialog window.
Set dlgSaveAs = Application.FileDialog(msoFileDialogFilePicker)
dlgSaveAs.Filter = "Text Files (.txt)|*.txt|Word Documents (.docx)|*.docx|Word Template (.dotx)|*.dotx|All Files (*.*)|*.*"
dlgSaveAs.ValidateNames = true
Res = dlgSaveAs.Show

VBA code (if needed) to save new Word document in same directory as other Word doc

Many users, myself included, begin editing a Word document, create a new Word doc, and wish to save the second doc in the same directory as the first doc. This is the default behavior in an editor such as emacs. Word 2013, alas, makes me pick the directory ("Folder") where I wish to save the new file, forcing me to select among various network folders and then select multiple subdirectories before I reach the one containing my first document. I used to have a simple VBA script that would find the directory of the first file and then save the new file in that directory, or at least it would set the directory of the first file as the default directory for saving files. Does anyone have code to do what I'm looking for? Thanks!
This will the working directory to the directory of the first file you were working on:
ChDir(Application.Documents(Application.Documents.Count).Path)
If you want to save the most recent opened document to the same location as the first file you were working on, then try the following
Sub SaveWithFirstFile()
NewFileName = "filename1.docx
Application.Documents(1).SaveAs2 FileName:=Application.Documents(Application.Documents.Count).Path & "\\" & NewFileName
End Sub
Change "filename1.docx" to what you want it to

Macro (VBA) in Excel is failing on colleagues PC but works on mine when colleague is logged in

I'm developing VBA tools to automate a series of long winded administration tasks, the code runs fine in the following circumstances.
When I am logged into my PC
When my colleague is logged into my PC
When I am logged into my colleagues PC
However it fails to complete correctly, when my colleague runs it on her PC.
The specific area it is failing in is:
'creates 2 dims for location of the two files that need opening based on the critera set on the home page
Dim newdata As String
newdata = Range("f11").Value
Dim olddata As String
olddata = Range("f12").Value
Dim fileextension As String
fileextension = Range("f14").Value
Dim fulllocationolddata As String
fulllocationolddata = Range("f13") & olddata & fileextension
Dim fulllocationnewdata As String
fulllocationnewdata = Range("f13") & newdata & fileextension
'open file containing OLDDATA c&p previous days data to the conversion tool
'then shuts the old data workbook
Workbooks.Open Filename:=fulllocationolddata
Workbooks(olddata).Activate
Worksheets("sheet1").Select
Range("A1").CurrentRegion.Copy
Workbooks("Stockfile Conversion Tool.xlsm").Activate
Sheets("OLD STOCK").Activate
Range("A3").Select
Selection.PasteSpecial
Workbooks(olddata).Activate
Worksheets("sheet1").Select
Workbooks(olddata).Close SaveChanges:=False
The final line (Workbooks(olddata).Close SaveChanges:=False) does not shut the workbook, then later in the macro I open another workbook of the same name but as it's already open it just activates the window and the rest of the code falls apart.
If anyone has any ideas where I'm going wrong it would be appreciated.
Thanks in advance for your assistance
Plan303
Making my comments to an answer here:
Whether Workbooks("Name of the Workbook") is working or not depends on the settings in System control - Folder Options - View - [ x ] Hide extensions for known file types.
If this is set, then Excel's file extensions .xlsx, .xlsm, ... are not visible in Explorer or other file listings. Only the file names of the Excel files are visible. If so, then Workbooks("Name of the Workbook") will work.
If Hide extensions for known file types is not set, then Excel's file extensions .xlsx, .xlsm, ... are visible in Explorer or other file listings. If so, then Workbooks("Name of the Workbook") will not work. Then only Workbooks("Name of the Workbook.xlsx"), giving the name and the extension, will work.
But Workbooks("Name of the Workbook.xlsx") will also work if Hide extensions for known file types is set. So using the full name inclusive extension should be preferred.
So for the concrete question:
If olddata only contains the name of the workbook and not the file extension then Workbooks(olddata) will only work if Hide extensions for known file types is set in Folder Options. It will fail, if that option is not set and the file extensions are visible in Explorer. But Workbooks("Stockfile Conversion Tool.xlsm") will always work independent of whether Hide extensions for known file types is set or not. So Workbooks(olddata & fileextension) should also always work if olddata only contains the name of the workbook and fileextension contains .xlsx for example.

Attaching Excel File to finished Visual Basic project

I've created a Visual Basic project with many forms. In the project I write information to an excel file that I reference the location of on my local (C:) drive where the excel file resides. By reference I mean, I open an excel instance ten provide the source=local path on my hard drive. My question is how do I attach the excel file to the project so I don't have to reference the local location and can run the program on a different computer. In other words how do I bundle the excel file with the program? And how would I change the call to the excel file once I have it "bundled" to the program?
Add the excel file to the project. Go to solution explorer and right click on the excel file and select properties. Change the Copy to Output Directory to Copy Always. Now the file will be in the same directory as your exe. You can use Reflection.Assembly.GetExecutingAssembly to get the directory.
To add the file to the project:
Right click on project > Add > Existing Item > Your Excel File.xls
To include the file in build:
Right click on the file > Properties > Copy to Output Directory. Set this value to either Copy always or Copy if newer.
Here is the code to get the path to the excel file:
Dim exeDir As New IO.FileInfo(Reflection.Assembly.GetExecutingAssembly.FullName)
Dim xlPath = IO.Path.Combine(exeDir.DirectoryName, "Your Excel File.xls")
xlPath should now be the full path to the excel file.
MODIFIED REPLY AFTER USER COMMENTS
1) First create a Setting (named say MyExcelFile) to save the Name and Path of your excel file.
2) Now you can use My.Settings.MyExcelFile to refer to your excel file path.
3) If excel file is not found at the desired location, you can reset it by opening a File Open Dialog and asking the user to specify the file location.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Not IO.File.Exists(My.Settings.MyExcelFile) Then
Dim ofd As New OpenFileDialog
ofd.Filter = "Excel Files (*.xls, *.xlsx)|*.xls; *.xlsx"
ofd.Title = "Specify Excel File Location"
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
My.Settings.MyExcelFile = ofd.FileName
My.Settings.Save()
End If
End If
End Sub
i think iam not really getting what you mean but if you need to open the excel file with one click in the app and you dont want to write the path of the excel file (may be because you want you app portable) so the answer is to
put the excel file anywhere in the project folder
add existing item (*.* filter) and select the excel file
change the properties of the excel file build action: resource Copy to output directory:copy always
open the properties of the solution and click resources the drag the file and drop it
use the (using system.reflection & using system.io & using system.resources)
then write the code like this
string sPath = Path.GetTempFileName();
File.WriteAllBytes(sPath, Properties.Resources.excel File name here);
then take that (spath) and use it as your path so then you can change the project location with the excel file inside it and it will still be working without writing a constant path for it