How to get Dropbox directory using VBA? - vba

Dropbox for Teams are doing an upgrade on December 10, 2022
As part of the upgrade, the directory of Dropbox will be changed from Dropbox (me) to me (Dropbox).
This is part of the email we received.
The Dropbox folder name will change from “Dropbox [team name]” to
“[Team name] Dropbox.” You may need to modify any third-party
automation file paths that are using the old Dropbox folder name for
them to work properly after the upgrade.
At the moment we hard-code the dropbox file path in our VBA code
fromPath = "C:\Dropbox (me)\Development\" + aDir + "\"
Is there are a way of replacing this with code that will work both before and after the change.
For instance
fromPath = getDropBoxPath()+"\Development\" + aDir + "\"

The following code will do what I want. It is based upon #Reinaldo's answer in How do I programmatically locate my Dropbox folder using C#?.
Function getDropboxPath()
jsonPath = Environ$("LocalAppData") + "\Dropbox\info.json"
Open jsonPath For Input As #1
Input #1, firstLine
aSplit = Split(firstLine, Chr(34) + "path" + Chr(34) + ":")
Close #1
getDropboxPath = aSplit(1)
End Function
I know the code can be improved but this is probably enough for me at the moment.
It uses the info.json file provided by dropbox and relies on the first line of the .json file being something like this.
{"business": {"path": "C:\\Dropbox (me)"

Related

How to declare a path without the drive letter?

Example i have picture folder located at
Dim path As String = "D:\Student Picture\Student-" + textbox_Search.Text + ".jpg"
Something like that. now i want the the picture folder to just paste it in debug folder can i call it without the drive letter something like this ??
Dim path As String = "\bin\debug\Student Picture\Student-" + textbox_Search.Text + ".jpg"
Its not working. The reason why i want to achieve this is example i use my program in another computer but the other computer don't have drive D: then my program will not work because in my code all of the student picture declared in drive D: thank you so much.
You can get the application startup path and create the folder in either the Debug or Release locations, depending which you're running. The folder would be created within the directory in which the end-user runs the compiled application.
Dim PicPath As String = Application.StartupPath & "\Student Picture
If Not My.Computer.FileSystem.DirectoryExists(PicPath) Then
My.Computer.FileSystem.CreateDirectory(PicPath)
End If
PicPath &= "\Student-" & textbox_Search.Text & ".jpg""
Small edit- I realised you wanted to include file name as well. Please do not use "+" when concatenating strings, the proper operator is "&" use the "+" operator only when youre doing math and some other unique cases, such as datatable expression column concatenation.

Solidworks VBA save as new name

I am currently generating a solidworks part file name from a text document for a batch "rename" of the documents. This assigns sequential numbers to them which i then save each part in a folder as. I have been trying to figure out a method for the new file names to be referenced in the assembly they were a part of.
For saving the document I am using :
swModel.SaveAs3 "" & FileName & "", 0, 0
I have no issue creating all the files in a batch but I could not seem to find a method of apply this save in such a way that any open documents that it is referenced in change to referencing the new part name.
Edit: I figured out a serviceable solution using some of the information held in this link:
help.solidworks
Using the pack and go functionality in solidworks avoids having to deal with the references as they are already handled by the process itself Hope this helps.
Try the below approach:
Dim FileName As String
FileName = "blablabla" & Variable & "blablabla"
swModel.SaveAs3 ("C:\User\Username\Desktop" + FileName + ".SLDPRT", 0, 1)
Variable = Variable + 1
'continue with your loop

Kill Command Deleting Wrong File(s)i

In Access VBA, I have a procedure I've put together to do this:
Allow the user to select zip file(s)
Extract any files from the zip files to the same directory (In this
specific use-case instance, it is ALWAYS extracting Excel files from
Zip files, never any change, and always using the same password)
Then I want the code to Delete the Zip file after extracting the
.xls file.
Everything works beautifully except the delete file portion. The issue is that when I tell it to delete "FileName.Zip", it is deleting "FileName.Zip" AND "FileName.xls"
Is there any way to make sure that he kill command ONLY deletes what I want it to delete? I've used it before on various occasions, and never had this happen before.
Here is the code I am using:
Dim fd As FileDialog
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim i As Variant
Set db = CurrentDb
Set rs = db.OpenRecordset("tblProjectPath")
Set fd = FileDialog(msoFileDialogFilePicker)
fd.AllowMultiSelect = True
fd.Title = "Select TOC Return File(s) to process"
fd.InitialFileName = rs.Fields("ProjectPath") & "\FilingReports\*.zip"
fd.Show
For Each i In fd.SelectedItems
'Debug.Print i
Debug.Print '------------------------'
Debug.Print i
Unzip (i) 'The bit calling the command line unzip utility to unzip the file - just telling it to extract all files to the current folder.
Debug.Print i
'Kill i
'had to take out the kill bit, b/c it was deleting both the .zip and .xls files which is not desired nor expected
If InStr(i, ".zip") Then
Kill i 'Tried to specify only .zip files even though think I shouldn't need to, but it's still deleting .xls files
End If
Next i
Edit: Add Unzip code to post:
Unzip code:
Sub Unzip(Path As String)
Dim strUnzip As String
Dim QU As String 'quotation mark
QU = Chr(34)
strUnzip = QU & "c:\program files (x86)\winzip\wzunzip" & QU & " -s" & _
"ZipPassword " & _
Path & " " '& _
Call Shell(strUnzip)
End Sub
At this point, I don't really think a "real" answer will come about. However, I'll post what I've decided to do with the particular process I'm writing this code for anyway.
I'm going to use a folder structure to divide up the files:
1. Place zip file(s)
2. Unzip files to a 2nd folder
3. After processing Excel files in 2nd folder, move to a 3rd "complete" folder.
This will get around the deleting wrong files bit.
Also, it appears that the cause for the issue is related to something to do with the call to the WinZip Command Line Unzip utility (wzunzip) in the Unzip code above, or else something with the tool itself. I thought that maybe it was b/c the tool was asking me if I wanted to overwrite existing files, but that wasn't the case, b/c I had the same issue when there were no files to overwrite.
Anyway, I'm attempting to close this one up at this point. Thanks to Wayne G. Dunn for his assistance on this.

Visual Basic.NET Saving Preferences in a .txt or .ini

I want to save my preferences for a program. I just want to know how to do it, since I cant get how to save this file at my documents, because every computer has a different name and username, but using the
My.User.Name
returns 'USERNAME-PC-USERNAME' which doesn't work.
For example, I want the program to create this text file 'CPreferences' at 'C:\Users\'username'\Documents'. And then when the program is re-opened it will automatically load this file into itself, the file will basically contain 2 lines:
Option1:<Value 1>
Option2:<Value 2>
The values will represent choices, ofcourse. So fundamentally, my question is:
1) How to save this file automatically at this user's documents WITHOUT showing a savefiledialog?
2) How to automatically load it if the file there exists?
Thanks,
VB.Net has built-in features for storing and retrieving the user's settings. Just use them. You won't have to worry about how the settings are stored or where the files are. It all just works.
Using something along the lines of string = "----------- " & CurrentTitle & " (" & Now.ToString() & ") User " & Environment.UserName & " on computer " & Environment.UserDomainName & "------------"
will result in
----------- Google - Google Chrome (3/12/2012 2:09:49 AM) User Drise on computer Drise-LAPTOP ------------
Using a streamwriter with filename "C:\users\" & Environment.UserName & "..." should do the trick.
To auto-load the file, check if the file exists and use a streamreader with the same path variable as above, or if not, then set default settings.
For saving to the "My Documents" folder for the current user, this will return the directory path:
My.Computer.FileSystem.SpecialDirectories.MyDocuments
You can also use other items in "special directories" if you want things like the "downloads" or "my pictures" folders, or anything else like that.
I think what you're looking for is user configuration.
You can store settings for users that will be unique to the logged in user.
Use this as a reference for getting started with it:
http://www.codeproject.com/Articles/12252/Application-settings-in-VB-NET-2-0-and-Visual-Stud

Visual Basic in Word 2010 - Open Dir - Relative Path

I'm creating a word document and I'm having trouble working out the code to open a specific directory.
Basically, I'll have a bunch of video files on a USB drive or a DVD in separate folders and when the command button is clicked, I would like it to open the relevant directory in a windows explorer window.
I did some Google searching and came up with the following code:
FolderName = "c:\windows"
TaskID = Shell("explorer.exe " & FolderName, vbNormalFocus)
The only problem now is that I need to change the path reference from absolute to relative, as I'll be running everything from either a USB drive or a DVD on multiple computers.
I have tried every possible iteration of relative path references, but I can't seem to get it to work. The folders are simply sub-directories of where the word document is.
Anyone got any clues as how I can change the above code to be relative rather than absolute path references?
I think something like this might be what you're looking for:
FolderName = ActiveDocument.Path & "\SubFolder"
TaskID = Shell("explorer.exe " & FolderName, vbNormalFocus)
Assuming that your document is the active one, otherwise you might need to search through the Documents collection for the correct one.