+ Reply to Thread
Results 1 to 5 of 5

VBA Code Compile error: Sub undefined whenever run on a different PC

  1. #1
    Registered User
    Join Date
    08-24-2019
    Location
    England
    MS-Off Ver
    2016
    Posts
    8

    VBA Code Compile error: Sub undefined whenever run on a different PC

    Hello,

    I have an issue with a code that actually comes from the internet thatI want to use for my project.he projectI'm working on is a employee database with an image box on the right side. The code below is working when I'm running it at home however, when I finished it and about to present it to my boss and run the vba in my boss PC it's suddenly give me an error say: Compile error: Sub undefined whenever run on a different PC. Seems that both PC has the same references so I really need help on this. TIA.

    This is the code that having the issue:

    Public Sub prClipboardData2Image()
    Dim hCopy&: OpenClipboard 0&
    hCopy = CopyImage(GetClipboardData(2), 0, 0, 0, &H4)
    CloseClipboard
    If hCopy = 0 Then Exit Sub
    Const IPictureIID = "{7BF80981-BF32-101A-8BBB-00AA00300CAB}"
    Dim iPic As IPicture, tIID As GUID, tPICTDEST As PICTDESC, Ret&
    Ret = IIDFromString(StrConv(IPictureIID, vbUnicode), tIID)
    If Ret Then Exit Sub
    With tPICTDEST
    .cbSize = Len(tPICTDEST)
    .picType = 1
    .hImage = hCopy
    End With
    Ret = OleCreatePictureIndirect(tPICTDEST, tIID, 1, iPic)
    If Ret Then Exit Sub

    frmEmpDetails.imgEmp.Picture = LoadPicture("")
    frmEmpDetails.imgEmp.Picture = iPic

    Set iPic = Nothing
    End Sub

  2. #2
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: VBA Code Compile error: Sub undefined whenever run on a different PC

    Welcome to the forum! Please paste code between code tags. Click the # icon on toolbar to insert the tags.

    As posted, you left out the API routines.

    Even if both have the code, one may be 32bit Excel and the other 64bit Excel. API code can be coded to handled both cases sometimes.
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    08-24-2019
    Location
    England
    MS-Off Ver
    2016
    Posts
    8
    Quote Originally Posted by Kenneth Hobson View Post
    Welcome to the forum! Please paste code between code tags. Click the # icon on toolbar to insert the tags.

    As posted, you left out the API routines.

    Even if both have the code, one may be 32bit Excel and the other 64bit Excel. API code can be coded to handled both cases sometimes.
    Please Login or Register  to view this content.
    Well, I hope this is it. I saw some code that has 32bit maybe if I changed it to 64 bit or with this code everything will works fine. Thank you. Will update you after if I can make it work.

  4. #4
    Registered User
    Join Date
    08-24-2019
    Location
    England
    MS-Off Ver
    2016
    Posts
    8

    Re: VBA Code Compile error: Sub undefined whenever run on a different PC

    Hi Kenneth,

    It seems that code didn't work either. Here's my full code in the module that having the issue:

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ''''''''''''''''''''''''''''''''''''''''''''''' OM ''''''''''''''''''''''''''''''''''''''''''''''
    ''
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Option Explicit
    Private Type GUID
    Data1 As Long
    Data2 As Integer
    Data3 As Integer
    Data4(8) As Byte
    End Type

    Private Type PICTDESC
    cbSize As Long
    picType As Long
    hImage As Long
    End Type
    #If VBA7 And Win64 Then
    Private Declare PtrSafe Function OpenClipboard& Lib _
    "user32" (ByVal hwnd&)

    Private Declare PtrSafe Function EmptyClipboard& Lib "user32" ()
    Private Declare PtrSafe Function GetClipboardData& Lib _
    "user32" (ByVal wFormat%)
    Private Declare PtrSafe Function SetClipboardData& Lib _
    "user32" (ByVal wFormat&, ByVal hMem&)
    Private Declare PtrSafe Function CloseClipboard& Lib "user32" ()
    Private Declare PtrSafe Function CopyImage& Lib "user32" (ByVal handle& _
    , ByVal un1&, ByVal n1&, ByVal n2&, ByVal un2&)
    Private Declare PtrSafe Function IIDFromString Lib "ole32" _
    (ByVal lpsz As String, ByRef lpiid As GUID) As Long
    ' Private Declare PtrSafe Function OleCreatePictureIndirect Lib "olepro32" _
    ' (pPictDesc As PICTDESC, ByRef riid As GUID _
    ' , ByVal fOwn As Long, ByRef ppvObj As IPicture) As Long

    Private Declare PtrSafe Function OleCreatePictureIndirect Lib "oleaut32" _
    (pPictDesc As PICTDESC, ByRef riid As GUID _
    , ByVal fOwn As Long, ByRef ppvObj As IPicture) As Long

    #Else
    ' Private Declare Function OpenClipboard& Lib _
    ' "user32" (ByVal hwnd&)
    ' Private Declare Function EmptyClipboard& Lib "user32" ()
    ' Private Declare Function GetClipboardData& Lib _
    ' "user32" (ByVal wFormat%)
    ' Private Declare Function SetClipboardData& Lib _
    ' "user32" (ByVal wFormat&, ByVal hMem&)
    ' Private Declare Function CloseClipboard& Lib "user32" ()
    ' Private Declare Function CopyImage& Lib "user32" (ByVal handle& _
    ' , ByVal un1&, ByVal n1&, ByVal n2&, ByVal un2&)
    ' Private Declare Function IIDFromString Lib "ole32" _
    ' (ByVal lpsz As String, ByRef lpiid As GUID) As Long
    ' Private Declare Function OleCreatePictureIndirect Lib "olepro32" _
    ' (pPictDesc As PICTDESC, ByRef riid As GUID _
    ' , ByVal fOwn As Long, ByRef ppvObj As IPicture) As Long
    ' Private Declare Function DestroyIcon& Lib "user32" (ByVal hIcon&)
    #End If



    Public Sub prImage2Print()
    Dim hCopy&: OpenClipboard 0&
    hCopy = CopyImage(GetClipboardData(2), 0, 0, 0, &H4)
    CloseClipboard
    If hCopy = 0 Then Exit Sub
    Const IPictureIID = "{7BF80981-BF32-101A-8BBB-00AA00300CAB}"
    Dim iPic As IPicture, tIID As GUID, tPICTDEST As PICTDESC, Ret&
    Ret = IIDFromString(StrConv(IPictureIID, vbUnicode), tIID)
    If Ret Then Exit Sub
    With tPICTDEST
    .cbSize = Len(tPICTDEST)
    .picType = 1
    .hImage = hCopy
    End With
    Ret = OleCreatePictureIndirect(tPICTDEST, tIID, 1, iPic)
    If Ret Then Exit Sub
    SavePicture iPic, prTmpPath & "outputImage.jpg"

    Set iPic = Nothing
    End Sub

    Public Sub prClipboardData2Image()
    Dim hCopy&: OpenClipboard 0&
    hCopy = CopyImage(GetClipboardData(2), 0, 0, 0, &H4)
    CloseClipboard
    If hCopy = 0 Then Exit Sub
    Const IPictureIID = "{7BF80981-BF32-101A-8BBB-00AA00300CAB}"
    Dim iPic As IPicture, tIID As GUID, tPICTDEST As PICTDESC, Ret&
    Ret = IIDFromString(StrConv(IPictureIID, vbUnicode), tIID)
    If Ret Then Exit Sub
    With tPICTDEST
    .cbSize = Len(tPICTDEST)
    .picType = 1
    .hImage = hCopy
    End With
    Ret = OleCreatePictureIndirect(tPICTDEST, tIID, 1, iPic)
    If Ret Then Exit Sub

    frmEmpDetails.imgEmp.Picture = LoadPicture("")
    frmEmpDetails.imgEmp.Picture = iPic

    Set iPic = Nothing
    End Sub

  5. #5
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: VBA Code Compile error: Sub undefined whenever run on a different PC

    Run the code that I posted on both machines. Then you will know if Excel bit version is an issue.

    In the #4 code, there is probably no reason to comment out the routines. The purpose of #If in #4 is to let either version work. e.g. OleCreatePictureIndirect was called in #1 but both bit versions were commented out in #4. A VBE > Debug > Compile should have showed that issue though.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. VBA code getting Compile error:
    By UPA in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-22-2017, 07:34 AM
  2. [SOLVED] code compile error
    By Burt_100 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-09-2015, 03:43 AM
  3. [SOLVED] Compile Error VBA error code attached
    By cgibson92 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 06-01-2015, 11:07 AM
  4. code generating compile error: syntex error
    By sankarsasmal in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-10-2013, 05:29 AM
  5. [SOLVED] Compile Error in Hidden Module and Compile Error: Can't find project or library
    By Taislin in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-10-2013, 07:03 PM
  6. Compile Error in VBA Code
    By andrewc in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-22-2011, 06:49 AM
  7. VB code - compile error box help!!!
    By excel.xls in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-10-2007, 10:15 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1