VBA Express
Get File Name [Archive] - VBA Express Forum

PDA

View Full Version : Get File Name



Divad
09-20-2007, 01:18 AM
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

Divad
09-20-2007, 01:23 AM
It's ok now



It's ok now

ActiveDocument.Name

Divad
09-20-2007, 02:15 AM
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

fionabolt
09-20-2007, 02:38 AM
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?

Divad
09-20-2007, 02:43 AM
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.

Divad
09-20-2007, 02:51 AM
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?

fionabolt
09-20-2007, 02:52 AM
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.

Divad
09-20-2007, 04:16 AM
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.

Divad
09-20-2007, 04:31 AM
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

TonyJollans
09-21-2007, 04:40 AM
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.

Divad
09-21-2007, 05:30 AM
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.

TonyJollans
09-21-2007, 06:15 AM
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.

Divad
09-21-2007, 06:21 AM
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?

fionabolt
09-21-2007, 07:47 AM
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.

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


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

TonyJollans
09-21-2007, 08:39 AM
Try ...

With Dialogs(wdDialogFileSaveAs)
.Name = ""
.Format = 0
.Show
End With

Divad
09-23-2007, 11:26 AM
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?

Divad
09-25-2007, 05:16 AM
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.