+ Reply to Thread
Results 1 to 10 of 10

Use a Variable to Retun an Element from a Type Array

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-25-2011
    Location
    Virginia
    MS-Off Ver
    Excel 2016
    Posts
    166

    Re: Use a Variable to Retun an Element from a Type Array

    Rory,
    It's really a long story (and several hundred lines of code) but I am trying to set a dozen or so extended printer settings (like NUP) which requires manipulating DEVMODE types. In my code there are 5 different DEVMODES based on Universal vs. PostScript and also Windows versions. Snippet of the code (reduced DEVMODES to 3 and features to 3 for ease of viewing) looks like this:
    Option Base 1
    Option Explicit
    
    Private Type DEVMODE_A
      'many more elements
      dmOrientation As Integer
      dmPaperSize As Integer
      'many more elements
      dmFormName As String * 32
      'many more elements
    End Type
    Private Type DEVMODE_B
      'many more elements
      dmOrientation As Integer
      dmPaperSize As Integer
      'many more elements
      dmFormName As String * 32
      'many more elements
    End Type
    Private Type DEVMODE_C
      'many more elements
      dmOrientation As Integer
      dmPaperSize As Integer
      'many more elements
      dmFormName As String * 32
      'many more elements
    End Type
    Type TT
      lTest As Long
    End Type
    Sub SetDevModebyType(Feature As Long, Setting As Variant)
    Dim DMType As Long
    Dim DEVMODE_A As DEVMODE_A
    Dim DEVMODE_B As DEVMODE_B
    Dim DEVMODE_C As DEVMODE_C
    Dim lRet As Long
    
    DMType = GetDevmodeType 'Returns 1,2 or 3
    
    'Fill the correct DEVMODE based on DMType
    Select Case DMType
      Case 1
        DEVMODE_A = GetDevModeA
      Case 2
        DEVMODE_B = GetDevModeB
      Case 3
        DEVMODE_C = GetDevModeC
     End Select
    
    'Apply the setting for the Feature in the right DEVMODE
    
    Select Case Feature
    Case DM_ORIENTATION
      Select Case DMType
        Case 1
          DEVMODE_A.dmOrientation = Setting
        Case 2
          DEVMODE_B.dmOrientation = Setting
        Case 3
          DEVMODE_C.dmOrientation = Setting
      End Select
    Case DM_PAPERSIZE
      Select Case DMType
        Case 1
          DEVMODE_A.dmPaperSize = Setting
        Case 2
          DEVMODE_B.dmPaperSize = Setting
        Case 3
          DEVMODE_C.dmPaperSize = Setting
      End Select
    Case DM_FORMNAME
      Select Case DMType
        Case 1
          DEVMODE_A.dmFormName = Setting
        Case 2
          DEVMODE_B.dmFormName = Setting
        Case 3
          DEVMODE_C.dmFormName = Setting
      End Select
    End Select
    
    Select Case DMType
      Case 1
        Call CopyMemory(yDevModeArr(1), DEVMODE_A, Len(DEVMODE_A))
      Case 2
        Call CopyMemory(yDevModeArr(1), DEVMODE_B, Len(DEVMODE_B))
      Case 3
        Call CopyMemory(yDevModeArr(1), DEVMODE_C, Len(DEVMODE_C))
    End Select
    
    End Sub
    While this method works, what I am trying to avoid are the interior cases which have to be repeated for each feature. I was hoping there was a way (such as evaluate and replace) to replace each of these cases with a single line that would set the feature to the correct DEVMODE.

    I had previously tried your type within a type method but that doesn't help with this particular issue.

    Note: I played around with conditional compiling but that won't work as I don't know the printer driver type (and thus DEVMODE type) until well into program execution.

    Thanks,
    Last edited by tom.hogan; 05-21-2014 at 02:07 PM.
    Tom

    If my answer resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post
    .

+ 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. Taking an element in array (variant type) in Excel VBA
    By blackarrow in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-16-2013, 01:45 PM
  2. Error passing element of string array get ByRef argument type mismatch
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-17-2011, 02:59 PM
  3. we type data on one page and retun to sevral pages
    By Music master in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-17-2009, 08:01 AM
  4. Array problem: Key words-Variant Array, single-element, type mismatch error
    By davidm in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-09-2005, 01:54 AM
  5. Array problem: Key words-Variant Array, single-element, type mismatch error
    By davidm in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-08-2005, 12:30 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