Microsoft KB Archive/108351: Difference between revisions

From BetaArchive Wiki
(importing KB archive)
 
m (Text replacement - "&" to "&")
 
(One intermediate revision by the same user not shown)
Line 49: Line 49:
<br />
<br />
For example, the following Visual Basic code will fail
For example, the following Visual Basic code will fail
<pre class="codesample">  ChDir &quot;C:&quot;
<pre class="codesample">  ChDir "C:"
                 </pre>
                 </pre>
and you will receive the following error message:
and you will receive the following error message:
Line 59: Line 59:
</div>
</div>
The following lines of code will succeed, because they each contain a backslash:
The following lines of code will succeed, because they each contain a backslash:
<pre class="codesample">  ChDir &quot;C:\&quot;
<pre class="codesample">  ChDir "C:\"
                 </pre>
                 </pre>
<div class="indent">
<div class="indent">
Line 67: Line 67:


</div>
</div>
<pre class="codesample">  ChDir &quot;C:\EXCEL\LIBRARY&quot;
<pre class="codesample">  ChDir "C:\EXCEL\LIBRARY"
                 </pre>
                 </pre>
It is possible to incorporate error-handling into a ChDir function to prevent an error from occurring. The Visual Basic code example below demonstrates one way to do this.
It is possible to incorporate error-handling into a ChDir function to prevent an error from occurring. The Visual Basic code example below demonstrates one way to do this.
Line 74: Line 74:
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. This Visual Basic code example displays an input box which asks you which drive or directory you would like to switch to. When you enter a drive or directory, the example checks whether the path you enter contains a backslash and adds one, if needed, before switching to that drive or directory.<br />
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. This Visual Basic code example displays an input box which asks you which drive or directory you would like to switch to. When you enter a drive or directory, the example checks whether the path you enter contains a backslash and adds one, if needed, before switching to that drive or directory.<br />
<br />
<br />
To run the example, position the insertion point in the line that reads &quot;Sub MainMacro()&quot; and press F5.
To run the example, position the insertion point in the line that reads "Sub MainMacro()" and press F5.
<pre class="codesample">'----------------------------------------------------------------------
<pre class="codesample">'----------------------------------------------------------------------
Option Explicit
Option Explicit
Line 83: Line 83:
   'Prompts you to enter a drive/directory. For example, D: or C:\EXCEL.
   'Prompts you to enter a drive/directory. For example, D: or C:\EXCEL.
   'If you type in an invalid directory, the subroutine will fail. For
   'If you type in an invalid directory, the subroutine will fail. For
   'example, typing just &quot;C&quot; (without the quotes) is not going to work.
   'example, typing just "C" (without the quotes) is not going to work.
   NewDir = InputBox(&quot;Switch to which drive/directory?&quot;)
   NewDir = InputBox("Switch to which drive/directory?")
   'If the NewDir ends in a colon, indicating it is a root directory,
   'If the NewDir ends in a colon, indicating it is a root directory,
   'concatenate a backslash onto the end of it. For example, C: would
   'concatenate a backslash onto the end of it. For example, C: would
   'become C:\. If you actually enter &quot;C:\&quot;, the subroutine doesn't add
   'become C:\. If you actually enter "C:\", the subroutine doesn't add
   'another backslash.
   'another backslash.
   If Right(NewDir, 1) = &quot;:&quot; Then
   If Right(NewDir, 1) = ":" Then
       NewDir = NewDir &amp; &quot;\&quot;
       NewDir = NewDir & "\"
   End If
   End If
   'Switches to the proper drive (the first letter in NewDir: 'Left'
   'Switches to the proper drive (the first letter in NewDir: 'Left'
Line 97: Line 97:
   ChDir NewDir                        'change to the directory
   ChDir NewDir                        'change to the directory
   'Display the name of the current directory so you know it worked.
   'Display the name of the current directory so you know it worked.
   MsgBox &quot;Current directory is &quot; &amp; CurDir()
   MsgBox "Current directory is " & CurDir()
End Sub
End Sub
'----------------------------------------------------------------------
'----------------------------------------------------------------------
                 </pre>
                 </pre>
The If-End If routine is the error-checking procedure. Whenever you use a ChDir statement, preceding it with the If-End If routine (or some variation of it) can help prevent the &quot;no backslash&quot; error.
The If-End If routine is the error-checking procedure. Whenever you use a ChDir statement, preceding it with the If-End If routine (or some variation of it) can help prevent the "no backslash" error.


</div>
</div>

Latest revision as of 11:25, 21 July 2020

Knowledge Base


ChDir May Fail When Changing to a Root Directory

Article ID: 108351

Article Last Modified on 6/23/2005



APPLIES TO

  • Microsoft Excel 97 Standard Edition



This article was previously published under Q108351

SUMMARY

In Microsoft Excel, the Visual Basic ChDir statement will fail if the path argument does not contain a backslash character (\). This requirement may cause problems if you use ChDir to change to a root directory, because the path of a root directory does not necessarily contain a backslash.

NOTE: When you use Microsoft Excel version 7.0 or later, you won't get an error message, but the directory won't change.

For example, the following Visual Basic code will fail

   ChDir "C:"
                

and you will receive the following error message:

Run-time error '76':
Path not found

The following lines of code will succeed, because they each contain a backslash:

   ChDir "C:\"
                

-or-


   ChDir "C:\EXCEL\LIBRARY"
                

It is possible to incorporate error-handling into a ChDir function to prevent an error from occurring. The Visual Basic code example below demonstrates one way to do this.

Visual Basic Code Example

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. This Visual Basic code example displays an input box which asks you which drive or directory you would like to switch to. When you enter a drive or directory, the example checks whether the path you enter contains a backslash and adds one, if needed, before switching to that drive or directory.

To run the example, position the insertion point in the line that reads "Sub MainMacro()" and press F5.

'----------------------------------------------------------------------
Option Explicit

Sub MainMacro()
   'Dimension some variables.
   Dim NewDir As Variant
   'Prompts you to enter a drive/directory. For example, D: or C:\EXCEL.
   'If you type in an invalid directory, the subroutine will fail. For
   'example, typing just "C" (without the quotes) is not going to work.
   NewDir = InputBox("Switch to which drive/directory?")
   'If the NewDir ends in a colon, indicating it is a root directory,
   'concatenate a backslash onto the end of it. For example, C: would
   'become C:\. If you actually enter "C:\", the subroutine doesn't add
   'another backslash.
   If Right(NewDir, 1) = ":" Then
      NewDir = NewDir & "\"
   End If
   'Switches to the proper drive (the first letter in NewDir: 'Left'
   'gives us this) and directory.
   ChDrive Left(NewDir, 1)             'change to the drive (C:, etc.)
   ChDir NewDir                        'change to the directory
   'Display the name of the current directory so you know it worked.
   MsgBox "Current directory is " & CurDir()
End Sub
'----------------------------------------------------------------------
                

The If-End If routine is the error-checking procedure. Whenever you use a ChDir statement, preceding it with the If-End If routine (or some variation of it) can help prevent the "no backslash" error.


Additional query words: 8.00 97 Run-time error 76 Path not found XL

Keywords: kberrmsg kbinfo kbprogramming KB108351