Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Closed Thread
 
Thread Tools Display Modes
  #1  
Old 12-02-2016, 12:05 PM
Krategy Krategy is offline URGENT: Macro to open document and apply other macros successively Windows 10 URGENT: Macro to open document and apply other macros successively Office 2013
Novice
URGENT: Macro to open document and apply other macros successively
 
Join Date: Dec 2016
Posts: 1
Krategy is on a distinguished road
Exclamation URGENT: Macro to open document and apply other macros successively

I need to create a macro that prompts the user for a location to find the documents via an InputBox and then applies the features of other macros created in the original document to the opened documents one by one (It opens the document, applies features, repeat)



This is for a school assignment that is due today in a few hours and I am willing to pay if someone can help me figure this out in time - also it is a requirement to use nested loops
(Exact description: You must employ nested loops in order to get credit for this feature, an outer loop successively opens each document in the specified location and will apply Features 1 - 7 inside its body)

Code:
Sub otherDocuments()
'
' otherDocuments Macro
' This macro opens other documents and applies some features of this document to them
'
Dim Folder As String
Dim File As String
Dim i As Long
Folder = InputBox("Please enter the directory of the folder containing the documents")
File = Dir(Folder & "\*.docx")
i = 1
Dim numDocuments As Long
numDocuments = Documents.Count
Do While File <> ""
    Documents.Open FileName:=Folder & "\" & File
    Do While (i <= numDocuments)
        Call searchAndReplace
        i = i + 1
    Loop
Loop
    
End Sub
So far I've created this however it opens one document, calls the macro and then opens all the other documents all at the same time without calling the macro, how can I get it to open one document at a time and call the macros each time while using nested loops?

Any help is greatly appreciated.
  #2  
Old 12-02-2016, 04:41 PM
macropod's Avatar
macropod macropod is offline URGENT: Macro to open document and apply other macros successively Windows 7 64bit URGENT: Macro to open document and apply other macros successively 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

The following macro allows you to browse to a folder containing the documents you want to process, then replace a given string in all documents in that folder automatically.
Code:
Sub UpdateDocuments()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, strDocNm As String, wdDoc As Document
strDocNm = ActiveDocument.FullName
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.doc", vbNormal)
While strFile <> ""
  If strFolder & "\" & strFile <> strDocNm Then
    Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
    Call SearchAndReplace(wdDoc)
    wdDoc.Close SaveChanges:=True
  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 SearchAndReplace(wdDoc As Document)
With wdDoc
  'Your document-specific code goes here
End With
End Sub
Note how the document you want to process is passed to the SearchAndReplace sub. In that sub, you replace references to ActiveDocument with wdDoc. Other changes, too, may be required but, as you haven't posted the relevant code, I can't advise on that.

PS: Asking others to do coding for you that you're being assessed on is called cheating. The only reason I posted the above (hopefully too late to be of use) is because a search of this forum (which you're apparently too lazy to do) would turn up plenty of threads using almost identical code.

Thread closed.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Closed Thread



Similar Threads
Thread Thread Starter Forum Replies Last Post
URGENT: Macro to open document and apply other macros successively Macro to Open to a Website When No Document Is Open lostinwebspace Word VBA 1 02-13-2016 10:28 AM
Help - Need Macro to Apply Blur SSL PowerPoint 6 07-26-2015 08:59 AM
URGENT: Macro to open document and apply other macros successively URGENT: macro icons don't work when 'save as' AndyDDUK PowerPoint 9 11-21-2012 08:21 AM
URGENT: Macro to open document and apply other macros successively Macro to apply style to selected tables ubns Word 1 08-02-2012 04:09 AM
macro to open another document coconutt Word VBA 1 06-11-2012 04:39 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 04:01 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