Results 1 to 11 of 11

Thread: [RESOLVED] Open word document from one directory and set SaveAs in a diffent location

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    350

    Resolved [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.

  2. #2
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    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

    Name:  Untitled.jpg
Views: 1336
Size:  67.3 KB

    put it in the before_save event and set cancel = true

    should do the trick.
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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
    Last edited by westconn1; Mar 1st, 2013 at 06:37 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    350

    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

  5. #5
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    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.
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Open word document from one directory and set SaveAs in a diffent location

    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?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    350

    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.
    Last edited by DavidGraham167; Mar 4th, 2013 at 04:25 AM.

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    350

    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?

  10. #10
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2010
    Posts
    350

    Re: Open word document from one directory and set SaveAs in a diffent location

    Thanks, that has fixed it.

Posting Permissions

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



Click Here to Expand Forum to Full Width