Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-05-2017, 07:21 AM
ballpoint ballpoint is offline Loop Through all documents in a folder Windows 10 Loop Through all documents in a folder Office 2016
Advanced Beginner
Loop Through all documents in a folder
 
Join Date: Sep 2017
Posts: 42
ballpoint is on a distinguished road
Question Loop Through all documents in a folder

I am trying to run a similar macro to https://www.msofficeforums.com/word-...ocx-files.html (actually, a much simplified version of it), but to no avail.



My idea is to run a macro I have (foot2inline) on a number of files, but I am somehow failing at that.

I have done this:

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 foot2inline
    .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, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
Word appears to start something, but then everything goes back to a blank sheet. Is there anything obvious I am missing? Thanks!
Reply With Quote
  #2  
Old 11-05-2017, 12:56 PM
macropod's Avatar
macropod macropod is offline Loop Through all documents in a folder Windows 7 64bit Loop Through all documents in a folder Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

Assuming you're running this from Word (what you mean by "everything goes back to a blank sheet" is ambiguous), there is no reason the code stub you'd posted won't work properly. For all we know, though, the problem is with your foot2inline code - which you haven't posted.

PS: I've split this off to a separate thread as, aside from looping through a folder or Word files, the topic seems unrelated to the original one.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 11-05-2017, 12:58 PM
ballpoint ballpoint is offline Loop Through all documents in a folder Windows 10 Loop Through all documents in a folder Office 2016
Advanced Beginner
Loop Through all documents in a folder
 
Join Date: Sep 2017
Posts: 42
ballpoint is on a distinguished road
Default

You are absolutely correct, sorry for the omission.

Here is the code:

Code:
Sub foot2inline()
Dim oFeets As Footnotes
Dim oFoot As Footnote
Dim oRange As Range
Dim szFootNoteText As String
Dim index As Long

' Grabs the collection of FootNotes
Set oFeets = ActiveDocument.Footnotes

' Iterates through each footnote
For Each oFoot In oFeets
    index = index + 1
    szFootNoteText = oFoot.Range.Text
    
    'Start search from beginning of document
    Set oRange = ActiveDocument.Range
      
    With oRange.Find
        .Text = "^f" ' Looks for all footnotes
        .Forward = True
        .Wrap = wdFindStop
        .Execute
    End With
    
    ' Delete the footnote
    oFoot.Delete
    
    'Insert the footnote text
    oRange.Text = " [Note " & index & ": " & szFootNoteText & "] "

    'CHANGE COLOR HERE. Color code is below.
    'oRange.Font.Color = 6299648

    'Disables undo to save memory on very large documents.
    'ActiveDocument.UndoClear
Next
End Sub
Reply With Quote
  #4  
Old 11-05-2017, 01:26 PM
macropod's Avatar
macropod macropod is offline Loop Through all documents in a folder Windows 7 64bit Loop Through all documents in a folder Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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 strDocNm As String, strFolder As String, strFile As String, wdDoc As Document
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.doc", vbNormal)
strDocNm = ThisDocument.FullName
While strFile <> ""
  If strFolder & "\" & strFile <> strDocNm Then
    Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
    With wdDoc
      Call Foot2Inline(wdDoc)
      .Close SaveChanges:=True
    End With
  End If
  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, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function

Sub Foot2Inline(wdDoc As Document)
Dim i As Long, Rng1 As Range, Rng2 As Range
With wdDoc
  For i = .Footnotes.Count To 1 Step -1
    With .Footnotes(i)
      Set Rng1 = .Reference
      Set Rng2 = .Range
      Rng2.End = Rng2.End - 1
      With Rng1
        .Collapse wdCollapseEnd
        .Font.Reset
        .FormattedText = Rng2.FormattedText
        .InsertBefore "[Note " & i & ": "
        .InsertAfter "]"
        .Font.Color = 6299648
      End With
      .Delete
    End With
  Next
