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
385 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now