-
Nov 16th, 2016, 08:28 AM
#1
Thread Starter
New Member
[RESOLVED] [Word] Macro breaks when looping through multiple files
Hello everyone. I am using Word 2016, and I work very little with macros, but I have been asked to create a macro that will create a new document property and to populate it with the existing Title property. I have that part working, but when I try to use a macro to loop this macro through all of the documents in a folder, I receive the following error:
Code:
Run-time error '-2147467259 (800004005)':
Automation error
Unspecified error
Macro for looping through the folders
Code:
Sub UpdateDocuments()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, wdDoc As Document
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.docx", vbNormal)
While strFile <> ""
Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
With wdDoc
'Call your other macro or insert its code here
Call ChangeProperties
.Close SaveChanges:=True
End With
strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub
Code:
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.path
Set oFolder = Nothing
End Function
Macro to create/change property
Code:
Sub ChangeProperties()
With ActiveDocument.CustomDocumentProperties
.Add Name:="NewTitle", _
LinkToContent:=False, _
Type:=msoPropertyTypeString, _
Value:=ActiveDocument.BuiltInDocumentProperties("Title")
End With
End Sub
I have shamelessly borrowed the folder looping macro from a post on the Internet and I tried a few different ones, but they all end up with the same error. We have a couple hundred documents that we need to run this macro on, so I was hoping I could just specify a folder to run it against.
If I press the debug button on the error, it highlights lines 3 through 6 of the ChangeProperties macro with an arrow pointing to the 6th line. I tried setting Visible to True, and adding wdDoc.Activate, in UpdateDocuments, but neither helped. Programming is not my forte, so any help would be appreciated.
Thank you.
-
Nov 16th, 2016, 03:25 PM
#2
Re: [Word] Macro breaks when looping through multiple files
i would avoid working with activedocument, when adding the property, work with the fully qualified object
as the changeproperties procedure only really contains one line of code, i would remove the additional procedure and put the one line of code in the main procedure like
Code:
Sub UpdateDocuments()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, wdDoc As Document
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.docx", vbNormal)
While strFile <> ""
Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
With wdDoc
.CustomDocumentProperties.Add Name:="NewTitle", _
LinkToContent:=False, _
Type:=msoPropertyTypeString, _
Value:=ActiveDocument.BuiltInDocumentProperties("Title")
.Close SaveChanges:=True
End With
strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub
i can not say that any of this will fix your problem, but better code anyway and worth a try, i do not see any other issues in your code
is the number of documents processed before error always the same? you could put a counter in the code to see if this is the case
i suppose there is some good reason to add an additional property to duplicate an existing one
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
-
Nov 16th, 2016, 05:13 PM
#3
Thread Starter
New Member
Re: [Word] Macro breaks when looping through multiple files
I ended up adding in a check to see if the document property already exists before trying to add the property, which fixed the error. None of the documents should have had that property, but it works now so... Thanks for your help though.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|