End With
End Sub
Note: The above code preserves formatting in your footnotes (e.g. bold italics, etc.).
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 11-05-2017, 03:40 PM
ballpoint ballpoint is offline Loop Through all documents in a folder Windows 10 Loop Through all documents in a folder Office 2016
Advanced Beginner
Loop Through all documents in a folder
 
Join Date: Sep 2017
Posts: 42
ballpoint is on a distinguished road
Default

Thank you so much for taking the time and producing this stellar code!
Reply With Quote
  #6  
Old 12-13-2018, 04:27 PM
Guessed's Avatar
Guessed Guessed is offline Loop Through all documents in a folder Windows 10 Loop Through all documents in a folder Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

FWIW, I still think the code should also include docx and docm files. For a minor coding efficiency improvement, I would also change the GetFolder function to add the trailing "" to the returned string. This would remove the need to include it separately 3 times in the calling macro.
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 & Application.PathSeparator
  Set oFolder = Nothing
End Function
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #7  
Old 12-13-2018, 08:20 PM
macropod's Avatar
macropod macropod is offline Loop Through all documents in a folder Windows 7 64bit Loop Through all documents in a folder Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

Quote:
Originally Posted by Guessed View Post
FWIW, I still think the adjusted code should also include docx and docm files as per an earlier suggestion.
Simply searching via:
strFile = Dir(strFolder & "\*.doc", vbNormal)
will return docx & docm files as well as doc files.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #8  
Old 12-13-2018, 09:28 PM
Guessed's Avatar
Guessed Guessed is offline Loop Through all documents in a folder Windows 10 Loop Through all documents in a folder Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Sorry Paul, you are correct.

I didn't know that.
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #9  
Old 08-15-2022, 10:37 AM
shpkmom shpkmom is offline Loop Through all documents in a folder Windows 11 Loop Through all documents in a folder Office 2021
Novice
 
Join Date: Aug 2022
Posts: 2
shpkmom is on a distinguished road
Default

Thank you so much for this! I have modified the coding a bit to match a macro I am working on. This macro is to loop through all the documents in a folder, search and replace some text and save and close the document. The problem is that the document will save even if no changes were actually made. The user wants to be able to identify which documents were actually changed. Is there a way to do that?
Reply With Quote
  #10  
Old 08-15-2022, 04:24 PM
Guessed's Avatar
Guessed Guessed is offline Loop Through all documents in a folder Windows 10 Loop Through all documents in a folder Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,932
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

You can test whether Word considers the document had a change made before deciding whether to save.
Try changing this code
Code:
    With wdDoc
      Call Foot2Inline(wdDoc)
      .Close SaveChanges:=True
    End With
To this (untested aircode)
Code:
    With wdDoc
      Call Foot2Inline(wdDoc)
      If .Saved then
         .Close SaveChanges:=False
      Else
         .Close SaveChanges:=True
      End If
    End With
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #11  
Old 08-15-2022, 05:24 PM
macropod's Avatar
macropod macropod is offline Loop Through all documents in a folder Windows 10 Loop Through all documents in a folder Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
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

shpkmom: Kindly don't post the same question in multiple threads. In any event, you've omitted crucial details here as to what is going on in your document (see https://www.msofficeforums.com/word-...h-replace.html).
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Loop Through all documents in a folder How to open Documents folder directly from CTRL+O of Open folder on QAT scvjudy Word 2 08-11-2014 10:58 PM
Loop Through all documents in a folder Content of all documents in folder changes to match one of them jemmac2525 Word 2 11-11-2013 12:32 AM
Office 2010 Can't Open Or Save Documents in My Documents Folder trippb Office 1 07-12-2013 07:29 AM
Loop Through all documents in a folder Documents saved to wrong folder stevecarr Word 1 09-22-2011 05:32 AM
Loop through folder of workbooks and copy range to other workbook Snvlsfoal Excel Programming 3 07-29-2011 05:55 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:26 AM.


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