Code Problem?

Code Problem?

Anonymous
Not applicable
435 Views
3 Replies
Message 1 of 4

Code Problem?

Anonymous
Not applicable
Could someone please act as another set of eyes for me? The following
code works fine if there is a preexistent path, but if the code has to
write the path to the drive, it creates all the directories except the
last one. I can't seem to pin down the problem.
thanks in advance,
-Josh

Option Explicit
Public Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" (ByVal
lpExistingFileName As String, ByVal lpNewFileName As String, ByVal
bFailIfExists As Long) As Long
Public MyPath As String
Public MyName As String
Dim NewDir As String
Dim AlternatePath As String
Dim AlternatePathName As String

Public Sub test_Sav2Floppy()
Dim StringCount As Integer
Dim MidPath As String
Dim CopyCount As Integer
Dim testSlash As String
Dim DirectoryInfo As String
If Len(ThisDrawing.Name) > 7 Then
If Mid$(ThisDrawing.Name, 1, 7) = "Drawing" Then
MsgBox "You Must First Save the Drawing to a Name Using the
Saveas Command Before You Can Use Save to Floppy.", vbCritical
End
End If
End If
If Mid$(ThisDrawing.FullName, 1, 12) = "S:\DWG\Jobs\" Then
StringCount = Len(ThisDrawing.FullName) - 12
AlternatePathName = "A" & ":\" & Mid$(ThisDrawing.FullName, 13,
StringCount)
StringCount = Len(ThisDrawing.Path) - 12
AlternatePath = "A" & ":\" & Mid$(ThisDrawing.Path, 13, StringCount)
& "\"
Else
StringCount = Len(ThisDrawing.FullName) - 1
AlternatePathName = "A" & Mid$(ThisDrawing.FullName, 2, StringCount)

StringCount = Len(ThisDrawing.Path) - 1
AlternatePath = "A" & Mid$(ThisDrawing.Path, 2, StringCount) & "\"
End If
ThisDrawing.Save
If Dir(AlternatePath) = "" Then
NewDir = "A:\"
DirectoryInfo = AlternatePath
CopyCount = 4
Do While CopyCount < Len(DirectoryInfo)
testSlash = Mid$(DirectoryInfo, CopyCount, 1)
If testSlash <> "\" Then
NewDir = NewDir & testSlash
ElseIf testSlash = "\" Then
If Dir(NewDir, vbDirectory) = "" Then MkDir NewDir
NewDir = NewDir & testSlash
End If
CopyCount = CopyCount + 1
Loop
SaveDwg AlternatePathName
End If
End Sub
0 Likes
436 Views
3 Replies
Replies (3)
Message 2 of 4

Anonymous
Not applicable
I'll have to admit, I'm just too lazy to look through all of your code but
here's a routine I use to create nested paths. It will create only the
missing portions of a path up to, and including, the entire path if
necessary. BTW, it assumes the path is fully qualified, i.e. no relative
pathing allowed.

Public Sub MakePath(ByVal sPath As String)

Dim tokens() As String, CurrPath As String
Dim i As Integer

tokens = Split(sPath, "\")

CurrPath = tokens(LBound(tokens))

For i = LBound(tokens) + 1 To UBound(tokens)
CurrPath = CurrPath & "\" & tokens(i)
If Dir(CurrPath, vbDirectory) = "" Then MkDir CurrPath
Next

End Sub
0 Likes
Message 3 of 4

Anonymous
Not applicable
P.S. The Split function only exists in VB. If you need to do this in VBA,
use this instead:

Public Sub MakePath(ByVal sPath As String)

Dim tokens, CurrPath As String
Dim i As Integer

tokens = Parse(sPath, "\")

CurrPath = tokens(LBound(tokens))

For i = LBound(tokens) + 1 To UBound(tokens)
CurrPath = CurrPath & "\" & tokens(i)
If Dir(CurrPath, vbDirectory) = "" Then MkDir CurrPath
Next

End Sub

Public Function Parse(Str As String, Delim As String) As Variant
Dim tokens() As String, pos As Long, i As Integer

pos = InStr(1, Str, Delim, vbTextCompare)
i = 0

Do While pos > 0
ReDim Preserve tokens(0 To i)
tokens(i) = Mid$(Str, 1, pos - 1)
Str = Mid$(Str, pos + 1)
i = i + 1
pos = InStr(1, Str, Delim, vbTextCompare)
Loop

If Len(Str) > 0 Then
ReDim Preserve tokens(0 To i)
tokens(i) = Str
End If

Parse = tokens

End Function
0 Likes
Message 4 of 4

Anonymous
Not applicable


Thanks Frank,

    I must have stared at my code for an hour...grumble...grumble.
I'd set a watch, check the variables in debug, ect. and everything looked
good. I finally figured it out though.


Change:

Do While CopyCount < Len(DirectoryInfo)

to:

Do While CopyCount < Len(DirectoryInfo) + 1

duh!

thanks again,

-Josh
0 Likes