Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>AFAIK there is no pure VBA solution to override the original behaviour. You can use an alternative from Robert Mearns answer but it doesn't show the windows form so it's less customizable.</p> <p>Follow this answer if you want to achieve the exact effect - FileOpenDialog.</p> <p>You can <a href="https://stackoverflow.com/questions/935160/environ-function-code-samples-for-vba/937900#937900">print all the environmental variables using the Environ$() function</a>. This will not show any variable directly pointing to <strong>MyComputer</strong> therefore you can't pass it to the <code>.InitialFileName</code> property.</p> <p><strong>MyComputer</strong> is not a <em>physical location</em> that you can access through <em>cmd</em>. I think of it as an abstract Interface and it's quite difficult to explain how VBA and <code>.InitialFileName</code> uses a string to access a location. </p> <p>Well, the only workaround the problem I can think of it's to use an external library written in for example C# that can access the <strong>MyComputer</strong>.</p> <p><strong>It's easier than it sounds!</strong></p> <h1>Follow the below steps to create your Custom OpenFileDialog.</h1> <p>You need a <a href="http://www.microsoft.com/visualstudio/eng/downloads" rel="nofollow noreferrer">Visual Studio Express For Desktop</a> - it's free to download and use. </p> <p>After installation - run as <code>Administrator</code>! (<em>it's necessary for the libraries to get registered</em>)</p> <p>Select <code>File</code> and <code>New Project</code>. Rename it to <code>CustomOFD</code> and and hit the <code>OK</code>.</p> <p><img src="https://i.stack.imgur.com/7zuFJ.png" alt="enter image description here"></p> <p>Right-click the <code>CustomOFD</code> Project in the Solution Explorer and Select <code>Add References</code></p> <p>Add references to the <code>System.Windows.Forms</code> as shown in the below img</p> <p><img src="https://i.stack.imgur.com/hLZks.png" alt="enter image description here"></p> <p>Right-click <em><code>Class1.cs</code></em> in the Solution Explorer and rename it to <code>CustomOFD.cs</code>.</p> <p>Double click your <code>CustomOFD</code> and replace the code with the one from below</p> <pre><code>using System; using System.Runtime.InteropServices; using System.Threading.Tasks; using System.Windows.Forms; namespace CustomOpenFileDialog { [InterfaceType(ComInterfaceType.InterfaceIsDual), Guid("541EDD34-4CDC-4991-82E9-6FC23F904B5B")] public interface ICustomOFD { DialogResult ShowDialog(); string FileName(); } [ClassInterface(ClassInterfaceType.None)] [Guid("E33102F0-B3C0-441C-8E7A-B9D4155A0D91")] public class CustomOFD : ICustomOFD { private OpenFileDialog box = new OpenFileDialog(); public CustomOFD() { box.Multiselect = false; box.Title = "Select file"; box.InitialDirectory = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"; } public DialogResult ShowDialog() { return box.ShowDialog(); } public string FileName() { return box.FileName; } } } </code></pre> <p><em>Note: you can generate a new GUID for your own class using the <code>Tools</code> => <code>Create GUID</code> and replace it with your own, if you wanted to...</em></p> <p>Right-click the <code>CustomFileOpenDialog</code> in the Solution Explorer and select <code>Properties</code></p> <p><img src="https://i.stack.imgur.com/RtleU.png" alt="enter image description here"></p> <p>In the Properties window go to Application tab and click <code>Assembly Info</code> and tick the <code>Make COM-Visible</code> box</p> <p><img src="https://i.stack.imgur.com/eIb3r.png" alt="enter image description here"></p> <p>Then go to the <code>Build</code> tab and tick <em>Register for COM interop</em></p> <p><img src="https://i.stack.imgur.com/SCw7R.png" alt="enter image description here"></p> <p>Right-click the project and select <code>Build</code> from the menu</p> <p>Now look in the Output tab as it shows you where the library was compiled to</p> <p>usually its</p> <pre><code>c:\users\administrator\documents\visual studio 2012\Projects\CustomOpenFileDialog\CustomOpenFileDialog\bin\Debug\CustomOpenFileDialog.dll </code></pre> <p>Ok. Now save and close VS.</p> <p>Open Excel and go into VBE <kbd>ALT</kbd>+<kbd>F11</kbd> and insert a standard module</p> <p>Click <code>Tools</code> on the menu bar and select <code>References</code></p> <p>Click the <code>Browse</code> button and navigate to the <code>CustomOpenFileDialog.tlb</code> file and click OK add to the list of references</p> <p>Copy paste the code for module</p> <pre><code>Option Explicit Sub Main() Dim ofd As New CustomOFD Set ofd = New CustomOFD ofd.ShowDialog Debug.Print ofd.Filename End Sub </code></pre> <p><img src="https://i.stack.imgur.com/5QCeh.png" alt="enter image description here"></p> <hr> <p>finally, run the sub and enjoy the computer as the default location for the customized OpenFileDialog box!</p> <p><img src="https://i.stack.imgur.com/DTvGp.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload