Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems inserting image into Word doc using VBA

Status
Not open for further replies.

DougP

MIS
Dec 13, 1999
5,985
US
Code:
        .Selection.InlineShapes.AddPicture FileName:= _
        "F:\Marketing\Graphics & Logos\OHC Blue Logo smaller2.jpg", LinkToFile:= _
        False, SaveWithDocument:=True
        .Selection.InlineShapes(1).Height = 36
        .Selection.InlineShapes(1).Width = 156
Error is:
5941 The requested member of the collection does not exist

How do I select the newly added image? or

DougP, MCP, A+
[r2d2] I Built my own R2D2
I love this site and all you folks that helped me over the years!
 



Hi,

the implied reference object, which you never show, is probably a range.

The reference for an InlineShape collection is the document, so...
Code:
With ThisDocument
  With .InlineShapes(.InlineShapes.Count)
    .Height = 36
    .Width = 156
  End with
Error 
End with
will referenc the LAST inline shape added.

Skip,

[glasses] To be safe on the [red]FOURTH[/red],
Don't take a [red]FIFTH[/red] on the [red]THIRD[/red]
Or you might not come [red]FORTH[/red] on the [red]FIFTH[/red]
[red][highlight blue]FORTH[/highlight][/red][white][highlight red]WITH[/highlight][/white] [tongue]
 
Skip has the correct solution, although it CAN be done with Selection. Your problem was that you are still using Selection after you inserted the image file.

After an image insert, Selection does NOT contain the image. It is moved to the first character location past it. Therefore Selection.InlineShapes(1) does not exist. The alternative to Skip's answer (ie. using Selection) is to select the inserted image. Then of course Selection.InlineShapes(1) WILL exist. You do this by extending the Selection to the left - to the inserted image.
Code:
With Selection
   .InlineShapes.AddPicture FileName:= _
     "F:\Marketing\Graphics & Logos\OHC Blue Logo smaller2.jpg", _
     LinkToFile:=False, SaveWithDocument:=True
[COLOR=red]   .MoveLeft Unit:=wdCharacter, Count:=1, _
         Extend:=wdExtend[/color red]
   With .InlineShapes(1)
     .Height = 36
     .Width = 156
   End With
End With
Note that I used Selection, not .Selection, as indeed (as Skip notes) you do not mention your reference.

faq219-2884

Gerry
My paintings and sculpture
 
BTW: using Selection to insert an image is OK, but hopefully you are aware that it will replace the Selection. So if any content is actually selected (ie. Selection is NOT collapsed), it will be replaced by the inserted image.

Depending on how careful you are, and your requirements, you may want to consider checking first, and collapsing the Selection before inserting.

faq219-2884

Gerry
My paintings and sculpture
 
here is what I start with
this is in Access 2003
Code:
   'create reference to Word Object
   Dim objWord As Word.Application
   Set objWord = CreateObject("Word.Application")
   
   'Word Object is created - now let's fill it with data.
   With objWord
        
        .Visible = True
        .Documents.Add
         ....
         ....
        .Selection.InlineShapes.AddPicture FileName:= _
        "F:\Marketing\Graphics & Logos\OHC Blue Logo smaller2.jpg", LinkToFile:= _
        False, SaveWithDocument:=True
<<< next I want to size it to 36 x 156  >>>
 end with

DougP, MCP, A+
[r2d2] I Built my own R2D2
I love this site and all you folks that helped me over the years!
 
Use Skip's suggestion:
With .ActiveDocument.InlineShapes(.ActiveDocument.InlineShapes.Count)
.Height = 36
.Width = 156
End with

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Yep PHV that works great

I have the issue that if I don't add this
.Selection.EndKey Unit:=wdLine
it deletes whats just entered the .selection is writing over it.
I got my code from a creating a bunch of macro's in Word.
I create a Word macro then paste it in Access then fix what is wrong.
What's a better way? than .selection....

Code:
   'create reference to Word Object
   Dim objWord As Word.Application
   Set objWord = CreateObject("Word.Application")
   
   'Word Object is created - now let's fill it with data.
   With objWord
        
        .Visible = True
        '.Documents.Open (strDocPath)
        '.Documents.Open ("c:\PNG.dot")
        .Documents.Add
        
        Call MakeBorder(objWord)
        
        .Selection.TypeParagraph
        .Selection.TypeParagraph
        .Selection.TypeParagraph
        .Selection.Text = "Asbestos Survey"
        .Selection.Range.Case = wdUpperCase
        .Selection.Font.Size = 22
        .Selection.Font.Bold = wdToggle
        .Selection.Font.Italic = wdToggle
        .Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
        .Selection.EndKey Unit:=wdLine
        .Selection.TypeParagraph
        .Selection.TypeParagraph
        .Selection.TypeParagraph
        .Selection.TypeParagraph
        .Selection.Text = "PERFORMED AT"
        .Selection.Font.Size = 12
        .Selection.Font.Italic = wdToggle
        .Selection.EndKey Unit:=wdLine
...
...


DougP, MCP, A+
[r2d2] I Built my own R2D2
I love this site and all you folks that helped me over the years!
 
Sorry I did'nt give credit to fumei for pointing out the .selection

DougP, MCP, A+
[r2d2] I Built my own R2D2
I love this site and all you folks that helped me over the years!
 
Could you explain:
I have the issue that if I don't add this
.Selection.EndKey Unit:=wdLine
it deletes whats just entered the .selection is writing over it.
"Writing over it"?????

Eeeuuuu. All those .Selection instructions. Bleeech.

All this could be eased if you used Styles.

You have three .TypeParagraph instructions. Lets assume these are "Normal", therefore 12 pts.

You have an Uppercase instruction, a Size instruction, a Bold instruction, an Italics instruction, then inserting text, followed by four more TypeParagraph instructions.

Then, you have a text insertion instruction, a Size instruction, an Italics instruction.

Hmmmm. If this is to be used more than once, use a template with Styles.

I made a style (TekTips1) with a BeforeSpacing = 36 (3 x 12 pts) to match your three TypeParagraphs. It is set for Uppercase, 22 pts, Bold and Italics, with a SpacingAfter = 60 pts (your four .TypeParagraphs), and Centered.

I made a style (TekTips2) as Centered, 12 pts, Bold. Here is your code (partial) using Styles. I removed the Call MakeBorder instruction.
Code:
Sub WithStyle()
Dim objWord As Word.Application
Dim r As Word.Range
Set objWord = CreateObject("Word.Application")

With objWord
   .Visible = True
   .Documents.Add Template:="c:\test\tektips.dot"
   Set r = objWord.ActiveDocument.Range
   With r
      .Style = "TekTips1"
      .InsertAfter Text:="asbestos survey" & vbCrLf
      .Collapse Direction:=wdCollapseEnd
      .Style = "TekTips2"
      .InsertAfter Text:="performed at"
   End With
End With
' destroy objects yadda yadda yadda
End Sub
Note than there is no requirement that the text "abestos survey" has to be in any format, as THAT is covered by the Style.

I printed off two test documents - one using all your Selection.TypeParagraphs, and toggles etc. etc. etc. etc., and one using Styles from a template.

They are identical. Not a single use of Selection.

faq219-2884

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top