Solved

Specifing Format prevents Save As dialog from returning full path

Posted on 2004-10-04
16
1,345 Views
Last Modified: 2012-06-27
I have a button on a form that I would like to display the SaveAs dialog from which the user can specify a file name and location to save a document in. I would like to be able to grab the full path of the desired location and desired name of the file from the dialog. Here is what I have so far...

    Dim dlgSave As Dialog
    Dim strSavePath As String
   
    Set dlgSave = Dialogs(wdDialogFileSaveAs)

    With dlgSave
        .Display
        strSavePath = .Name
    End With
   
    MsgBox strSavePath

In this case the dialog displays, the user is able to select the location and file name and when they click save the message box displays the full path and file name that the user has chosen. However, when I try to specify the Format as ‘document’ like so...

    Dim dlgSave As Dialog
    Dim strSavePath As String
   
    Set dlgSave = Dialogs(wdDialogFileSaveAs)

    With dlgSave
        .Format = 0   ' <-------------------- New line here
        .Display
        strSavePath = .Name
    End With
   
    MsgBox strSavePath

The message box only displays the chosen file name. No path. I need the path. What am I doing wrong? Any ideas?

Edit: I am using Word 2002.

Thanks,
Aaron
0
Comment
Question by:aaronrelyea
  • 6
  • 3
  • 2
  • +2
16 Comments
 
LVL 9

Expert Comment

by:gilbar
Comment Utility
the curDir function shows the current directory ( where the dialog box is pointing)
so
MsgBox CurDir & "\" & strSavePath
should give you the full path
0
 
LVL 9

Expert Comment

by:gilbar
Comment Utility
my question is : why is your first version showing the full path? it shouldn't ( and doesn't for me). Both should be displaying just the document name, as you say the second version is.  Anyway, the curDir function should fix you up
0
 
LVL 15

Expert Comment

by:Colosseo
Comment Utility
Hi

tried your code and both give me just the result of doc1.doc for .name

but in order to get the full path and name you can use FileNameInfo

so using your second example with the format specified you need to replace

strSavePath = .Name

with

strSavePath = WordBasic.FilenameInfo$(Filename$, 1)

A guy at work had the same problem and in trying to resolve it we found a good reference for this at

http://word.mvps.org/FAQs/MacrosVBA/WordBasicCommands.htm

you will find an explanation of the other filetype options you can specify

HTH

Scott
0
 
LVL 15

Expert Comment

by:Colosseo
Comment Utility
Hi again

sorry for your code that should be

strSavePath = WordBasic.FilenameInfo$(.Name, 1)

that'll teach me to cut and paste rather than type :)

Scott
0
 
LVL 20

Expert Comment

by:Debsyl99
Comment Utility
sorry i am a boring geek that just lost my partner through constantly e mailing strangers
0
 

Author Comment

by:aaronrelyea
Comment Utility
Ok, I just got home from work, which was where I was while writing the previous post as someone was speaking to me as I was being rushed to leave. I am sorry. That last post is an abomination and unreadable. Please accept this substitute...

I agree that .Name shouldn't return the full path, but it does. I just tried again, using the exact code I describe above and I still get the full path, however when I run it on a new template with no code .Name works properly (it returns just the name of the file not the path) so something is going on at my end I'm guessing and I will try and track it down and explain it here if anyone is interested.

Regardless, I think that is what screwed me up and sent me down this path. I know about curdir and I initially thought .Name would only return the file name but when I saw that it returned the full path and file name I figured I had misunderstood .Name in the first place (misunderstood that .name should only return the file name, that is) so I couldn't understand why it wouldn't continue to do so if I set .Format = 0.

I don't know why the point value was set at 500 for this question, I had intended for it to be 250. My goof and of no consequences anyway. I will be happy to award the points to you but I would like to give this question a little more time to see if anyone has any ideas regarding the .Name returning the full path. And more points for anyone that does.

