Insert image macro

Hello,

I have a macro that inserts an image into a document at an absolute position. This works really well.modified it so if the document is protected it unprotects and then add the image.
I am able to create docs, add form fields, protect doc and then use the macro to insert the image. A number of pre-existing documents crash for some reason when I try and run the macro.
Can you please have a look and try and see why this doc attached crashes when i try and run the macro - code below Arrears-1.dot
Sub InsertPictureShape()
Dim sh As Shape
With ActiveDocument
    ProtectType = .ProtectionType
    If Not (ProtectType = wdNoProtection) Then
      .Unprotect
    End If
  Set sh = .Shapes.AddPicture("C:\image\sbs.png", False, True, , , , Selection.Paragraphs(1).Range)
    sh.RelativeHorizontalPosition = wdRelativeHorizontalPositionLeftMarginArea
    sh.RelativeVerticalPosition = wdRelativeVerticalPositionTopMarginArea
    sh.Top = CentimetersToPoints(0.8)
    sh.Left = CentimetersToPoints(12.2)
    
    If Not (ProtectType = wdNoProtection) Then
      .Protect ProtectType, True
    End If
    End With
    
End Sub

Open in new window

sbsbatsAsked:
Who is Participating?
 
GrahamSkanConnect With a Mentor RetiredCommented:
Sorry. No 2010. 2007 ate enough into my pension.

I have seen the symptom before. Somewhere, internally, a value is actually out of range, but it isn't needed anyway.

Apart from  creating a new document from the template in the way that I said, I can also create a new document by copying the open document and pasting to a new blank one. Either way, the new document doesn't have the problem.
0
 
HainKurtSr. System AnalystCommented:
which line? whats the error message?
0
 
GrahamSkanRetiredCommented:
I created a new document form the template under Word 2007, and the macro worked without a problem.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
sbsbatsAuthor Commented:
LIne 8
"One of the values passed to this method or property is out of range"
0
 
sbsbatsAuthor Commented:
HI Graham,

Haven't tried 2007 - doc created in 2003 and being used in 2010.
Will try converting and let you know
0
 
GrahamSkanRetiredCommented:
I can reproduce that problem if I open the template directly (instead of creating a new document from it).
0
 
sbsbatsAuthor Commented:
Thats strange - Let me test again but I open the old fashion way - file - new- templates.
0
 
sbsbatsAuthor Commented:
Nope - it is still crashing when I try and run the doc - am double clicking and creatig the .doc for sure.
Do you have a copy of 2010 to test?
0
 
sbsbatsAuthor Commented:
Hi Graham,

Not really an option considering how many doc's I want to use this with - makes sense what you say about error as image does get placed on doc, just with the error - also it is placed on the left hand side of the page where the curser would start from instead of its absolute position.
0
 
sbsbatsAuthor Commented:
Looks like I may need some help from an Office 2010 expert - the problem seems to be related to the fact its a template and not a doc.
When I re-create this doc and save it as a doc and run it no problems - when i save it as a 97-2003 template it causes this crash.
Any help from an Office expert please?
0
 
sbsbatsAuthor Commented:
compatibility issue alright
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.