[RESOLVED] Open word document from one directory and set SaveAs in a diffent location
I am opening a Word template using the code below.
'file' contains the location and file name of the template.
MyWord = CreateObject("Word.Application")
MyWord.Visible = True
MyWord.Documents.Open(file)
I then fill the word document with the required data and the user either prints the document or saves it.
If he/she saves it, then default directory is where it was opened and it will overwrite the file as the name is still .dot.
How do I make it so that it is saved in a predifined directory with a .doc extension?
You could suggest copying & renaming the file to the required destination first, but if the user just wants to print the file you do not want it copied.
Thanks.
1 Attachment(s)
Re: Open word document from one directory and set SaveAs in a diffent location
Hi,
application.activedocument.saveas()
just follow the tooltip for the information it requires
Attachment 97173
put it in the before_save event and set cancel = true
should do the trick.
Re: Open word document from one directory and set SaveAs in a diffent location
the correct way to do it is to create a new document based on the template, unless you want to edit the template
Code:
Dim d As Document
Set d = Documents.Add(tfile) ' where tfile is a template path\filename
' other editing
d.SaveAs Path\FileName
Re: Open word document from one directory and set SaveAs in a diffent location
My code is now like this:
MyWord = CreateObject("Word.Application")
MyWord.Visible = True
Dim NewDoc As Word.Document
NewDoc = MyWord.Documents.Add(file)
But I get an error Bad Image Format Exception was unhandled.
Could not load file or assemply Interop Word Version 8.2.0.0.
I have:
Public MyWord
and reference MSWord 10.0 Object Library v 8.2.0.0
Re: Open word document from one directory and set SaveAs in a diffent location
i think your going to have to reference 'visual studio tools for office,
in references menu under .net look for microsoft.office.interop.word.
Re: Open word document from one directory and set SaveAs in a diffent location
Quote:
But I get an error Bad Image Format Exception was unhandled.
Could not load file or assemply Interop Word Version 8.2.0.0.
on which line?
Re: Open word document from one directory and set SaveAs in a diffent location
Thanks, I added microsoft.office.interop.word and now got it compiling with no errors.
I now have the code:
MyWord = CreateObject("Word.Application")
MyWord.Visible = True
Dim NewDoc As Document
NewDoc = MyWord.Documents.Add(file) ' where file is the template location
With MyWord
' fill document with required data
End With
NewDoc.SaveAs(FileName:=JobFolder & "\" & JobNo & " Jif 1 page.doc", FileFormat:=0) ' JobFolder is the required location
But it is saving the file - I only want to save it if the user selects 'File|SaveAs' from Word.
Re: Open word document from one directory and set SaveAs in a diffent location
well remove the last line
as i is a new document save will default to saveAs
Re: Open word document from one directory and set SaveAs in a diffent location
Thanks,
If I remove the last line and in Word, if I select SaveAs, the default location is in the MyDocuments directory.
How do I change this default location?
Re: Open word document from one directory and set SaveAs in a diffent location
try changing the default path
Application.Options.DefaultFilePath(wdCurrentFolderPath) = "C:\somepath"
but as this is for all documents, you should change back afterwards
safer to just force saveAs
Re: Open word document from one directory and set SaveAs in a diffent location
Thanks, that has fixed it.