locked
VBA Macro in Word 2010 RRS feed

  • Question

  • Hi

    Can someone please help with this very frustrating situation.  I have googled for days, read pages from books, tried all types of alternatives, but no joy.

    Basically can anyone tell me the vba code I need to use.

    When I open an existing word document, I then want to run a macro that will save it as a new_name and in a different location?


    When ever I attempt this my code shows the File Save window, with it's original name in place and wanting to save to it's original location.

    How can I stop this window from appearing?

    Below are just two examples of my attempts. The second attempt shows me trying to use variables for the path name and the document name.

            

    'Save the OPENfile attempt 01

    ActiveDocument.SaveAs ("C:\Users\Mike\Desktop\mike.docx")

    ----

    'Save the OPENfile  attempt 02

    Dim newLocationPath As String
    Dim newDocumentName As String

    newLocationPath = "C:\Users\Mike\Desktop\"
    newDocumentName = "mike.docx"

    ActiveDocument.SaveAs FileName:=newLocationPath & newDocumentName, FileFormat:=wdFormatDocument
    ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges




    • Edited by Mikee S Tuesday, April 19, 2016 7:14 PM
    Tuesday, April 19, 2016 2:34 PM

Answers

  • Hi Mogulman

    You very much put me into the correct path. I have solved this crazy situation at last.

    It is indeed a word Addin - It is the abDocs Word Addin that is the culprit.

    abDocs is a software program developed by Acer. So this seems to be something that only effects Acer Owners.

    Thank you for your help

    • Marked as answer by Mikee S Friday, April 22, 2016 8:22 PM
    Friday, April 22, 2016 8:20 PM
  • Thank you for your help, I have solved this crazy situation at last.

    It is the abDocs Word Addin that is the culprit.

    abDocs is a software program developed by Acer. So this seems to be something that only effects Acer Owners.



    Thank you again for your help
    • Marked as answer by Mikee S Friday, April 22, 2016 8:21 PM
    Friday, April 22, 2016 8:21 PM
  • Thank you for your help, I have solved this crazy situation at last.

    It is the abDocs Word Addin that is the culprit.

    abDocs is a software program developed by Acer. So this seems to be something that only effects Acer Owners.



    Thank you again for your help
    • Marked as answer by Mikee S Friday, April 22, 2016 8:21 PM
    Friday, April 22, 2016 8:21 PM

