Macro works, but only if you 'step' through it - Word 2000


Joined
Dec 5, 2016
Messages
2
Reaction score
0
Can anyone tell me what is wrong with this code?
It loads a specific picture file, resizes it and sets it 'behind' text at the cursor point, just as it is meant to do but only if run under DEBUG, stepping through each line. If you just RUN it, it does nothing, but does not give an error.
I guess it has something to do with initialization or completion, but can't figure out what.

Sub pcn()
'
' pcn Macro
' Macro to insert signature 2016/11/20
'

Dim oDialog As Dialog
Dim oImage As Object
Dim oRng As Range
Set oImage = Selection.InlineShapes.AddPicture("D:\DOCUMENTS\PCN\pcnsig.png")
With oImage
.LockAspectRatio = msoTrue
.Height = 0.2 * .Height
.Width = 0.2 * .Width
Set oRng = oImage.Range
.ConvertToShape
End With
With oRng.ShapeRange(1)
.ZOrder msoSendBehindText
.WrapFormat.Type = wdWrapNone
End With
Set oDialog = Nothing
Set oImage = Nothing
Set oRng = Nothing

End Sub


The code from derived from examples culled from contributions elsewhere on this site. Thanks!
I am trying to run it on an ancient version of Word - 2000 under windows 10. It still works ok and I don't have problems with other versions of this macro.

PhillipN
 
Ad

Advertisements

macropod

Microsoft MVP
Joined
Mar 2, 2012
Messages
513
Reaction score
47
There doesn't appear to be anything inherently 'wrong' with your code, though it could be made more efficient. Try:

Code:
Sub pcn()
Dim iShp As InlineShape, Shp As Shape
Set iShp = Selection.InlineShapes.AddPicture("D:\DOCUMENTS\PCN\pcnsig.png")
With iShp
  .LockAspectRatio = msoTrue
  .Height = 0.2 * .Height
  Set Shp = .ConvertToShape
End With
With Shp
  .ZOrder msoSendBehindText
  .WrapFormat.Type = wdWrapNone
End With
Set iShp = Nothing: Set Shp = Nothing
End Sub
 
Ad

Advertisements

Joined
Dec 5, 2016
Messages
2
Reaction score
0
Ahhh! Thank you! That was quick!

And it works just dandy.
The only tweak is that on my old steam-driven version, it does need a ".Width = 0.2 * .Width" line as well, otherwise the "LockAspectRatio" isn't enough.

For anyone who wants to use this: the "0.2" factor in the Height / Width lines can be edited to suit the size of the png file image. Alternatively you could make the png image the right size and omit those two lines, but that would be less flexible.

Thanks again, macropod, for taking out the garbage. It looks and works MUCH better
P
 

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