Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-23-2017, 12:17 AM
Yisrael Yisrael is offline Failed to apply macro to files in folder Windows 10 Failed to apply macro to files in folder Office 2016
Novice
Failed to apply macro to files in folder
 
Join Date: Mar 2017
Posts: 9
Yisrael is on a distinguished road
Default Failed to apply macro to files in folder

Hey,



First of all I want to mention my bad English, I hope you can understand me anyway.

Well, I try to run macro on multiple files in a folder, on a particular computer it works well, but on another computer the macro only runs on one file in a folder.
I have debugged using F8 and I see that the macro identifies the files, that is, it enters the loop 3 times as many files in the folder.

I have run the search command on each file from the files in its own right and it works well - which tells me that the search code is correct.

Where can this be the problem ?!

Thank you!

It is worth noting that I took this code from here

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

     With Selection.Find
        .ClearFormatting
        .Format = True
        .text = ""
        .Font.NameBi = "Arial"
        .Font.SizeBi = 11
        .Replacement.text = ""
        .Replacement.Font.NameBi = "david"
        .Replacement.Font.SizeBi = 20
        .Wrap = wdFindContinue
        .Execute Replace:=wdReplaceAll
    End With
    
    .Close SaveChanges:=True
  End With
  strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub
 
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Select a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
Reply With Quote
  #2  
Old 03-23-2017, 01:17 AM
gmayor's Avatar
gmayor gmayor is offline Failed to apply macro to files in folder Windows 10 Failed to apply macro to files in folder Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,105
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Can we assume that the documents are formatted in a right to left language (presumably Hebrew)? NameBi and SizeBi are RTL related parameters.

Add the line
Code:
Debug.Print .FullName
immediately after
Code:
With wdDoc
and check the VBA immediate window (CTRL+G) that the documents are opened.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 03-23-2017, 02:11 AM
Yisrael Yisrael is offline Failed to apply macro to files in folder Windows 10 Failed to apply macro to files in folder Office 2016
Novice
Failed to apply macro to files in folder
 
Join Date: Mar 2017
Posts: 9
Yisrael is on a distinguished road
Default

The documents were indeed written in Hebrew.

I'll try and update.

Thank you!
Reply With Quote
  #4  
Old 03-23-2017, 11:16 AM
Yisrael Yisrael is offline Failed to apply macro to files in folder Windows 10 Failed to apply macro to files in folder Office 2016
Novice
Failed to apply macro to files in folder
 
Join Date: Mar 2017
Posts: 9
Yisrael is on a distinguished road
Default

I did what you said, the output is the names of all the files in the folder...
Is there a chance?

And thank you again!
Reply With Quote
  #5  
Old 03-23-2017, 03:03 PM
Yisrael Yisrael is offline Failed to apply macro to files in folder Windows 10 Failed to apply macro to files in folder Office 2016
Novice
Failed to apply macro to files in folder
 
Join Date: Mar 2017
Posts: 9
Yisrael is on a distinguished road
Default

I will redefine the problem according to what I have discovered.

The macro only works on the active document, if none of the documents in the folder is the active document, it does not override any of the active documents.

The problem can now be focused: What can cause VBA to not run on inactive documents?
Reply With Quote
  #6  
Old 03-23-2017, 04:04 PM
macropod's Avatar
macropod macropod is offline Failed to apply macro to files in folder Windows 7 64bit Failed to apply macro to files in folder Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try:
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
     With .Content.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Format = True
        .Text = ""
        .Font.NameAscii = "Arial"
        .Font.SizeBi = 11
        .Replacement.Text = ""
        .Replacement.Font.NameAscii = "David"
        .Replacement.Font.SizeBi = 20
        .Wrap = wdFindContinue
        .Execute Replace:=wdReplaceAll
    End With
    .Close SaveChanges:=True
  End With
  strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub
 
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Select a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 03-25-2017, 11:24 PM
Yisrael Yisrael is offline Failed to apply macro to files in folder Windows 10 Failed to apply macro to files in folder Office 2016
Novice
Failed to apply macro to files in folder
 
Join Date: Mar 2017
Posts: 9
Yisrael is on a distinguished road
Default

Thank you!
it worked !!!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Failed to apply macro to files in folder Macro to change all text color to black in all docx files in a selected folder joewoods Word VBA 13 05-16-2016 06:29 PM
Macro to change/convert/delete txt files in folder+subfolders NoS Word VBA 4 03-03-2016 12:10 PM
Macro to replace SSN in all files within a folder caj1980 Word VBA 7 09-11-2014 04:17 PM
Failed to apply macro to files in folder macro to change name of files in a folder in order expert4knowledge Word VBA 5 07-10-2014 03:54 PM
Failed to apply macro to files in folder Word Macro - change date in footer for all files in a folder patidallas22 Word VBA 2 03-09-2012 08:14 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:20 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft