Solved

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

Posted on 2013-06-03
6
403 Views
Last Modified: 2013-06-06
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.
0
Comment
Question by:GD_GRAY
  • 3
  • 2
6 Comments
 
LVL 1

Expert Comment

by:xFDSx
ID: 39217957
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
 

Author Comment

by:GD_GRAY
ID: 39218056
You said Also specify the ini path.... do I put it in the function only or also in the Sub someplace ?
0
 

Author Comment

by:GD_GRAY
ID: 39218074
Also can it be set to read line 1 or line 2 ?
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 23

Expert Comment

by:Michael74
ID: 39218162
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
 
LVL 1

Accepted Solution

by:
xFDSx earned 500 total points
ID: 39219822
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
 

Author Closing Comment

by:GD_GRAY
ID: 39226170
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question