VBA - Open a folder on Mac - vba

I simply want to open a folder by VBA. I've tried the following code but it does nothing.
For Windows I know the following command is working fine
Call Shell("explorer.exe " & filepath, vbNormalFocus)
This is the code I am using...
Sub Open_Folder_On_Mac()
Dim folderPath As String
Dim RootFolder As String
Dim scriptstr As String
On Error Resume Next
RootFolder = MacScript("return (path to desktop folder) as String")
scriptstr = "do shell script ""open " & RootFolder & """"
MacScript (scriptstr)
End Sub
Can you help me, getting the code for simply opening a folder on a Mac? Thanks!

AppleScript does these kinds of things by telling a particular application to perform one or more of the commands exposed in the scripting dictionary (if any) provided by the application developer. In the case of the Finder (which runs at all times), It would be:
tell application "Finder" to open theFolder
where theFolder is an alias, file, or POSIX file object, for example from:
set theFolder to (choose folder)
Note that the open shell utility is normally used to open files and applications.
Untested examples:
The older (deprecated) style:
• Note that this command may not work in a sandboxed application.
Dim ScriptString as String
Dim FolderPath as String
ScriptString = "tell application " & Chr(34) & "Finder" & Chr(34) & " to open folder (path to desktop)"
-- or --
FolderPath = "Macintosh HD:Users:you:Path:To:Some:Folder:"
ScriptString = "tell application " & Chr(34) & "Finder" & Chr(34) & " to open folder " & FolderPath
MacScript(ScriptString)
The newer (2016+) style:
• Create a script using the Script Editor that contains the handlers (subroutines) you want to call, for example:
on openFolder(folderPath)
tell application "Finder" to open folder folderPath
end openFolder
• Place the script in your user's ~/Library/Application Scripts/[bundle id] folder, where [bundle id] is the bundle identifier of the application using the script.
• The new command is in the form AppleScriptTask("script file name.scpt", "handler name", "handler argument"), for example:
Dim FolderPath as String
FolderPath = "Macintosh HD:Users:you:Path:To:Some:Folder:"
AppleScriptTask("MyScript.scpt", "openFolder", FolderPath)
See Run an AppleScript with VB.

Related

VBA FileExists and Sharepoint

I'm running into issues trying to pull info from files stored in Sharepoint.
Namely, FileExists isn't working and Overwrite file doesn't seem to be working either.
There was a discussion here, but few answers -> posting this question again in hopes some things have changed
My code runs like this:
strFileExists = Dir(Filepath & Filename)
And returns: File path not found -> I checked the path and even opened a file and recorded the macro to make sure it was the same file path without issue, but it appears DIR() is the issue.
The business dept I'm working with is entirely switching over to Sharepoint so hoping there's a straightforward solution without setting up network shares or doing C/personal/OneDrive things
You can navigate and look for files on OneDrive like this
Sub check_File_Exists()
Dim path As String
Dim strType As String
Dim file As Variant
Dim yourFile As String
'replace uname with your user name
path = "C:\Users\uname\OneDrive\"
strType = "*txt"
yourFile = "test.txt"
file = Dir(path & strType)
Do While (file <> "")
If file = yourFile Then
Debug.Print ("File: " & file & " found!")
Exit Do
End If
file = Dir
Loop
End Sub
Hope it helps

Search and Match Partial Folder Name in Access VBA

I've looked at the top results when typing in the Title of this question, and I hit a dead end...
I have a list of customers, and each customer gets a Job Number [Job_Ref__]. In conjunction with this, each customer gets a folder in SharePoint for all of their documents. The naming convention is Job Number - Last Name, First Name. I want to be able to click a button on my Access form that opens the customer's specific folder, but it keeps opening "My Documents" on my local disk instead.
I've tried the below code without the customer's folder details, and it opens the root of the SharePoint 'drive' with no issue...
Below is what works when I click my OPEN FOLDER button on the form:
Private Sub Command232_Click()
Dim folderName As String
Dim folderfullPath As String
folderName = Me.FilePath
folderfullPath = "C:\Users\" & Environ("Username") & "\SharePoint Site\Customers 2020\"
Call Shell("explorer.exe " & folderfullPath, vbNormalFocus)
End Sub
When I use folderName is when I hit the issue; I've tried to wildcard the folder name, but to no avail:
Private Sub Command232_Click()
Dim folderName As String
Dim folderfullPath As String
folderName = Me.FilePath
folderfullPath = "C:\Users\" & Environ("Username") & "\SharePoint Site\Customers 2020\"
Call Shell("explorer.exe " & folderfullPath & folderName & "*", vbNormalFocus)
End Sub
Any help would be GREATLY appreciated, as I've hit a pretty big brick wall.
Of note: I tried to define folderName = Job_Ref__, but I figured that was too vague, so I added a FilePath field with macros in the Access Form that builds the customer's folder name Job_Ref__ - Last Name, First Name
None of this has worked - am I doing too much with this? XD
Windows allows comma in file name but Shell() function does not like. Options:
don't use comma in file name and use Replace() function in VBA to eliminate comma from field value to match file name
use FollowHyperlink to open folder - it does accept comma
FollowHyperlink(folderfullPath & folderName)

Issue with an LPR Command in VB

I am creating a VB app which will "move" xls reports from a directory to a ReportSafe app. I am also working in an existing VB app which does just that, so I am using it for reference.
It isn't as simple as moving files from one directory to another, because ReportSafe requires an lpr command to tell it (ReportSafe) which file to pick up.
Here is what I have so far:
Imports System.IO
Module Module1
Sub Main()
''Declarations
Dim Files As ArrayList = New ArrayList()
Dim FileName As String
''Write All Files in *directory* to ReportSafe
Files.Clear()
Files.AddRange(Directory.GetFiles(*directory*))
For Each FileName In Files
Dim RPname As String
Dim RealName As String
RPname = FileName.ToString
RealName = "/"
RealName = RealName & RPname.Remove(0, 34)
Dim a As New Process
a.StartInfo.FileName = "C:\Windows\system32\lpr.exe"
a.StartInfo.Arguments = "-S*ServerName* -Plp -J" & Chr(34) & RealName & Chr(34) & " " & Chr(34) & RPname & Chr(34)
a.StartInfo.UseShellExecute = False
Next
End Sub
End Module
The whole lpr command/arguments are throwing me for a loop. I'm not sure if my question is specific to ReportSafe, and if that's the case, I may be out of luck here. I have pulled this code from the already existing app which moves reports to ReportSafe, and adjusted for my own use, but no luck so far.
FYI, I had to turn on LPR Monitor services to obtain to the lpr.exe
Questions:
What are the proper arguments to pass through to this lpr command?
Is there a problem with the logic that is causing the issue?
I continued to tinker and look at my reference code and discovered some flaws in logic:
For one, the report name I was passing did not include the complete file path.
Another thing is that I never started the process with a.Start(). Rookie mistakes for sure... haha

Open Word Document From Dynamic Directory VB.Net

I have a program for which I have developed a user guide. I have placed this user guide within the project directory. I created a MenuStrip Item by which to open the user guide in Word on the user's machine. I was successfully able to do this with the following code:
Try
userGuide = MSWord.Documents.Open("C:Users\administrator\Documents\VisualStudio2010\Project3\UserGuide.doc")
MSWord.WindowState = Word.WdWindowState.wdWindowStateNormal
MSWord.Visible = True
Catch ex As Exception
MsgBox("An error has prevented the document from opening. The document may not be available." & vbCrLf & vbCrLf & _
"Please try one of the following options:" & vbCrLf & _
"- Check to see if the document is already open" & vbCrLf & _
"- Restart the program")
End Try
The problem is, the path used to open the file will not exist on the users machine. This is a standalone system, so no file share can be created in which to place the document, therefore no common path can be coded.
Is there a way to code dynamic paths? Perhaps something like:
userGuide = MSWord.Documents.Open("%windir%\UserGuide.doc")
Thanks!
if the document will be stored relative to the install path of the application executable, then start with the path of the exe:
Dim path As String
path = System.IO.Path.GetDirectoryName( _
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
Dim docPath as String;
docPath = Path.Combine(path,"UserGuide.doc");

downloading file using SFTP with VBA

My objective is to download, not upload a file from an SFTP server, and I am trying to adapt the code from another question on this site to do so (I pasted the code below for your convenience).
I downloaded PSFTP from Putty. PSFTP closes when I try to connect using the following command line:
open username:password#server.com.port:1111
I have three questions:
Is something wrong with my command line? If not then what could be the problem?
As far as I know SFTP would normally utilize get/put commands, but i don't see a put command in the code below, so I don't understand where I should enter the get command to download the file instead of uploading it (which is what the code below is supposed to be doing).
Is it correct that pRemotePath is the location of the file on the SFTP server, and pFile is the location I want the file downloaded to?
A simple explanation would be very much appreciated.
Public Sub SftpGet()
Const cstrSftp As String = """C:\Users\Ron\UtilityTools\psftp.exe"""
Dim strCommand As String
Dim pUser As String
Dim pPass As String
Dim pHost As String
Dim pFile As String
Dim pRemotePath As String
pUser = "uid"
pPass = "PW"
pHost = "dns"
pFile = "C:\Users\Ron\activity.txt"
pRemotePath = "Z:/activity.log"
strCommand = cstrSftp & " -sftp -l " & pUser & " -pw " & pPass & _
" " & pFile & " " & pHost & ":" & pRemotePath
Debug.Print strCommand
Shell strCommand, 1 ' vbNormalFocus '
End Sub
I think you should start with a Windows command prompt session. Work out the details of your command line there, as I suggested in an answer to a similar question: SFTP upload with VBA. Once you have a command line which works there, it will be very easy to execute that same command from VBA.
I've never used Putty's psftp.exe tool, only pscp.exe, so I can't offer help about how to construct your psftp.exe command line. One thing I noticed in Putty's documentation is that PSFTP (pscp.exe) can only work with a SSH-2 server --- if your target server supports only SSH-1, PSFTP will not work.
I think it would be worthwhile for you to review the Putty documentation at that link.