VBA store path including Environ("username") in a text file - vba

I am trying to create a text file that stores a folder path. This text file is then referenced via a vba sub. The path I want to use is something like:
"C:\Users\" & Environ("username") & "\AppData\Roaming\Microsoft\Templates"
This works fine in the sub but I've tried all kinds of variations in the text file but none of them get recognised and trigger error 52 - bad file.
Is there a way to make this work? I'm trying to allow people to set a different file path without needing to modify the code.

If you are trying to provide a path to the folder where user templates are stored then you could try
ActiveDocument.AttachedTemplate.Path
as an alternative (returns the path to the folder where the current template is stored for the user).
Otherwise store the path template as something like
"C:\Users\###UserName###\AppData\Roaming\Microsoft\Templates"
Which gives you a single string to retrieve. Then you can use the VBA Replace function to change the ###UserName### to the value of Environ("UserName)"
my_user_path = replace(my_path_template, Environ$("UserName"))
You might also want to explore using either a CustomDocumentProperty, or a Variables, to store your path template as this keeps the path template string as part of the Document or Template and not in a separate file.

Related

How to pass parameters to vb.net menu form at run time

As per #jmcilhinney, rewriting request/question from scratch.
I have a code for a form with listboxes (Vb.net) which can be viewed at this link:
Multiple Selections from Listbox to rename Folders
I would like to know if it is possible to store the variables, for example, folderPath, in a text file and have them read as the form loads rather than have them hardcoded in my vb code. I actually have 5 listboxes currently and other variables, for example folderPath, folderPath2. From my limited experience programming, I've been able to pass variables in an ini file to a simple c# program. It is incredibly simple and requires 1 line of code per variable. I'm asking if this can also be done in VB.net, then how the ini or config or appsetting or xml file would be designed (its structure) and finally how i would read those variables from the text file and assign them to my code. This would allow me to change folder paths and other variables from a text file rather than opening VS, editing the paths, and recompiling.
Code says: Folderpath = MyFolderPath
Ini file has: MyFolderPath = "c:/myfolders"
Project at runtime is then: Folderpath= "c:/myfolders"
Solution credited to #jmcilhinney. From the Project Menu in Visual Studio, select Settings.
Under Name, I gave a path variable the name MyPath.
Under Type, selected String since it's text.
Under Scope I selected User as it is just for personal use.
Under Value I put in the path that I wanted to assign. You enter the path without quotations mark. For example, C:\MyApp\Myfolders.
In the VS code, Form1.vb in my project, you declare the variable this way:
Dim folderPath = My.Settings.MyPath
This is the desired alternative to the hard-coded
Dim folderPath = "C:\MyApp|Myfolders"
Notice in VS Settings we don't use quotations marks, whereas in the VB code we do use quotation marks around path. So in summary, Dim folderPath = My.Settings.MyPath is the VB code to replace Dim folderPath = "C:\MyApp|Myfolders"
The last step is to go the folder where your .exe is and open the file with the .config extension. In that file, you will see these tags, under the closing tag for userSettings. Once more the path does not have quotation marks. Note: this .config file was generated by VS when compiled (build).
<ModMenuR1.My.MySettings>
<setting name="MyPath" serializeAs="String">
<value>C:\MyApp|Myfolders</value></startup>
</setting>
</ModMenuR1.My.MySettings>
Now we can change the path variable, MyPath, in the value tag without Visual Studio, without recompiling, just by editing the .config file. This allows me to repurpose the form menu displayed in the image. A batch file could also be used to swap .config files just before run time. The actual project and config file will have at least 5 path variables.

How can I get the disk path of a word document using VBA? Document.Path returns the web path instead

I have a word document with some vba code. I want to return the path of the directory in which the word document is saved.
This is the code I used:
Dim PathCurrentDocument As Variant
PathCurrentDocument = ActiveDocument.path
Debug.Print PathCurrentDocument
The desired output is:
"C:\Users\firstname.lastname\OneDrive - company name\VBA\4_update_documents_with_vba_inside_word\document_templates_to_modify"
What I am getting, is the web link, which looks like this:
https://my-company.sharepoint.com/personal/my_name/Documents/VBA/4_update_documents_with_vba_inside_word/Document_A_Template_with_macro.docm
The microsoft documentation only says the property returns the disk or web path to the document. It does not say how to choose which one to return.
Depending on where the document is coming from the Document.Path property returns the disk or Web path to the document.
Use the Document.SaveAs2 method if you want to get a local file path. Basically, you need to save the file on the hard drive anywhere.

File Path of Access Database

I'm working with vb.net 2008 as well. But I have a question.How to remove a file path like this C:\users\myDocu\debug\Dbase.accdb and I only want is the file name Dbase.accdb. Because I want to transfer my files in another computer but the problem is the file path. I always need to change the entire location in my codes to run without debug.
To get the filename without the path, you can use Path.GetFileName.
But if you want a painless way to find a place to store your database, consider putting it into the application data folder (AppData). You can get this folder with Environment.GetFolderPath and Environment.SpecialFolder.ApplicationData, using it like this:
Dim pathToDb = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"Dbase.accdb")
if you want to use the file locally. If you want to share the file between different instances of your application in a network, put the path e.g. in a config file like App.Config.
Try this:
Dim FullFilePath As String
Dim FileName As String
FullFilePath = "C:\users\myDocu\debug\Dbase.accdb"
FileName = Mid(FullFilePath,InStrRev(FullFilePath,"\") + 1)

MS Access replace part of file name with current directory

I'm trying to use MS Access to make a simple database of the store I work in and I have to insert photo's and I'm doing so by having a column where the filepath is stored eg. "C:\User\WL\Documents\Databasefolder\Pictures\img_xxxx.jpg" but when I hand over the file to my boss, the filename will be wrong as he will put it in a different folder.
So I'm looking for somekind of function that changes the "C:\User\WL\Documents\Databasefolder" to the current directory of the database with included pictures. I have almost no experience with SQL or Access alltogether, hence I came here.
You can retrieve the path for the user's Documents folder like this:
DocFolder = Environ("HOMEDRIVE") & Environ("HOMEPATH") & "\Documents"

VBA How to get path to The Current Users Application data folder?

In general,
Using VBA, how do I determine where the Current users Application Data folder is?
The FileSystemObjects special folders only knows about 3 folders
WindowsFolder
SystemFolder
TemporaryFolder
Specifically, I need a Word Macro to copy a file to the a folder under the Application Data folder.
e.g. In VB.Net I can use My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData to do this
You can use Environ("AppData") to get this path. Environ will pull any system variable, which can be found by using the set command at the DOS prompt.
Using advapi32.dll, you can get the USERPROFILE via
Environ("USERPROFILE")
Connect this with the "Application Data" directory (which has a standard, specific name) to get what you want
CStr(Environ("USERPROFILE") & "\Application Data")
For more information, check out MSDN