PCG FAQ

[Use your browser's BACK button to return to the PRIME FAQ Page or click here if you came directly to this page.]

(Code) Bullet-proof Path and Filename Assembly Using Excel/VBA and WordBasic

This was taken from the PRIME for Excel and PRIME for Word APIs. These APIs are available in our Word and Excel shareware packages which can be downloaded from our Products page.

We know you’ve encountered this situation before: your code’s got its hands on two separate components of a fully qualified filename, a pathname like "C:\This\Is\A\Convoluted\Folder\Listing" and a root filename like "(Code) Bullet-proof Path and Filename Assembly Using Excel-VBA and WordBasic.doc". Sometimes the pathname includes a folder separator character (usually a backslash "\") at the end and sometimes it doesn’t (more on this in a moment). And your goal is to concatenate these components into a fully qualified filename. The simple but often-repeated question is whether or not you need to stuff in a separator character between the pathname and the root filename.

Wouldn’t it be nice to have a function in a common code procedure library that would take the two components and handle this for you, returning a perfect, fully qualified filename every time? That’s what we did with our WordBasic function fPathFromParts$ and its Excel VBA counterpart fstrPathFromParts. (Written by Jonathan Sachs.) Often it’s these logically simple and straightforward operations that we tend to ignore as fodder for inclusion in a procedure library, even though they occur frequently. After all, it’s so "simple" to just test for the trailing separator with in-line code. Wrong. You’ll generate more error-free code more quickly by constantly being on the lookout for these types of operations and storing their solutions in a code library.

In fact, we had a case where not using fPathFromParts$ in one macro came back to haunt us. It was a setup routine macro, and it turns out that in Word 6 the WINWORD6.INI file’s various path keys — like STARTUP-PATH — are just as likely to have a trailing separator as not. Go figure. The code in question would assume there was no separator. Half the time it’d be right and the other half, ahem, wrong, resulting in a run-time error. So we fixed the code by using fPathFromParts$. One bug squashed.

Shown below is a WordBasic example that calls the function, presumed to be stored in the procedure library macro GeneralLib, followed by both the Excel VBA and WordBasic versions of the function. (There are some slight syntactic differences between the VBA and WordBasic versions but essentially it’s the same code.)

' WordBasic calling example
Sub MAIN
    Path$ = "C:\Data\Winword\Docs"
    File$ = "Delete1.doc"
    Kill GeneralLib.fPathFromParts$(Path$, File$)
End Sub

' Excel/VBA...
' --------------------------------------------------------------------
' Purpose:  Assemble a valid path name from its parts.
'           Written for PRIME 6 for Word by Jonathan Sachs.
'
' Inputs:   strPath - the pathname, which may be null
'           strFile - the file name
'
' Returns:  String. A complete path name, consisting of the path, a
'           backslash if necessary, and the filename.
'
' Updated:  2/17/95 - ported from PRIME 6 for Word this date
' --------------------------------------------------------------------
' Copyright © 1994-96 PRIME Consulting Group, Inc.
' --------------------------------------------------------------------
Public Function fstrPathFromParts(strPath As String, strFile As String) As String
    Dim strS As String
    strS = ""
    If strPath <> "" Then
        If Right$(strPath, 1) <> "\" Then
            strS = "\"
        End If
    End If
    fstrPathFromParts = UCase$(strPath + strS + strFile)
End Function

' WordBasic...
' --------------------------------------------------------------------
' Purpose:  Assemble a valid path name from its parts.
'
' Inputs:   Path$ - the pathname, which may be null
'           File$ - the file name
'
' Returns:  A complete path name, consisting of the path, a backslash 
'           if necessary, and the file.
'
' Updated:  (PCG: JS)
' --------------------------------------------------------------------
' Copyright © 1994-96 PRIME Consulting Group, Inc.
' --------------------------------------------------------------------
Function fPathFromParts$(Path$, File$)
    s$ = ""
    If Path$ <> "" Then
        If Right$(Path$, 1) <> "\" Then
            s$ = "\"
        EndIf
    EndIf
    fPathFromParts$ = UCase$(Path$ + s$ + File$)
End Function

The Naked PC
Subscribe to our free electronic newsletter. Get the latest on all things PC, updates to PRIME Freeware page, and more. Type your email name and click Subscribe.

Return to Top