Consulting

Results 1 to 17 of 17

Thread: Get File Name

  1. #1

    Get File Name

    What i want to do is to get the name of the word document in vba so that i can use the Right function to determine wether it is a dot or doc file and the dependent on the result to run a macro but cannot find the code to pull the file name.

    Does anyone know what it is?

    Thanks

  2. #2
    It's ok now

    It's ok now
     
    ActiveDocument.Name

  3. #3
    My next dilema is as follows.

    I have this code now which opens up the SaveAs dialogue if the file is a .dot, but in the dialogue, the 'Save As Type' field defaults to dot. Can i change this to default to doc?

    Also, is there a way to shut down the document if macros are not enabled? I'm thinking not as the macro couldn't run to check it in the first place but you never know.

    Dim DocumentName As String
    DocumentName = ActiveDocument.Name
    If Right(DocumentName, 3) = "dot" Then
    MsgBox "You must first save this file to your local/team drive and re-open it before completing"
    With Dialogs(wdDialogFileSaveAs)
      .Name = ""
      .Show
    End With
    ActiveDocument.Close
    Else
    End If
    Thanks

  4. #4
    Divad

    Also, is there a way to shut down the document if macros are not enabled? I'm thinking not as the macro couldn't run to check it in the first place but you never know.
    Correct, if the macros are not enabled then you could not get a macro to run.

    I have this code now which opens up the SaveAs dialogue if the file is a .dot, but in the dialogue, the 'Save As Type' field defaults to dot.
    I don't understand what you are trying to do here? Why would a user have opened the .dot and not created a new document?

  5. #5
    Is is opened via a web page and it opens as a dot file. It is fine if you open it straight from a drive.

    If it can't be done, i'll just put some extra wording in the dalogue box. If they save it as a dot file to their drive from the web page, they won't be able to open it as the dialogue will keep coming up until they save it as a doc file, and if they disable macro's, then the functionality won't work.

  6. #6
    I see now that some of what i am saying is pointless as when they save and then re-the dot file it will re-open as a doc file.

    Do they need to keep the dot file (it has toolbar macros in it) or can i delete it via a macro when they open it?

  7. #7
    I don't know how to do it. Hopefully someone else will respond. However, it is definitely possible to use a web page to create a new document from a dot file. This would be a better option than the solution you have asked for.

    If you open a dot file and then save it as a doc you will save multiple instances of all the code. This could cause you problems not only because of the increase in file size, but if you were to email the document externally it could get trapped by a firewall, also it could cause problems if you were to update the template as previously created documents would not incorporate the new code.

  8. #8
    Openening is as a doc file sraight from the web page would be perfect. Does anyone know?

    Thanks for the info on the multiple instances of code malarky. Didn't realise that.

  9. #9
    fionabolt

    What would be the impact if i renamed the dot file to doc and linked that from the web page. Would i loose my macros?

    The documnt has a macro button and also it is using the vba spell check as it is a protected document.

    Thanks

  10. #10
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    First of all, Document.Type will have a value of wdTypeDocument for a Document, and wdTypeTemplate for a Template and is a better indicator than the suffix (and easier to check).

    Next, it is unusual (for a User) to Open a Template itself - rather they open a New Document based on the Template.

    Now, and here is where I get out of my depth, I don't think you can create a Document from a Template inside a Browser And this may be why you are finding your Template itself open.

    The water is getting deeper When you open a Document from a Browser, whether it opens inside the Browser or in a separate instance of Word is a User setting.

    Finally, I would be very worried if VBA could run (without the User's very explicit consent, at least) inside a Browser.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  11. #11
    Tony

    When the file opens from the link it will automatically open the dialogue to save the file and then close it down (unless of course they disble macros), so they won't really be opening it from the browser as they will have to open the dot file from where they saved it. The link is on an internal web site.

    Why is the water getting deeper? Apart from global warming.

  12. #12
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Clearly I need to check this out. Are you saying that taking a link from a Browser to a Word Template can automatically run VBA code within that Template when it opens - code which could do absolutely anything?

    Apparently it's now called Climate Change <g> I'm just out of my depth because I so rarely browse to Documents.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  13. #13
    Yep. When they open the dot file via the intranet, as long as enable macro's, there is a macro called 'Private Sub Document_Open()' in the ThisDocument vba window that opens up the dialogue box.

    Thanks for your info earlier. I changed my macro to look like the following

    Private Sub Document_Open()
    If ActiveDocument.Type = wdTypeTemplate Then
    MsgBox "Please save this file to your local/team drive as a doc file and re-open it." & vbCrLf & _
            "To save it as a doc file, change the extension in the 'Save as type' field to 'Word Document (*.doc)'"
    With Dialogs(wdDialogFileSaveAs)
      .Name = ""
      .Type
      .Show
    End With
    ActiveDocument.Close
    Else
    End If
     
    End Sub
    The only thing know is that i want to change the SaveAs dialogue to default to saving as a doc file and not a dot file as the user has to change that part manually.

    I tried fiddling with the .Type bit but no luck. Any ideas?

  14. #14
    As previously discussed... I still think a better option would be to resolve the way in which your browser opens the template....

    It might be a good idea to start a new thread with an appropriate title asking this question?

    However, the following will force your save as dialog to save as a document and not a template.

    [vba]If ActiveDocument.SaveFormat <> wdFormatDocument Then
    ActiveDocument.SaveAs FileFormat:=wdFormatDocument
    Dialogs(wdDialogFileSaveAs).Show
    End If
    [/vba]

    Be very aware tho, the code will be contained in EVERY document created, and the resulting document will not be linked to the template.

  15. #15
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Try ...

    [VBA]With Dialogs(wdDialogFileSaveAs)
    .Name = ""
    .Format = 0
    .Show
    End With[/VBA]
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  16. #16
    Thank you for your help. I originally had the code in the 'ThisDocument' module as 'Document_Open' but when the SaveAs dialogue came up, it wouldn't save. I have moved it into another module and changed it to the following. It works great now.

    Sub AutoOpen()
    If ActiveDocument.Type = wdTypeTemplate Then
    MsgBox "To complete this form, save it to your local/team drive and then open it from there."
    With Dialogs(wdDialogFileSaveAs)
        .Name = ""
        .Format = 0
        .Show
    End With
    ActiveDocument.Close
    Else
    End If
     
    End Sub
    fiona. I don't have a problem with the code being in every document, but will there be any great advantages to having it linked to a template?

    When the dot file opens in Internet Explorer, if the template document is opened via the Intranet, how does it link to the template. I take it that it will stick a copy of the template on the c: drive in the Templates folder or something?

  17. #17

    SOLVED: Get File Name

    Doesn't matter now. People are having probs with the macros not working so i'll just put it on the web as a doc file. I've put a macro in place to find some hidden characters and if it finds them it deletes them and then prompts the user to save the file and then closes. When the user opens the form back up, the hidden characters are deleted so it just opens as normal.

    The main problem before was getting people to save the form rather than complete it when Internet Explorer opens, but this will solve that.

    Thanks all for your help.

Posting Permissions

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