Consulting

Results 1 to 9 of 9

Thread: VB6 macro code for Word to save a document in a specified folder as a Word file

  1. #1
    VBAX Newbie
    Joined
    Dec 2017
    Location
    Sydney
    Posts
    5
    Location

    VB6 macro code for Word to save a document in a specified folder as a Word file

    I'm trying to save a document from a software program after running a macro in a specified folder as a Word file that is read only and not displayed on the screen. I would expect the following to work but it is saving as the default .inv file generated by the program rather than changing to a Word file. Is there a better way to save as a .doc file? Word is also not closing afterward, so I changed the close code and then added "ActiveWindow.Close" to the original section of the macro after calling Invoice but it still doesn't close.

    Sub Invoice()
    Dim NewPath As String
    NewPath = "m:\Invoices\"
    ActiveDocument.SaveAs FileName:=NewPath & ActiveDocument.Name, FileFormat:=wdFormatDocument, ReadOnlyRecommended:=True

    'ActiveDocument.Close
    ActiveDocument.ActiveWindow.Close

    End Sub
    Last edited by RajanSyd; 12-13-2017 at 10:25 PM.

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    So what does ActiveDocument.Name return?
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Newbie
    Joined
    Dec 2017
    Location
    Sydney
    Posts
    5
    Location
    Quote Originally Posted by macropod View Post
    So what does ActiveDocument.Name return?
    For invoice 220 it saves 220.inv but if I use the following code it displays a save as dialog with Word file selected as the document type. It seems to ignore that when I hide it, unless there is a better way to save it in a nominated folder as a Word file.
    ActiveDocument.SaveAs ActiveDocument.FullName, wdFormatDocument

    I tried ActiveDocument.SaveAs FileName:=NewPath & ActiveDocument.FullName, FileFormat:=wdFormatDocument, ReadOnlyRecommended:=True
    but get a message saying it isn't a valid file format
    Last edited by macropod; 12-13-2017 at 10:58 PM. Reason: posts merged

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    You didn't answer my question...
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    VBAX Newbie
    Joined
    Dec 2017
    Location
    Sydney
    Posts
    5
    Location
    220

    The number of the invoice being generated by the software
    Last edited by macropod; 12-13-2017 at 11:21 PM. Reason: merged posts

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    In that case, use:
    ActiveDocument.SaveAs2 FileName:=NewPath & ActiveDocument.Name & ".doc", FileFormat:=wdFormatDocument, ReadOnlyRecommended:=True, AddToRecentFiles:=False
    ActiveDocument.Close

    Kindly don't keep posting consecutive parts of your reply. We shouldn't have to keep merging them...
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    VBAX Newbie
    Joined
    Dec 2017
    Location
    Sydney
    Posts
    5
    Location
    thanks I'll try that in the morning.
    I haven't used this forum (or many others) before. Should I not click Reply to respond? or did I click one of the other reply options?

  8. #8
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by RajanSyd View Post
    Should I not click Reply to respond? or did I click one of the other reply options?
    Sure, but your last two posts (see above) were originally four posts. Surely you can do better than put one of two lines in each post? '220' and 'The number of the invoice being generated by the software', for example, really don't justify two separate posts...
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  9. #9
    VBAX Newbie
    Joined
    Dec 2017
    Location
    Sydney
    Posts
    5
    Location
    OK, sorry. I posted the first then thought it was inadequate and so I posted the second without thinking of just editing the first one. It seems that the software outputs the file as 220.inv so I was going to investigate trimming it and replacing the extension but I've now heard that the .inv extension will be OK for what we are trying to achieve. Thanks for your assistance.

Posting Permissions

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