(If I can figure out how to edit or remove my other post I will.)

Thanks for your help.
0
 
LVL 20

Expert Comment

by:Debsyl99
Comment Utility
Hi

Right on with your assessment although am glad it made you laugh, - my partner is a naughty little sod and he did indeed post that last comment - all because I am sick of watching miserable soaps and would rather post on EE instead! Sadly though, I am a network admin and not a word expert and so cannot assist with your issue. If you have any server/client problems I'm your girl! - otherwise am currently seeking partner who is into IT ;-))

Best wishes and sorry for the distraction,

Deb :))



0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:aaronrelyea
Comment Utility
I have a feeling this is going to end in tears but here goes anyway. I still get differing returns from .Name depending on how the ‘Save As’ dialog is used. If anyone would like to follow along with me I wouldn’t mind an explanation as to what’s going on.

It looks to me like .Name returns either the full path or the file name depending on different situations (revolving around the current directory and if that differs from the current location of the file or the templates directory or the current file location or something.) I am not familiar enough with Word to know when various settings are modified so I am having difficulty nailing it down. If anyone could help I would really like to know what is going on. For example try this…

Start out with a clean document with no code, add a form with a button and added the following code for the button:

    Dim strPath As String
    Dim dlgSave As Dialog
   
    MsgBox "Pre Diag" & Chr(10) & "CurDir: " & CurDir
    Set dlgSave = Dialogs(wdDialogFileSaveAs)
    With dlgSave
        .Display
        strPath = .Name
    End With
    MsgBox "Post Diag" & Chr(10) & ".Name: " & strPath & Chr(10) & _
"CurDir: " & CurDir

Click on the button repeatedly going through the following permutations each time (might look long and boring but its pretty quick and, well, boring):

1) ‘Save as Type’ Document, Don’t change the file location, and click ‘Save’
2) ‘Save as Type’ Document, Don’t change the file location, and click ‘Cancel’
3) ‘Save as Type’ Document, Don’t change the file location, and click the ‘x’ (close button)

4) ‘Save as Type’ Template, Don’t change the file location, and click ‘Save’
5) ‘Save as Type’ Template, Don’t change the file location, and click ‘Cancel’
6) ‘Save as Type’ Template, Don’t change the file location, and click the ‘x’ (close button)

7) ‘Save as Type’ Document, Change the file location, and click ‘Save’
8) ‘Save as Type’ Document, Change the file location, and click ‘Cancel’
9) ‘Save as Type’ Document, Change the file location, and click the ‘x’ (close button)

10) ‘Save as Type’ Template, Change the file location, and click ‘Save’
11) ‘Save as Type’ Template, Change the file location, and click ‘Cancel’
12) ‘Save as Type’ Template, Change the file location, and click the ‘x’ (close button)

Do you get different .Name results depending on which options you choose? I do. The first several seem to be consistent until I start mixing up the order in which I run various permutations. What is really getting me is when I go through the exact process I describe above but do step 5 twice in a row I get different responses each time. I think I see a bit of a pattern going on here (depending on the current curdir and the options selected in the dialog) but like I said I don’t know enough about how word operates to help me predict what .Name will return. I have tried this on three PCs in the office so I don’t think it is specific to me. Make sense to anyone? If so, why?

Ok, go ahead and let me have it.

Aaron
0
 

Author Comment

by:aaronrelyea
Comment Utility
Regarding Colosseo's comments:

Thanks for the suggestion but unfortunately the decision from on high is that I am NOT to use the WordBasic Object. They are concerned with MS dropping it eventually or something to that degree.

Thanks again however,
Aaron
0
 
LVL 15

Assisted Solution

by:Colosseo
Colosseo earned 250 total points
Comment Utility
Hi Aaron

Fair enough about the WordBasic Object :)

Had a try at your suggestions above and when i got to 5 i got just the filename the first time but the full path and filename the second... weird

