vba dir function error 5174 - vba

I have been getting a runtime error 5174. In the directory there are .docx and .docm files. I have tried to add .doc files, as I read .docx are not supported by the dir function. Following adding the file, the code will go through all files in the directory like it should. If I run it again, it will fail with an error 5174. Any help would be greatly appreciated.
sMyDir = "C:\weekly\" & "*.doc?"
sDocName = Dir(sMyDir)
While sDocName <> ""
Documents.Open FileName:=sDocName, Visible:=False
' Does stuff
sDocName = Dir()

Error 5174 indicates file not found, which means it is possible that the directory is incorrect. Try using LIKE:
sMyDir LIKE "C:\weekly\" & "*.doc?"
Edit: Your sDocName does not provide with a full path as well

The result was the Dir function returns the file name only. So when I used the open method I added the path before the variable which stored the filename. I just needed to read the documentation on the Dir function.
Thanks

Related

Get full path to .exe file if only a subfolder path to it is given

I have to find the rscript.exe that exists in a folder called bin but I do not have the full reference to this folder.
Full Reference would be:
C:\Program Files\R\R-3.5.3\bin
but I do not know the version part beforehand. So I must go to
C:\Program Files\R and search the subfolders for the rscript.exe and recieve the full path to it.
I have already searched but all I could find was how to find out the path of the running application via reflections and tried out to rewrite the code without any solution. Could anyone help me here?
You can use the Directory.GetDirectories method with the searchPattern argument and loop throught the subdirectories:
Dim rootPath As String = "C:\Program Files\R"
If Directory.Exists(rootPath) Then
Dim currPath As String
For Each subFolder As String In IO.Directory.GetDirectories(rootPath, "R-*", SearchOption.TopDirectoryOnly)
currPath = IO.Path.Combine(subFolder, "bin", "rscript.exe")
If IO.File.Exists(currPath) Then
MessageBox.Show("File found at " & currPath)
End If
Next
End If

Check if directory exists, but string is relative path

I am currently writing a program to check if hyperlinks in a file are broken. Some hyperlinks go directly to folders on a mapped drive.
I want to use dir() to check if the folder exists, but Excel shortens the string to the relative version "../../Random folder/Another random folder"
This string returns "" so my function thinks the folder does not exist, but it does and its link functions properly.
Any help is greatly appreciated.
Thanks!
I believe you need to use the overloaded function of Dir and specify vbDirectory as the additional argument. I believe it should look something like this:
If Dir("X:\random directory\other random directory", vbDirectory) = "" Then

How to open a file from folder where EXE was opened. VB

Part of a program I am making I need to open a file (for example a txt file) from the folder where the program was opened.
The idea is that it can be zipped up and put anywhere without having to place the file in a certain location.
It's got to be Visual Basic and I will really appreciate some help.
I have googled this but found nothing for VB. I'm relatively new to the language.
Thanks, Jack
To open the file do this:
Dim fileName as String = "yourfile.txt"
Dim appDir as String = System.IO.Path.GetDirectoryName( _
System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
Process.Start(appDir & "\" & fileName)
You can use this to get the path to the folder where the currently executing assembly (i.e. the EXE) is located:
System.Reflection.Assembly.GetExecutingAssembly().Location.Substring(0, assembly.Location.LastIndexOf(System.IO.Path.DirectorySeparatorChar))

How do I specify the file path in vba code in powerpoint 2011 for mac?

I want to access an image file in my VBA code for PowerPoint 2011 for Mac.
I have used both "/" and ":" as separator but it didn't work.
It works only when I save both .pptm file and the image file in the same folder and specify the relative path for the image file as below:
Dim strPath As String
strPath = "image.png"
But if I try saving the file at a different location than the .pptm file and provide the full path for the StrPath as:
strPath = "Users:Me:Desktop:image.png"
it throws a runtime error saying "the specified file wasn't found".
I have kept in mind the case sensitivity in case of Mac but nothing seems to be working for me.
Can anyone please help me in finding the possible workaround so that I can save the .pptm file and the image file at different locations on Mac?
Try:
Presentations.Open "Macintosh HD:Users:Me:Desktop:filename.ext"
Substitute appropriate strings for the name of the HD, your user name and the file.

SaveAs file name with new format in current folder

I have setup the following macro in a Word Template to save my file to the place where the template is current saved, but in a sub-folder with a new name and file format. Each time I run the macro I get a "Command failed" Error.
Here is my code:
pathName = ActiveDocument.Path & "\Periodic Count\CaseManager_CSV.txt"
ActiveDocument.SaveAs fileName:=pathName, FileFormat:=wdFormatText
I think the reason of your problem stem from unsaved document which doesn't have a path. As a result your pathName variable value is only like this: "\Periodic Count\CaseManager_CSV.txt" which is incorrect.
As you mentioned you want to save in the subfolder of the folder where your template is saved. Possibly you should modify pathName variable in this way:
pathName = ActiveDocument.AttachedTemplate.Path & "\Periodic Count\CaseManager_CSV.txt"