+ Reply to Thread
Results 1 to 4 of 4

Using FileDialogue.show / Returning as a string in Word VBA

  1. #1
    Registered User
    Join Date
    12-08-2017
    Location
    Minneapolis, MN
    MS-Off Ver
    Standard 2013
    Posts
    83

    Using FileDialogue.show / Returning as a string in Word VBA

    I love learning VBA and would gladly spend hours trying to figure this out, but don't have that much time. Keep in mind this is Word VBA not excel but it should be similar enough I ***

    The following code works perfectly at extracting a page in word and saving it into a static directory. The problem is I want the user to be able to dynamically choose the directory with a FileDialogue object. I keep running into issues/errors.

    This code works but is static.


    Please Login or Register  to view this content.
    I can't remember whom to credit for that code but I didn't write it.

    Now I try to use FileDialogue.show() method to pick a directory and in some of the attempts below I did get that box to appear.

    I first tried changing
    Please Login or Register  to view this content.
    and nothing else, but that gave a runtime error 5152 This is not a valid filename. I assume because it's not returning a string. In fact I had it printed to a messagebox and it returns a -1. (Huh? Why would it return a mere code and not something more substantive?)

    and I tried adding

    Please Login or Register  to view this content.
    I tried different values as argument including -1)


    That gave a runtime error 424 Object required. I read the docs on that so I tried using dim fldr as FileDialogue and then
    set fldr = Application.FileDialog(msoFileDialogFolderPicker).Show

    but that got a compile error object required.

    what does Application.FileDialogue.show() actually return? One might assume it returns a string, or a FileDialogue object that can be converted to a string.
    I read the docs and I searched google and S/O. It seems people had similar questions and got answers but they weren't clear enough for me and I'm not sure they addressed the issue. If I could spend a day at this I'm sure I could get it and learn a lot in the process. The lightbulb is getting brighter inside of me every day but it's a slow process. Thanks. I just wanted to put this out there before I shut down for the day so I don't forget what I did.
    Last edited by 6StringJazzer; 05-06-2019 at 08:47 PM. Reason: Moved from Excel Programming / VBA / Macros

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,532

    Re: Using FileDialogue.show / Returning as a string in Word VBA

    Here is the documentation for the methods and properties.

    Show just returns a flag telling whether the user pressed the Select button or the Cancel button. Your first version didn't work because the flag value was being assigned to strFolder. The second version didn't work because the flag value was being assigned to fldr.

    Here is a simple version that should work in your situation. You can make it fancier with a default path, prompt, title, custom button name, etc.
    Please Login or Register  to view this content.
    Just for completeness, what you were trying to do with your second version would be done like this, although I recommend what I did above.
    Please Login or Register  to view this content.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    12-08-2017
    Location
    Minneapolis, MN
    MS-Off Ver
    Standard 2013
    Posts
    83

    Re: Using FileDialogue.show / Returning as a string in Word VBA

    Yee Haw, it works and I learned a lot in the process.

    Thank you. This makes more sense now. I saw the code you gave with the loop yesterday but it didn't make as much sense to me as it does now.

    It was strange that I couldn't access the Selecteditems method outside of such a loop (or I didn't figure out how to) because as you say, the flag value was being assigned to the variable, so it's no longer a complete object I guess (just a one dimensional string). I guess the only way to access the property we want, or at least the most straightforward, is with such a loop to help us get inside the object. As long as I have a working theory on what's going on, I'm pretty happy. I'm taking a C++ course Unreal Engine in the evenings and weekends and we're learning about references and pointers. We kind of bypassed needing to worry about those. I guess we used it with the set statement (VBA's version of a pointer/reference), which is what I thought was the issue, but it's cool I can reinforce and learn this at my day job.

    Thanks for your help

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,532

    Re: Using FileDialogue.show / Returning as a string in Word VBA

    There is no loop. Maybe you are thinking of the With.

    When dealing with this particular object, you have two choices:

    1. Declare an Object and then Set that object to be an Application.FileDialog object. That is what you really wanted to do with flder (but didn't quite get there).

    2. What I demonstrated is what's called an anonymous object. There is no named Application.FileDialog object. The With statement creates the object and you may reference its methods and properties within the With scope by using dot notation. After the End With, it is implicitly destructed. This type of thing is found in object-oriented languages, and VBA is an object-oriented-ish language.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Grab last word from string and then delete word from original string.
    By mtilbury in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 09-14-2015, 04:14 AM
  2. Replies: 1
    Last Post: 09-11-2014, 10:59 AM
  3. [SOLVED] Find word in text string and return word adjacent to the left
    By eadamquinn in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 05-06-2014, 03:22 PM
  4. Replies: 2
    Last Post: 08-05-2013, 04:45 PM
  5. find how many times a word appears in a string at certain positions within the string
    By jordan2322 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-05-2013, 12:05 AM
  6. [SOLVED] How to conditionally insert text string into a line after first word using Word 2010
    By staggers47 in forum Word Formatting & General
    Replies: 5
    Last Post: 12-08-2012, 11:07 AM
  7. Replies: 3
    Last Post: 04-17-2010, 05:02 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1