VBA create and save a series of word docs

ldarley

Board Regular
Joined
Apr 10, 2012
Messages
106
Office Version
  1. 2019
  2. 2016
Hi

I've got a macro that creates a series of folders then opens a series of dotx word templates and then saves them to the folders that have been created. This has been working fine, but now seems to have hit a problem where some of the documents open but don't save automatically to the folder and instead prompts the user to save which causes the code to bomb out.

Would anyone be able to point out how I could improve my code to prevent this, its quite long so I've only posted the section with the word document creation loop. I've got option explicit at the top and all my variables are ok. Any pointers in sorting this is much appreciated.



Code:
'open templates, populate and save
        x = 0
'intFoundFileswd is total number of word templates to run        
Do Until x = intFoundFileswd
            ' this is the save name
            WdTemplate = Left(FileListwd(x), Len(FileListwd(x)) - 5)
            
            With wApp
   
                Set wDoc = wApp.Documents.Open(strFolder & FileListwd(x))
                'pre populate some info into doc fields
                With wDoc
                    .BuiltinDocumentProperties("Title").Value = Title
                    .BuiltinDocumentProperties("Manager").Value = Manager
                    .BuiltinDocumentProperties("Subject").Value = Reference
                    If InStr(WdTemplate, "Checklist") > 0 Then
                    .SaveAs Filename:=strNewProjectFolder & SubFolder & FolderReference & "\1. Checklists\" & Reference & "-" & WdTemplate, FileFormat:=wdFormatXMLDocument, AddtoRecentFiles:=False
                    ElseIf InStr(WdTemplate, "Assessment") > 0 Then
                    .SaveAs Filename:=strNewProjectFolder & SubFolder & FolderReference & "\2. Gateway Assessments\" & Reference & "-" & WdTemplate, FileFormat:=wdFormatXMLDocument, AddtoRecentFiles:=False
                    ElseIf InStr(WdTemplate, "Meeting") > 0 Then
                    .SaveAs Filename:=strNewProjectFolder & SubFolder & FolderReference & "\5. Project Meetings\" & Reference & "-" & WdTemplate, FileFormat:=wdFormatXMLDocument, AddtoRecentFiles:=False
                    Else
                    .SaveAs Filename:=strNewProjectFolder & SubFolder & FolderReference & "\4. Project Documents\1. Project Management Documents\" & Reference & "-" & WdTemplate, FileFormat:=wdFormatXMLDocument, AddtoRecentFiles:=False
                    End If
                End With
            End With
        Set wDoc = Nothing
        x = x + 1
        Loop
        wApp.Quit
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
application.displayalerts=false at the beginning
and
application.displayalerts=true at the end maybe?
 
Upvote 0
Ok I've added that but it still seems to open all of the templates but only save some of them to the correct file location. It seems to happen with the xl templates too, although all of them are prompting a user save:

Code:
'close all other workbooks
        For Each wb In Workbooks
            If Not (wb Is ActiveWorkbook) Then wb.Close
        Next
        wb.DisplayAlerts = False
        
        'open templates,populate and save
        z = 0
        Do Until z = intFoundFilesxl
            xlTemplate = Left(FileListxl(z), Len(FileListxl(z)) - 5)
            Set wb = Workbooks.Open(strFolder & FileListxl(z))
            If InStr(xlTemplate, "Inventory") > 0 Then
            Sheets("Dashboard").Range("D4").Value = Title
            Sheets("Dashboard").Range("D5").Value = Reference
            Sheets("Dashboard").Range("J4").Value = Manager
            End If
            wb.SaveAs Filename:=strNewProjectFolder & SubFolder & FolderReference & "\4. Project Documents\1. Project Management Documents\" & Reference & "-" & xlTemplate, FileFormat:=52
            wb.Close
        z = z + 1
        Loop
        wb.DisplayAlerts = False
 
Upvote 0
Ok a little bit more on this, I am getting a runtime error 1004 on the wb.SaveAs line the error says: cannot access read only document '1. Project Management Documents'. I don't understand that at all as that is a folder reference not a document as you can see in that line of code it ends with a \ to demarcate the end of the folder reference.

If I remove the final part of the filepath 1. Project Management Documents\ it will save one level up but as an 'x file' with .x at the end, not a macro enabled workbook as intended. The next template then opens and saves as a excel 97-2003 worksheet. Bizarre

Whats weird is this worked saving this way 179 times before the error came up. Any thoughts?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,510
Messages
6,125,234
Members
449,216
Latest member
biglake87

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top