I have written a macro that will (when it works) search for a key word ("Fig")in a Word document then capture the subsequent string (which is the path to the file to be inserted) then set that string equal to a variable which is then used to .AddPicture to the file at the Selection point. The only problem is that the file is not inserted. If anyone can point out where I went wrong I would appreciate it.
The Word document would contain text like this:
Fig D:\My Pictures\statechart.jpg
The macro is:
Help! Please!Code:Public Sub InsertFigures() Rem Find the next Figure to be inserted Selection.Find.ClearFormatting With Selection.Find .Text = "Fig" .Replacement.Text = "" .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = True .MatchWholeWord = True .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Rem Move the selection to the word after the "Fig" marker Selection.Move Unit:=wdWord, Count:=1 Selection.EndOf Unit:=wdSentence, Extend:=wdExtend Selection.Copy Rem dealing with the Clipboard Section Rem Dim the object Dim strText As DataObject Set strText = New DataObject Dim figPath As String On Error GoTo NotText strText.GetFromClipboard figPath = strText.GetText(1) MsgBox figPath Rem remove the highlighted Selection Selection.Collapse direction:=wdCollapseEnd Rem Insert the file into the document Selection.InlineShapes.AddPicture FileName:=figPath, SaveWithDocument:=True NotText: If Err <> 0 Then MsgBox "data on clipboard is not text" End If End Sub




Reply With Quote