But I was thinking about what you said for predicting what .name returns and that shouldn't be required we can just check.

Filenames cannot contain the \ character so in the following code we still use .Name then we use the instr function to see if the name has a \ character in it.

If it doesn't then we know that we only have the filename so we can concatenate on the curdir and an extra \ to give us the full path and filename.

If it does then we dont have to do anything cause we already have the full path and filename

    Dim dlgSave As Dialog
    Dim strSavePath As String

    Set dlgSave = Dialogs(wdDialogFileSaveAs)

    With dlgSave
        .Format = 0
        .Display
        strSavePath = .Name
    End With

    If InStr(strSavePath, "\") = 0 Then strSavePath = CurDir & "\" & strSavePath
   
    MsgBox strSavePath

Cheers

Scott
0
 

Author Comment

by:aaronrelyea
Comment Utility
We are on the same page here.

As a matter of fact I have already done something similar in this project. (I have wasted too much time on this hiccup already so I have decided to just workaround it for now.) I don't know if you tried entering file names with spaces or not but that adds another dimension to the scenarios described above. File names with spaces can cause .Name to return values in quotes in various circumstances. Again, not a killer but curious.

Altogether, not a huge issue but it bugs me and I would like to know what is going on so I will look into it on my own later tonight and tomorrow. Besides the fact that it bugs me I am curious and I don't want this workaround to cause issues in the future because I will be leaving this office in a month or so. (As I'm sure you know small issues today can be a bear tomorrow.) Just want to get this thing as tight as possible.

Thanks for your help. And a special thank you for validating the discrepancy. I was beginning to think I was bonkers.

Aaron
0
 
LVL 37

Expert Comment

by:Joanne M. Orzech
Comment Utility
I wrote a macro which is not available right now.. but will be tomorrow.  It specifies the drive, the folder, if the folder does not exist it creates it, and then gives the document a name.  Would that be helpful?
0
 

Author Comment

by:aaronrelyea
Comment Utility
Yeah, I would like to take a look at that. Thank you,

Aaron
0
 
LVL 37

Accepted Solution

by:
Joanne M. Orzech earned 250 total points
Comment Utility
Here it is Aaron:

Private Sub CommandButton1_Click()
    ChangeFileOpenDirectory "F:\USERS\WILLS\"
    DocName = TextBox1.Text
    ClientName = TextBox2.Text
    AttyName = ComboBox1.Text
    CurrentDate = Format(Date, "mmddyy")
    sRootPath = "F:\USERS\WILLS\"
    ChDir (sRootPath & AttyName)
    If Dir(ClientName, vbDirectory) = Found Then
    temp = MsgBox("Directory does not exist.  Create directory?", vbYesNo + vbQuestion)
        If temp = 6 Then
            MkDir (ClientName)
            activedocument.SaveAs FileName:="F:\USERS\WILLS\" & AttyName & "\" & ClientName & "\" & DocName & CurrentDate & "-will.doc"
            temp1 = MsgBox("The Will has been saved with the current date appended to the file name.", vbOKOnly + vbInformation)
        End If
    Else
        activedocument.SaveAs FileName:="F:\USERS\WILLS\" & AttyName & "\" & ClientName & "\" & DocName & CurrentDate & "-will.doc"
        temp2 = MsgBox("The Will has been saved with the current date appended to the file name.", vbOKOnly + vbInformation)
    End If
Application.ScreenUpdating = True
frmSaveWill.Hide
End Sub
0
 

Author Comment

by:aaronrelyea
Comment Utility
I have split the points as indicated above. I still haven't found and answer to the actuall issue but it doesn't seem it is worth the trouble. I did develop a workaround similiar to the ones described above. Thanks to everyone who responded.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Word Mac 2011: changing the language assignment 2 54
add bookmark label to 78 documents 2 50
PDF to Word 25 103
Mail Merge/EMail 10 53
A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now