Answer the question
In order to leave comments, you need to log in
Why does the image insertion macro in Word work weird?
Wrote a primitive macro for inserting images via links in Word documents. Those. I have documents containing text that contains links to images from one particular site. You need to replace the links to the images themselves.
Sub Link2img()
Dim TempData As DataObject
Dim Count As Integer
Dim ImgURL As String
Dim ImgNum As String
Count = 0
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = "https://aaa.bbb.com/images/?*.[jpng]{3}"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
Do While (.Execute = True)
Selection.Find.Execute
Selection.Copy
Set TempData = New DataObject
TempData.GetFromClipboard
ImgURL = TempData.GetText
Selection.Delete
Selection.InlineShapes.AddPicture FileName:=ImgURL, LinkToFile:=False, SaveWithDocument:=True
Count = Count + 1
Loop
End With
If Count < 2 Then ImgNum = " изображение"
If Count > 1 And Count < 5 Then ImgNum = " изображения"
If Count > 4 Then ImgNum = " изображений"
MsgBox ("Вставлено " & Count & ImgNum)
End Sub
Answer the question
In order to leave comments, you need to log in
Figured it out myself. The Selection.Find.Execute line in the Do While (.Execute = True) ... Loop was redundant. She forced, when finding a link, to immediately look for the next one and only then perform actions.
Sub Link2img()
Dim TempData As DataObject, Count As Integer, ImgURL, ImgNum As String
Count = 0
Selection.HomeKey Unit:=wdStory
With Selection.Find
.ClearFormatting
.Text = "https://aaa.bbb.com/images/?*.[jpng]{3}"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
Do While (.Execute = True)
Selection.Copy
Set TempData = New DataObject
TempData.GetFromClipboard
ImgURL = TempData.GetText
Selection.Delete
Selection.InlineShapes.AddPicture FileName:=ImgURL, LinkToFile:=False, SaveWithDocument:=True
Count = Count + 1
Loop
End With
If Count < 2 Then ImgNum = " изображение"
If Count > 1 And Count < 5 Then ImgNum = " изображения"
If Count > 4 Then ImgNum = " изображений"
MsgBox ("Вставлено " & Count & ImgNum)
End Sub
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question