|
-
Apr 11th, 2008, 06:34 AM
#1
Thread Starter
New Member
[RESOLVED] Can't get Picture to Insert when using variable
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:
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
Help! Please!
-
Apr 11th, 2008, 08:06 AM
#2
Fanatic Member
Re: Can't get Picture to Insert when using variable
Welcome to the forums. You can accomplish what you are wanting without doing anything with the clipboard. I've modified your code.
Code:
Option Explicit
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
'Change this line
'Selection.EndOf Unit:=wdSentence, Extend:=wdExtend
'to this
Selection.MoveEndUntil Chr(13) 'extend selection until paragraph marker is found
'No need to overwrite the clipboard.
Dim figPath As String
figPath = Selection.Text
'Or you could set a reference to "Microsoft Scripting Runtime" and declare for early binding
'Dim fs as New FileSystemObject
Dim fs As Object
Set fs = CreateObject("Scripting.FileSystemObject")
If fs.FileExists(figPath) Then
Rem remove the highlighted Selection
Selection.Collapse direction:=wdCollapseEnd
Rem Insert the file into the document
Selection.InlineShapes.AddPicture FileName:=figPath, SaveWithDocument:=True
Else
MsgBox "File doesn't exist. Do error handling here."
End If
End Sub
-
Apr 11th, 2008, 08:09 AM
#3
Fanatic Member
Re: Can't get Picture to Insert when using variable
Where I changed this:
Code:
'Change this line
'Selection.EndOf Unit:=wdSentence, Extend:=wdExtend
'to this
Selection.MoveEndUntil Chr(13) 'extend selection until paragraph marker is found
will only work if the filename exists to the end of the paragraph. If this is not the case, you will need to check for and remove the paragraph markers from the text you have selected (ASCII 13). I would also clean the filename with a Trim(). Let me know what you think.
-
Apr 11th, 2008, 08:12 AM
#4
Thread Starter
New Member
Re: Can't get Picture to Insert when using variable
dMaruca,
That did it! Thanks very much!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|