Results 1 to 11 of 11
  1. #1
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Copying to a path that may not exist (VBA Word 2000)

    As part of an installation procedure, I need to copy a file to a specific directory:

    copy c:tempfile.fil

    to

    c:folder1folder2file.fil

    The problem is that folder1 and/or folder2 may not exist.

    What is the best way around this problem?

    Thanks!!
    Troy

  2. #2
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Toronto, Ontario, Canada
    Posts
    1,139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copying to a path that may not exist (VBA Word 2000)

    Troy,

    I would do it like this:

    <font color=448800>'Make Directories</font color=448800>
    MkDir "c:folder1"
    MkDir "c:folder1folder2"
    <font color=448800>'Rename and move file</font color=448800>
    Name c:tempfile.fil as c:folder1folder2file.fil

    and in you error handler, you do have an error handler, right? <img src=/S/grin.gif border=0 alt=grin width=15 height=15>, trap for Error #75 and Do a Resume Next

    Select Case Err.Number
    Case 75
    Resume Next
    Case Else
    'The rest of your error handler
    End Select

    Error 75 is a Path/File Access Error, which indicates the folder already exists
    --
    Bryan Carbonnell - Toronto <img src=/S/flags/Ontario.gif border=0 alt=Ontario width=30 height=18> <img src=/S/flags/Canada.gif border=0 alt=Canada width=30 height=18>
    Unfortunately common sense isn't so common!!
    Visit my website for useful Word, Excel and Access code, templates and Add-Ins

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Copying to a path that may not exist (VBA Word 2000)

    You are not the first to ask! See <!post=Post #147325,147325>Post #147325<!/post>.

  4. #4
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copying to a path that may not exist (VBA Word 2000)

    I love it!! I actually did search all the way back to January. Either I missed this or I didn't use the right key words to find it.

    I did, however, make a few changes to make this a standalone sub to which I could pass the path as a string. I commented out the message boxes since they would only get in the way for my current purposes. I've included my code below:

    <pre>Sub FolderTest(strFullPath)
    Dim strTestPath As String ' strFullPath As String,
    Dim intStart As Integer, intBSlash As Integer
    'strFullPath = "c:level1level2level3"

    intStart = 4 'start after the drive designation

    Do
    'determine location of "next" backslash in path
    intBSlash = InStr(intStart, strFullPath, "")
    If intBSlash = Len(strFullPath) Then
    'terminal , should be ignored
    Exit Do
    ElseIf intBSlash = 0 Then
    'remainder of string is last level
    strTestPath = strFullPath
    Else
    'extract path to intermediate folder level
    strTestPath = Left(strFullPath, intBSlash - 1)
    End If
    'test for existence of folder and create if desired
    If Dir(strTestPath, vbDirectory) = vbNullString Then
    ' If MsgBox("Folder " & strTestPath & " was not found. Create it?", _
    ' vbQuestion + vbYesNo) = vbYes Then
    MkDir strTestPath
    ' Else
    ' Exit Do 'proceeding will result in an error
    ' End If
    End If
    'exit if we have exhausted the full path string
    If Len(strTestPath) >= Len(strFullPath) - 1 Then Exit Do
    'otherwise, reset the search starting position and loop
    intStart = Len(strTestPath) + 2
    Loop
    'MsgBox "Done!"
    End Sub
    </pre>

    <pre>'Test usage of previous sub.
    Sub testdir()
    strFullPath = "c:twtesttwtesttwtest2"
    FolderTest (strFullPath)
    End Sub
    </pre>


    I was worried at first that I had done something wrong when the parenthesis wouldn't stay next to the sub name, but it seemed to work anyway in this case. My limited and feeble past experience has shown that it can indicate a problem sometimes when that happens. I guess not in this case.

    Thanks again!!
    Troy

    P.S. I put in a request to the moderator to see if we can get a count of the hits when we do a search. It would really help make better searches when we can know if we are getting 30 hits or 300. I haven't heard anything, but we'll see.

  5. #5
    5 Star Lounger
    Join Date
    Dec 2000
    Location
    Reading/Swindon, Berkshire, United Kingdom
    Posts
    664
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copying to a path that may not exist (VBA Word 2000)

    Troy,

    re the search engine: This has come up before and didn't happen: I believe that it only knows that there are more hits than are currently displayed and not how many more - I think when this came up the reason given was that the engine only searches a page at a time, then stops, in order to cut down on processor overhead or some other such reason. (Well, presumably a page and one line, but that's being pedantic.)

    HTH - or at least explains!

  6. #6
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copying to a path that may not exist (VBA Word 2000)

    That does explain why it is as it is.

    However, if you will permit me to continue whining, these forums, especially the VB/VBA forum contains a vast treasure trove of information. Thus, searching that information is almost as valuable as posting new information. It may take a total redesign (and I may not know what I ask), but if I had the chance, I'd do to allow a much greater opportunity for refined searches and so I don't waste the time of incredibly helpful people such as yourself with questions that have already been answered.

    Thanks for the ear!! <img src=/S/eargear.gif border=0 alt=eargear width=20 height=20>
    Troy

  7. #7
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copying to a path that may not exist (VBA Word 2000)

    Troy,

    Post a whinge in the "Lounge Matters" forum.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  8. #8
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Copying to a path that may not exist (VBA Word 2000)

    > Either I missed this or I didn't use the right key words to find it.

    I cheated, I searched for MkDir. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

    > I was worried at first that I had done something wrong when the parenthesis wouldn't stay next to the sub
    > name, but it seemed to work anyway in this case.

    Yes, well, you should be very wary of that and remove them when it happens. For background reading, see:

    Parentheses = passing by value
    Re: ByVal (VBA/Excel 2000)

    It came up within the past week, and also last month, but I can't find those threads. Trust me, it happens all the time. <img src=/S/smile.gif border=0 alt=smile width=15 height=15>

  9. #9
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Copying to a path that may not exist (VBA Word 2000)

    Whine all you please. Unfortunately, the Lounge is an incredibly complex web site, and a much more refined search feature isn't in the cards for the foreseeable future. We've coaxed <img src=/S/eileen.gif border=0 alt=eileen width=17 height=15> and <img src=/S/claude.gif border=0 alt=claude width=21 height=21> into adding some refinements along the way, but additional improvements will come slowly. <img src=/S/shrug.gif border=0 alt=shrug width=39 height=15>
    Charlotte

  10. #10
    4 Star Lounger
    Join Date
    Apr 2001
    Posts
    482
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copying to a path that may not exist (VBA Word 2000)

    I guess one workaround for now would be to change the number of threads displayed at one time to some rediculously high number, such as 999. Obviously it would take longer, espcially if you don't have a fast internet connection, but at least you would know how many hits you had.

    Thanks for giving me a realistic picture!! And please don't get me wrong. I love this place!! I just hate posting things again and again that have already been dealt with since it makes searches for myself and others in the future more difficult. I deleted a post of mine just yesterday (before any replies had been sent) when I finally found a response. I hope that's OK.

    Thanks again!!
    Troy

  11. #11
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Copying to a path that may not exist (VBA Word 2000)

    Don't worry about asking questions already asked too much.
    I have a set of boilerplate answers to common questions (reg. Excel) and I simply copy-paste them into a reply.

    I did a count on a boilerplate answer I have ready for a specific question in the Microsoft newsgroup yesterday and I got 156 hits on that answer...
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •