Focus plan to import vba image

Hello everyone, here I have made a piece of code integrated into my macro to insert a jpg image in a solidworks plan. The problem is that I select the plan that is already open with opendoc6 but it doesn't work. When I have the plan open it works but if it's not on the main window it doesn't work anymore.

I specify that at this point in the macro the plan will already be open, but the focus is on a .part, the plan is open but not selected.

Here is the function that should insert the barcode:

Function codebarreDRAW(FCB As String, namePL As String, chemin As String)
Dim myModelView As Object
Dim swModel As ModelDoc2
Dim swApp As SldWorks.SldWorks
Dim swLoadErrors As Long
Dim swLoadWarnings As Long
Dim Fplan As String

Fplan = namePL & ".SLDDRW"

Set swApp = Application.SldWorks
Set Part = swApp.ActiveDoc
Set myModelView = Part.ActiveView
Set swModel = swApp.OpenDoc6(Fplan, swDocDRAWING, swOpenDocOptions_Silent, "", swLoadErrors, swLoadWarnings)
myModelView.FrameState = swWindowState_e.swWindowMaximized
Dim SkPicture As Object
Set SkPicture = Part.SketchManager.InsertSketchPicture(FCB)
SkPicture.SetSize 130 / 1000, 20 / 1000, True
SkPicture.SetOrigin 110 / 1000, 35 / 1000
End Function

 

I have an error on the Skpicture.setsize line

The worst part is that the InsertSketchPicture line doesn't crash but the image isn't imported.

If you have any ideas, I'm all for it.

Thank you!

I managed to solve one of the problems, in fact instead of using opendoc6 I use activatedoc3 and it puts me on the plan window but on the other hand I still have the error on the line:

SkPicture.SetSize 130/1000, 20/1000, True

Error 91 Variable Object or with a Block Variable That Is Not Defined

So I guess the Skpicture didn't initialize well

Ok it's solved, I just put the set part before the activedoc3 so it couldn't work! I put you the function to insert into an open plan.

 

To be reworked as needed, especially at the level of setsize and set origin,

 

fcb is the image file, and namepl is the name of the plan to be entered as input in the function.

 

Function codebarreDRAW(FCB As String, namePL As String)
Dim myModelView As Object
Dim swModel As ModelDoc2
Dim swApp As SldWorks.SldWorks
Dim Errors As Long
Dim swLoadWarnings As Long
Dim Fplan As String

Fplan = namePL & ".SLDDRW"

Set swApp = Application.SldWorks
Set swModel = swApp.ActivateDoc3(Fplan, False, swRebuildOnActivation_e.swRebuildActiveDoc, Errors)
Set Part = swApp.ActiveDoc
Dim SkPicture As Object
Set SkPicture = Part.SketchManager.InsertSketchPicture(FCB)
SkPicture.SetSize 130 / 1000, 20 / 1000, True
SkPicture.SetOrigin 110 / 1000, 35 / 1000
End Function