PDA

View Full Version : Implement images in a word document does only work 580 time, please help!



mikeeez
07-25-2014, 09:37 AM
Hi!
I wrote a markro, which takes images from a folder and puts it in a word document. The makro works like a charm until it comes to the 580th image. Than it ether deletes the image while resizing it or it just not getting it from the folder, instead it puts a square character in. I have no idea what the problem is. Its even more mysterious that the same makro is working under Mac Office 2011. I hope somebody can help! Thank you!


Here is the code I'm using:

'Insert pictures
MyFile = Dir$(targetfolder & "\*.*")
Do
pn = ActiveDocument.InlineShapes.Count
targetfile = targetfolder & "\" & MyFile
MyFile = Dir
ActiveDocument.Range.Select
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "PICXX"
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
.Execute
End With
'paste
Call clear_clipboard
Selection.InlineShapes.AddPicture FileName:= _
targetfile _
, LinkToFile:=False, SaveWithDocument:=True
ActiveDocument.InlineShapes(pn + 1).Select
'change size
With Selection.InlineShapes(1)
If AutoScript.FormatBox2.ListIndex = 1 Then
.Height = 47.84
Else
.Height = 66.05
End If
.Width = 85.05
End With
Loop Until MyFile = ""

macropod
07-25-2014, 06:13 PM
That suggests there is a problem with the image file.

PS: When posting code, please use the code tags. They're indicated by the # symbol on the posting screen.

mikeeez
07-27-2014, 07:09 AM
Thank you, Paul! But I think it has not so much to do with it. It doesn't matter which format I use (png, jpeg), its all the same. It must be related to the memory or some other resource issue. Do you have another idea?
Thank you!

macropod
07-27-2014, 03:22 PM
How do you know it has nothing to do with it? After all, that last image doesn't get inserted correctly. What checks have you made?

mikeeez
07-27-2014, 03:48 PM
...checked with different image formats (jpeg, png). I tried also with a different image collection. Again, after the 580th image the script stopped working.
Cheers,
Michael

macropod
07-27-2014, 05:17 PM
You might try something like:

Dim wdRng As Range
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "PICXX"
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
.Execute
End With
If .Find.Found = True Then
MyFile = Dir$(targetfolder & "\*.*")
Set wdRng = .Duplicate
While MyFile <> ""
targetfile = targetfolder & "\" & MyFile
wdRng.Collapse wdCollapseEnd
wdRng.InlineShapes.AddPicture FileName:=targetfile, _
LinkToFile:=False, SaveWithDocument:=True
With .InlineShapes(.InlineShapes.Count)
If AutoScript.FormatBox2.ListIndex = 1 Then
.Height = 47.84
Else
.Height = 66.05
End If
.Width = 85.05
End With
If .InlineShapes.Count Mod 100 = 0 Then DoEvents
Loop Until MyFile = ""
End If
End With
Set wdRng = Nothing
Note the use of the DoEvents line. This gives Word a chance to do its own housekeeping.

mikeeez
07-28-2014, 04:59 PM
Hi Paul! thank you very much!!! I put the puzzle together and finally it works. This is how the code looks like:


'Insert pictures

MyFile = Dir$(targetfolder & "\*.*")
While MyFile <> ""
pn = ActiveDocument.InlineShapes.Count
'targetfile = targetfolder & "\" & MyFile
'MyFile = Dir
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "PICXX"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.MatchWholeWord = True
.Execute Replace:=WdReplace
End With
If .Find.Found = True Then

Set wdRng = .Duplicate

targetfile = targetfolder & "\" & MyFile
wdRng.Collapse wdCollapseEnd
wdRng.InlineShapes.AddPicture FileName:=targetfile, _
LinkToFile:=False, SaveWithDocument:=True
With ActiveDocument.InlineShapes(1)
If AutoScript.FormatBox2.ListIndex = 1 Then
.Height = 47.84
Else
.Height = 66.05
End If
.Width = 85.05
End With
If .InlineShapes.Count Mod 100 = 0 Then DoEvents
End If
End With
MyFile = Dir
Set wdRng = Nothing
Wend

I was close to giving up. Thank you so much, Paul!

Cheers,
Michael

macropod
07-28-2014, 08:22 PM
Do note that Word has a maximum supported file size of 512Mb. Since pictures tend to consume quite a lot of storage, and you seemingly have very large numbers of them to insert, you'll need to be careful to not transgress that limit. Otherwise, you'll find yourself unable to re-open the document.