Convert INCLUDEPICTURE text strings to Word fields with curly braces around the text string

G

google

Hi everyone
From a Filemaker database, I produce a Word document with exam
questions.
Some of the questions have images (diagrams). A text reference for
each image arrives in the Word document as a paragraph, with the
content required for a linked picture field. For example:
INCLUDEPICTURE "http://www.homepage.mac.com/itraining/iQ/24487.jpg

What I need is a macro to run through the document and convert the
INCLUDEPICTURE text string to a proper field, so the image is viewable
in Word. I am hopeless at VBA and have been unable to nail this with
my very limited Macro skills. Basically I need the opening and closing
"curly braces" to encompass the text string. Three examples are shown
below with live images that are accessible via a web browser:

Question 10
INCLUDEPICTURE "http://www.homepage.mac.com/itraining/iQ/24487.jpg"

Question 11
INCLUDEPICTURE "http://www.homepage.mac.com/itraining/iQ/24405.jpg"

Question 12
INCLUDEPICTURE "http://www.homepage.mac.com/itraining/iQ/22031.gif"

Any help would be greatly appreciated.
Thanks in advance.

Michael Richards
Brisbane (Australia)
 
H

Helmut Weber

Hi Michael,

it is only about searching,
checking whether the search result is not in a field,
and a bit of string handling.
I am hopeless at VBA and have been unable to nail this
with my very limited Macro skills.

Hmm, could be difficult for you as this it
at a somewhat advanced level, if I may say so.

Sub Macro12()
Dim r As Range
Dim s As String
Set r = ActiveDocument.Range
ActiveWindow.View.ShowFieldCodes = True
With r.Find
.Text = "INCLUDEPICTURE"
.MatchCase = True
While .Execute And r.Fields.Count = 0
r.Collapse Direction:=wdCollapseEnd
.Text = """*"""
.MatchWildcards = True
If .Execute Then
r.Select ' remove after testing
s = r.Text
s = "INCLUDEPICTURE " & s
r.Paragraphs(1).Range.Select
Selection.End = Selection.End - 1
Selection.Fields.Add _
Range:=Selection.Range, _
Type:=wdFieldEmpty, _
Text:=s, _
PreserveFormatting:=True
End If
Wend
End With
ActiveWindow.View.ShowFieldCodes = False
End Sub

With only 3 pictures, this is working alright,
here and now. However, for timing issues,
I would not recommend trying to do all in one go.

Rather process one picture after the other,
by manually executing the macro for each picture,
like this:

Sub Macro12A()
Dim r As Range
Dim s As String
Set r = ActiveDocument.Range
ActiveWindow.View.ShowFieldCodes = True
With r.Find
.Text = "INCLUDEPICTURE"
.MatchCase = True
If .Execute And r.Fields.Count = 0 Then
r.Collapse Direction:=wdCollapseEnd
.Text = """*"""
.MatchWildcards = True
If .Execute Then
r.Select ' remove after testing
s = r.Text
s = "INCLUDEPICTURE " & s
r.Paragraphs(1).Range.Select
Selection.End = Selection.End - 1
Selection.Fields.Add _
Range:=Selection.Range, _
Type:=wdFieldEmpty, _
Text:=s, _
PreserveFormatting:=True
End If
End If
End With
ActiveWindow.View.ShowFieldCodes = False
End Sub

There are many ways of improvement,
e.g. avoiding the selection-object.
I have kind of a strange mixture of
range and selection here.

I've just tried to keep it understandable.


HTH, keep on!

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top