![]() |
|
|
|
#1
|
||||
|
||||
|
The basic problem with your code is that the Dir inside the loop wipes out the data for the outer Dir. Try:
Code:
Sub Case_Image()
Dim strFldr As String, strDocs As String, strImgs As String, strPath As String, StrImg As String
Dim wdDoc As Document, Rng As Range, i As Long
strPath = "C:\Cases\" '>>>>> change folder path if cases stored in different folder
strFldr = Dir(strPath & "*.doc", vbNormal) '>>>>> check if doc or docx and change accordingly
'Build document & image lists
While strFldr <> ""
strImgs = strImgs & vbCr & strPath & "\images\" & Left(strFldr, InStrRev(strFldr, ".")) & "jpg"
strDocs = strDocs & vbCr & strPath & strFldr
strFldr = Dir()
Wend
'Loop through the lists to match documents with images
For i = 1 To UBound(Split(strDocs, vbCr))
'If the document and its image are both found, add the image to the document
If (Dir(Split(strImgs, vbCr)(i), vbNormal) <> "") And (Dir(Split(strDocs, vbCr)(i), vbNormal) <> "") Then
Set wdDoc = Documents.Open(Split(strDocs, vbCr)(i), AddToRecentFiles:=False)
With wdDoc
.Range.Characters.First.InsertBreak wdPageBreak
Set Rng = .Range.Characters.First
Rng.Collapse wdCollapseStart
.InlineShapes.AddPicture FileName:=Split(strImgs, vbCr)(i), _
LinkToFile:=False, SaveWithDocument:=True, Range:=Rng
.Close True
End With
'If the image is missing, report it
ElseIf (Dir(Split(strImgs, vbCr)(i), vbNormal) <> "") Then
MsgBox Split(strImgs, vbCr)(i) & vbCr & "Not found", vbInformation
End If
Next
Set Rng = Nothing: Set wdDoc = Nothing
MsgBox "", vbInformation, "Folder consolidated"
End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#2
|
||||
|
||||
|
Thanks macropod, works great... confusing because I hardly ever use DIM so will need to decode it.
I had another linked question instead of giving it a predefined folder path to run - is there a way to make it run from wherever it is opened. As in I open my first file and run the macro, it will auto pick the path from my first file and do it work and move onto next file? Is there also a way to tell it to give me a select folder option? Like when I run the macro it will ask me if I want to start from current file and folder or choose a different folder? |
|
#3
|
||||
|
||||
|
Quote:
Quote:
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
batch file
|
romanticbiro | Office | 1 | 06-30-2014 06:04 PM |
| Batch adding Metadata to Docs | AndyTake2 | Word | 0 | 02-01-2013 10:18 AM |
Batch create Word documents
|
cdfj | Word VBA | 6 | 11-07-2012 01:03 PM |
Batch Edit Links
|
tosti | PowerPoint | 5 | 01-31-2012 12:51 PM |
Processing Time Intervals
|
pkrishna | Excel | 5 | 09-30-2011 06:24 AM |