Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 12-24-2019, 12:16 PM
noslenwerd noslenwerd is offline Using VBA to find html img code, and insert images dynamically Windows 10 Using VBA to find html img code, and insert images dynamically Office 2016 for Mac
Novice
Using VBA to find html img code, and insert images dynamically
 
Join Date: Dec 2019
Posts: 15
noslenwerd is on a distinguished road
Default Using VBA to find html img code, and insert images dynamically

I am repurposing code that macropod had authored in a previous post for finding html for links, and converting the hyperlinks with custom text in a word document.



For reference that code is below:

Code:
Sub ConvertHyperlinks()
Dim StrAddr As String, StrDisp As String
' Turn Off Screen Updating
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .Text = "\<[Aa] href=([!\>]@)\>([!\<]@)\</a\>"
    .ClearFormatting
    .Replacement.ClearFormatting
    .Replacement.Text = ""
    .Forward = True
    .MatchWildcards = True
    .Wrap = wdFindStop
    .Execute
  End With
  Do While .Find.Found = True
    StrAddr = Replace(Replace(Replace(Split(Split(.Text, ">")(0), "=")(1), Chr(34), ""), Chr(147), ""), Chr(148), "")
    StrDisp = Split(Split(.Text, ">")(1), "<")(0)
    .Hyperlinks.Add Anchor:=.Duplicate, _
      Address:=StrAddr, TextToDisplay:=StrDisp
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
' Restore Screen Updating
Application.ScreenUpdating = True
End Sub
I am trying to repurpose that code to do the same for html code for images.

Essentially I want the macro to find all instances of <img src="c:\users\8172837\worksheet\test.jpg">, and embed that image into the document.

Below is the code I came up with, but I am running into issues. Admittedly I am not understanding the wildcards that macropod used here, but I tried researching to the best of my abilities.

Code:
Sub ConvertImages()
Dim StrAddr As String
' Turn Off Screen Updating
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .Text = "\<img src=([!\>]@)\>"
    .ClearFormatting
    .Replacement.ClearFormatting
    .Replacement.Text = ""
    .Forward = True
    .MatchWildcards = True
    .Wrap = wdFindStop
    .Execute
  End With
  Do While .Find.Found = True
    StrAddr = Replace(Replace(Replace(Split(Split(.Text, ">")(0), "=")(1), Chr(34), ""), Chr(147), ""), Chr(148), "")
	InlineShapes.AddPicture FileName:=StrAddr, LinkToFile:=False
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
' Restore Screen Updating
Application.ScreenUpdating = True
End Sub
Reply With Quote
  #2  
Old 01-01-2020, 04:00 AM
macropod's Avatar
macropod macropod is offline Using VBA to find html img code, and insert images dynamically Windows 7 64bit Using VBA to find html img code, and insert images dynamically 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 ConvertImageLinks()
Dim StrImg As String
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .Text = "\<img src=[!\>]@\>"
    .ClearFormatting
    .Replacement.ClearFormatting
    .Replacement.Text = ""
    .Forward = True
    .MatchWildcards = True
    .Wrap = wdFindStop
    .Execute
  End With
  Do While .Find.Found = True
    StrImg = Replace(Replace(Replace(Split(Split(.Text, ">")(0), "=")(1), Chr(34), ""), Chr(147), ""), Chr(148), "")
    .Text = vbNullString
    .InlineShapes.AddPicture StrImg, False, True, .Duplicate
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 01-02-2020, 08:47 AM
noslenwerd noslenwerd is offline Using VBA to find html img code, and insert images dynamically Windows 10 Using VBA to find html img code, and insert images dynamically Office 2016 for Mac
Novice
Using VBA to find html img code, and insert images dynamically
 
Join Date: Dec 2019
Posts: 15
noslenwerd is on a distinguished road
Default

Works perfectly!

Is there anyway to parlay alignment and text wrap into this? I tried the code below, but the bolded code does not work.

Code:
Sub DSSImages()
Dim StrImg As String
Application.ScreenUpdating = False
With ActiveDocument.Range
  With .Find
    .Text = "\<img src=[!\>]@\>"
    .ClearFormatting
    .Replacement.ClearFormatting
    .Replacement.Text = ""
    .Forward = True
    .MatchWildcards = True
    .Wrap = wdFindStop
    .Execute
  End With
  Do While .Find.Found = True
    StrImg = Replace(Replace(Replace(Split(Split(.Text, ">")(0), "=")(1), Chr(34), ""), Chr(147), ""), Chr(148), "")
    .Text = vbNullString
    .InlineShapes.AddPicture StrImg, False, True, .Duplicate
    .WrapFormat.Type = wdWrapSquare
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
End Sub
Lastly... (I am not asking you to write this code, I am curious if anything exits):

We need to source all of our images. So I would love to find a way to have the html below, pull in the image, and have a source link below it (where we found the image in the first place).

<img src="C:\test\logo.jpg" cap="http://linktothesource.com">
Reply With Quote
  #4  
Old 01-02-2020, 02:14 PM
macropod's Avatar
macropod macropod is offline Using VBA to find html img code, and insert images dynamically Windows 7 64bit Using VBA to find html img code, and insert images dynamically 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 noslenwerd View Post
Is there anyway to parlay alignment and text wrap into this?
Change:
.InlineShapes.AddPicture StrImg, False, True, .Duplicate
to:
ActiveDocument.Shapes.AddPicture StrImg, False, True, , , , , .Duplicate

Note that the empty arguments allow you to define the size & position - see the VBA help file for more details.
Quote:
Originally Posted by noslenwerd View Post
We need to source all of our images. So I would love to find a way to have the html below, pull in the image, and have a source link below it (where we found the image in the first place).

<img src="C:\test\logo.jpg" cap="http://linktothesource.com">
Delete:
.Text = vbNullString
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Tags
html, img code

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using VBA to find html img code, and insert images dynamically Macro Help Swapping HTML Code pclark2 Word VBA 2 02-11-2019 03:25 PM
How to insert a straight table into PPT where the values are dynamically updated? Officer_Bierschnitt PowerPoint 1 01-14-2016 10:34 AM
Search and replace/insert HTML code into Master File using tags dave8555 Excel 2 02-23-2014 03:51 PM
Outlook Web Mail (OWA) - sending email removes html and images mvrk Outlook 0 07-30-2013 03:56 AM
Strange HTML code inside an e-mail Joostdegrote Outlook 0 09-13-2010 07:57 AM

Other Forums: Access Forums

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