How can I get a macro to get the string for the ChDir path form a file the local user can update.

What I want to do is make a path.ini or path.config file where the user can enter the path of a folder where he or she wants the output files to be placed. Something like this I think ?
 Dim objFSO As Object, objFolder As Object, objFile As Object
 Dim sPath As String
 sPath= "C:\Program Files (x86)\myScript Folder\path.ini"

 ChDir = sPath

 ActiveWorkbook.SaveAs fileName:="Sample" & Format(Date, "_mm.dd.yyyy.") & "xls",   FileFormat:=xlText

 ActiveWorkbook.Saved = True
 ActiveDocument.Close

Open in new window


Than in the path.ini file the user can alter the path like:
C:\Program Files (x86)\myScript Folder\Files\

and where I have ChDir = sPath will use the path from the ini file to drop the file into that folder. I can't hard code the path because some of the users won't always be connected to the network and I need them to be able to set the path so they can upload or email the file to us.
GD_GRAYAsked:
Who is Participating?
 
xFDSxCommented:
Hey there; I meant that you have to specify the location of the path.ini in my code; so that it knows where the path.ini file is. In the case of my example path.ini is in the C drive, but you can put it anywhere...
If you want to read more than 1 line, you can just call the (st = oTS.Readline), every time you call readline it reads the next line in the file.... if you want to read more than that... well loop it..
0
 
xFDSxCommented:
Oh ok... so the path.ini will only have the path...

Function Readini() As String
    Dim st As String
    Dim fob As Object
    Dim fin As Object
    Set fob = CreateObject("Scripting.FileSystemObject")
    Set fin = fob.OpenTextFile("C:\path.ini")
    st = oTS.Readline
    oTS.Close
    Set oTS = Nothing
    Set oFS = Nothing
    Readini = st
End Function

that will read 1st line and return it.. so on your code up there... do this

ChDir =readini

Also specify the ini path....


As always...

THE CODE(SOFTWARE) IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
0
 
GD_GRAYAuthor Commented:
You said Also specify the ini path.... do I put it in the function only or also in the Sub someplace ?
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
GD_GRAYAuthor Commented:
Also can it be set to read line 1 or line 2 ?
0
 
Michael FowlerSolutions ConsultantCommented:
Another solution would be to have the user be prompted each time for a location

This function will prompt the user to select a folder and return the path

Michael

Function getFolder() As String

   With Application.FileDialog(msoFileDialogFolderPicker)
     .AllowMultiSelect = False
     .Show
     On Error Resume Next
     getFolder = .SelectedItems(1)
     Err.Clear
     On Error GoTo 0
   End With

End Function

Open in new window

0
 
GD_GRAYAuthor Commented:
I was able to get your code to work but only by changing the ChDir =readini to ChDir=("C:\") & readini;  Im not sure whats going to happen if the user wants to save the file on a mapped drive ? Any ideas ?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.