All replies

  • I don't have Word on this PC but I do something like this:

    rptDoc.SaveAs fileName:=fullFN, _
              FileFormat:=wdFormatDocumentDefault, AddToRecentFiles:=False

    Replace rptDoc with ActiveDocument.


    fullFN is the fully qualified path with ".docx" extension.
    • Edited by mogulman52 Tuesday, April 19, 2016 3:13 PM
    Tuesday, April 19, 2016 3:10 PM
  • Mike, I'm afraid I didn't understand your question.

    Could you explain it clearer?


    http://www.ambienteoffice.com.br - http://www.clarian.com.br

    Tuesday, April 19, 2016 7:07 PM
  • Dear Gualberto

    I have adjusted my wording, I hope this helps.

    Tuesday, April 19, 2016 7:15 PM
  • Dear mogulman52

    I have tried your code, but NO JOY!

    Tuesday, April 19, 2016 7:17 PM
  •     With Application.FileDialog(msoFileDialogSaveAs)
            .InitialFileName = "c:\your\path\here\"
            .Show
        End With
    


    http://www.ambienteoffice.com.br - http://www.clarian.com.br

    Tuesday, April 19, 2016 8:38 PM
  • Dear Gualberto

    Thank you for your reply. Your code achieves 99% of what I would like to do.

    Now I just need the code to automatically click the save button on the file save window. 

    (I do not want to see the file save window)

    Wednesday, April 20, 2016 6:10 AM
  • What? So, why don't you use:

    ThisDocument.SaveAs2 "c:\temp\mydocument.docm"


    http://www.ambienteoffice.com.br - http://www.clarian.com.br

    Wednesday, April 20, 2016 8:49 AM
  • Hi Gualberto

    Your new suggestion, opens the File Save window and shows the intended location as C:\Users\Mike\AppData\Roaming\Microsoft\Templates

    and with the intended filename as  Normal.docx.

    This is what I wrote : - ThisDocument.SaveAs2 "C:\Users\Mike\Desktop\mikeeeeee.docx"

    I have also tried : ActiveDocument.SaveAs2 "C:\Users\Mike\Desktop\mikeeeeee.docx"

    Which again leaves me with a File Save window with the intended location being the same as the original. And the intended filename the same as original.

    Your first suggestion is by far the closest

    Wednesday, April 20, 2016 10:57 AM
  • Your dilemma revolves around the difference between ActiveDocument and ThisDocument. This document is the template containing the code. ActiveDocument is the document that is currently active i.e. your newly opened document.

    If you add the following macro to your template

    Sub AutoOpen()
        ActiveDocument.SaveAs2 "C:\Users\Mike\Desktop\mikeeeeee.docx"
    lbl_Exit:
        Exit Sub
    End Sub

    The current document will be saved without displaying a dialog, provided the document you open is of the same type as the original document. Otherwise you must add the format to your command.

    Your problem is that you are running the code from the template with the template as the active document and not opening a document.

    Note that if you put this code in the normal template, every document you open will be saved with that name, which I doubt is what you require.


    Graham Mayor - Word MVP
    www.gmayor.com



    Wednesday, April 20, 2016 11:23 AM
  • Hi Graham

    Thank you for your response, I am finding this situation extremely frustrating.

    All I want to do is open an existing word document (not template) and then save the file with a new file name and in a new location. I would very much like the file save window not to appear however if it is the case that the window has to appear then at the very least, the intended location path should be in place and also the intended new name for the file should be in place. So that I can then just click the save button.

    As a newbie I have spent countless hours in a bid to try and solve this on my own in an attempt to hopefully not ask something so simple. But 99% of my efforts leave me with a file save window open where the intended location is the original file location and the intended filename is the original filename.

    Please help
    Wednesday, April 20, 2016 12:09 PM
  • If you do as I indicated in my previous reply, the macro will run whenever a document (any document)  is opened and the dialog is not shown. Any existing document of the same name in the save location will be overwritten.

    If you wish to restrict it to a particular document, you need to qualify that in the macro e.g.

    Sub AutoOpen()
        If ActiveDocument.FullName = "C:\Path\Name of Document.docx" Then
            ActiveDocument.SaveAs2 "C:\Users\Mike\Desktop\mikeeeeee.docx"
        End If
    lbl_Exit:
        Exit Sub
    End Sub

    where Path is the place where the document you are opening is stored and Name of Document should be self explanatory. The macro

    This sounds an odd thing to do. What is the real aim of the exercise?


    Graham Mayor - Word MVP
    www.gmayor.com

    Wednesday, April 20, 2016 1:25 PM
  • I still didn't understand why the following statement isn't suitable for you:

    ActiveDocument.SaveAs2 "C:\your_new_path\mike.docx"
    
    This statement won't open the file dialog window, it'll save the file directly.


    http://www.ambienteoffice.com.br - http://www.clarian.com.br

    Wednesday, April 20, 2016 2:40 PM
  • Hi Gualberto

    I totally agree with you. I too cannot understand why ActiveDocument.SaveAs2 "C:\your_new_path\mike.docx" does not work for me. I am using word 2010 and Windows 8. Could it be a setting within Word?

    I have now created two new folders on my desktop
    Folder 1 is called Original Folder
    Folder 2 is called Intended Location
    I have placed a word document into Folder 1
    I then open the word document and run the macro below

    You will see from my screenshot that it will not save to where intended or change the filename

    Both of my attempts below give me the same screenshot.

    What is going on?



    Sub PleaseSaveAsANewFile ()
    Dim newDirectoryPath As String
    Dim theNewFileName As String

    newdirectoryPath = "C:\Users\Mike\Desktop\Intended Location"
    theNewFileName = "newFilename.doc"

    ActiveDocument.SaveAs2 "C:\Users\Mike\Desktop\Intended Location\newFileName.doc"

    ActiveDocument.SaveAs2 newdirectoryPath & theNewFileName
    End Sub  
    Wednesday, April 20, 2016 4:04 PM
  • Can you tell me how to verify my account so that i can send a screenshot
    • Edited by Mikee S Wednesday, April 20, 2016 4:08 PM
    Wednesday, April 20, 2016 4:08 PM
  • Hi Graham

    I cannot understand why ActiveDocument.SaveAs2 "C:\your_new_path\mike.docx" does not work for me. I am using word 2010 and Windows 8. Could it be a setting within Word?

    I have now created two new folders on my desktop
    Folder 1 is called Original Folder
    Folder 2 is called Intended Location
    I have placed a word document into Folder 1
    I then open the word document and run the macro below

    I am unable to upload a screenshot (I think because I am new to this) But it will show you in the File Save window that it will not save to where intended or change the filename

    Both of my attempts below give me the same screenshot.

    What is going on?




    PleaseSaveAsANewFile ()
    Dim newDirectoryPath As String
    Dim theNewFileName As String

    newdirectoryPath = "C:\Users\Mike\Desktop\Intended Location"
    theNewFileName = "newFilename.doc"

    ActiveDocument.SaveAs2 "C:\Users\Mike\Desktop\Intended Location\newFileName.doc"

    ActiveDocument.SaveAs2 newdirectoryPath & theNewFileName
    End Sub  

    Wednesday, April 20, 2016 4:14 PM
  • Another suggestion:

        Application.DisplayAlerts = wdAlertsNone
        ActiveDocument.SaveAs2 "c:\path\file.docx"
        Application.DisplayAlerts = wdAlertsAll


    http://www.ambienteoffice.com.br - http://www.clarian.com.br


    Wednesday, April 20, 2016 4:24 PM
  • Hi Gualberto

    It opens the window but still it wants to save to the original location and with the original filename.

    My code

    Sub tryagain()
    '
    Application.DisplayAlerts = wdAlertsNone
        ActiveDocument.SaveAs2 "C:\Users\Mike\Desktop\Intended Location\newFileName.doc"
        Application.DisplayAlerts = wdAlertsAll
    End Sub
    Wednesday, April 20, 2016 4:30 PM
  • The code

    Sub PleaseSaveAsANewFile ()
    Dim newDirectoryPath As String
    Dim theNewFileName As String

    newdirectoryPath = "C:\Users\Mike\Desktop\Intended Location"
    theNewFileName = "newFilename.doc"

    ActiveDocument.SaveAs2 "C:\Users\Mike\Desktop\Intended Location\newFileName.doc"

    ActiveDocument.SaveAs2 newdirectoryPath & theNewFileName
    End Sub 

    will not work correctly for two reasons.

    1. You have not put the backslash at the end of the line

    newdirectoryPath = "C:\Users\Mike\Desktop\Intended Location"

    The code is trying to create a document - C:\Users\Mike\Desktop\Intended LocationnewFileName.doc.

    2. You have also indicated the file format DOC. The default extension for Word 2010 is DOCX. If you want to use other formats, you would have to specify them. The following code should work without any prompts or dialogs showing and will produce two files in the target folder if it exists.

    If the code prompts with a dialog box, close Word and rename the normal template to oldnormal.dotx . Restart Word. Add the macro back and try again.

    I have reconfigured the username to apply to any user.

    Sub PleaseSaveAsANewFile()
    Dim newDirectoryPath As String
    Dim theNewFileName As String
    
        newDirectoryPath = "C:\Users\" & Environ("UserName") & "\Desktop\Intended Location\"
        theNewFileName = "newFilename.doc"
        'Save as DOC FORMAT'
        ActiveDocument.SaveAs2 Filename:=newDirectoryPath & theNewFileName, FileFormat:=wdFormatDocument97
        'Save as DOCX format ('x' added to filename)'
        'Compatibility mode option added as the document was originally saved as DOC'
        ActiveDocument.SaveAs2 Filename:=newDirectoryPath & theNewFileName & "x", _
                               FileFormat:=wdFormatXMLDocument, _
                               CompatibilityMode:=Val(Application.Version)
    End Sub


    Graham Mayor - Word MVP
    www.gmayor.com


    Thursday, April 21, 2016 4:02 AM
  • Hi Graham

    Thank you so much for your definition and code. I tried the code on a friends computer and YES IT WORKED FANTASTIC...

    But the bad news is, is that it does not work on my machine.

    1. I did as you said and renamed the normal template to oldnormal.dotx

    2. I thought it might be an existing macro interfering, but the renaming of the normal template knocks out that idea.

    So unfortunately I am left in a situation, where I know I have the correct code, but some reason my word is not acting in the way it should.

    I really feel like I am taking up your time on something that should be so straight forward and I apologize for this. 

    I don't know what it could be or what else to try.

    If you have any suggestions I would be grateful, but I can appreciate if this is too frustrating for you also.

    Kind regards

    By the way, the weird thing is, when I try the code below to save as a pdf this works for me... Crazy I know

        'Save Form as a PDF

            ActiveDocument.ExportAsFixedFormat OutputFileName:= _
            newPathAndFolder & timeSheetFileName, _
            ExportFormat:=wdExportFormatPDF, OpenAfterExport:=False, OptimizeFor:= _
            wdExportOptimizeForPrint, Range:=wdExportAllDocument, Item:= _
            wdExportDocumentContent, IncludeDocProps:=False, KeepIRM:=True, _
            CreateBookmarks:=wdExportCreateNoBookmarks, DocStructureTags:=True, _
            BitmapMissingFonts:=True, UseISO19005_1:=False



    • Edited by Mikee S Thursday, April 21, 2016 6:49 PM
    Thursday, April 21, 2016 6:29 PM
  • I know this is a strange request but do you by chance have a bluetooth addin showing up in Word.  I think it is an Intel addin.  If so remove it.
    Thursday, April 21, 2016 7:37 PM
  • Hi Mogulman

    I dont think so, how can I check for this?

    Thursday, April 21, 2016 7:48 PM
  • I don't understand why you can't get it to work so I usually look for other issues.  I've had problems with other addins causing problems.  One I remember is a bluetooth addin.

    Start Word
    Click File 
    Click word options
    Click Addins
    Look at addins (maybe btmoffice.dll)

    Look at all addins that are active.

    • Marked as answer by Mikee S Friday, April 22, 2016 8:13 PM
    • Unmarked as answer by Mikee S Friday, April 22, 2016 8:13 PM
    Thursday, April 21, 2016 10:17 PM
  • Hi Mogulman

    You very much put me into the correct path. I have solved this crazy situation at last.

    It is indeed a word Addin - It is the abDocs Word Addin that is the culprit.

    abDocs is a software program developed by Acer. So this seems to be something that only effects Acer Owners.

    Thank you for your help

    • Marked as answer by Mikee S Friday, April 22, 2016 8:22 PM
    Friday, April 22, 2016 8:20 PM
  • Thank you for your help, I have solved this crazy situation at last.

    It is the abDocs Word Addin that is the culprit.

    abDocs is a software program developed by Acer. So this seems to be something that only effects Acer Owners.



    Thank you again for your help
    • Marked as answer by Mikee S Friday, April 22, 2016 8:21 PM
    Friday, April 22, 2016 8:21 PM
  • Thank you for your help, I have solved this crazy situation at last.

    It is the abDocs Word Addin that is the culprit.

    abDocs is a software program developed by Acer. So this seems to be something that only effects Acer Owners.



    Thank you again for your help
    • Marked as answer by Mikee S Friday, April 22, 2016 8:21 PM
    Friday, April 22, 2016 8:21 PM