Question

Need help with Solidworks API's in VBA

Asked by: dsoderstrom

This may be the wrong forum for this question but maybe someone out there has had experience with this.  I'm trying to develop a program using the Solidworks API's within VBA  to extract Bill of Material information from a Solidworks CAD database.  Has anyone had experience with this?  I know the information is stored because it can be exported from within Solidworks.  All I need to retrieve is Parent, Component and Qty Per.  Sample code would be great!

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2008-04-08 at 08:31:52ID23305030
Tags

VBA

Topics

SolidWorks CAD Software

,

Microsoft Access Database

Participating Experts
1
Points
0
Comments
4

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. API's?
    What are win32 API's? and how do they operate in visual basic 4 upwards? what are there purpose?
  2. How to use F2, F3 ... with VBA (API'S ???)
    I am trying to assign a function to a function key (F2) using VBA. How can I do it ??? Is there a Windows API that help me in this case ??? vantive.
  3. Winsock API in VBA
    Hi, In VBA I need to use the Winsock API to connect to an IP address (IP number is known, so no resolving, just straight connect), send a textstring and receive a textstring (4K) in return. That's it. I have seen lot's of Api stuff on this site, of which I don't need the mo...
  4. VBA module running an API
    I have a VBA module which downloads a file using FTPGETFILE. When the transfer is in progress the screen is open to repaint problems. How do I run the API so that windows still has control over the screen.
  5. TwipsPerPixelX/Y via the API (for VBA)?
    Is it possible to get the TwipsPerPixelX and TwipsPerPixelY via an API call? VBA (for PowerPoint) does not have the Screen object, so I can't get them that way. I am having a problem sizing my forms in VBA correctly and I think that the whole twip v.s. pixel thing might be ...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: wildboy85Posted on 2008-04-08 at 10:39:43ID: 21307647

If you can't select the bill of material in solidwork, this wont be of any use to you
but in our case the bill of material have to be on the drawing (i think it's the case for everyone)

here is my plan about this kind of data transfert: (excel to solidwork)
part 1: generate a HTML table with excel selection (in your case solidwork selection)
(not done yet)
part 2: put the table in windows clipboard in html format
part 3: past it in solidwork (in your case in something else) as a clean cool html table

only part 2 is done for me right now
but if you are interested in the copy past method i will post my futur code here

it will be reversed, (excel to solidwork) but your skill being advanced, i think you can reverse it back to solidwork selection to clipboard html table

i am coding this stuff right now, so it should be 2or 3 days before i post the whole copy past as html table if you are interested

Option Explicit
 
'=== 2008-03-11 9:35 SEF
 
' --------------------------------------------------------------------
Private Const sMODULE_NAME As String = "clsClipboard"
' --------------------------------------------------------------------
'   VBA doesn't provide any method to get or set data in the Windows
'   Clipboard.  This class provides some basic methods for Clipboard
'   operation.
' --------------------------------------------------------------------
'   Notice:
'   This example code is provided as-is by LA Solutions Ltd with no
'   warranty of fitness for purpose and with no support.  This
'   example is derived from published information found at this URL:
'   http://vb.mvps.org/articles/ap200106.asp
'
'   You are free to use and adapt this code for personal or commercial use
'   provided that this notice is retained in full
'   End of notice
' --------------------------------------------------------------------
' Clipboard Manager Functions
' --------------------------------------------------------------------
Private Declare Function OpenClipboard Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function RegisterClipboardFormat Lib "user32" Alias "RegisterClipboardFormatA" (ByVal lpString As String) As Long
Private Declare Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As Long) As Long
Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long
Private Declare Function EmptyClipboard Lib "user32" () As Long
Private Declare Function IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Long) As Long
Private Declare Function GetPriorityClipboardFormat Lib "user32" (lpPriorityList As Long, ByVal nCount As Long) As Long
' --------------------------------------------------------------------
' Other useful APIs
' --------------------------------------------------------------------
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pTo As Any, uFrom As Any, ByVal lSize As Long)
Private Declare Function lstrlenA Lib "kernel32" (ByVal lpString As Long) As Long
' --------------------------------------------------------------------
Private Const GMEM_FIXED                    As Long = &H0
' --------------------------------------------------------------------
' Predefined Clipboard Formats
' --------------------------------------------------------------------
Private Const CF_TEXT                       As Long = 1
Private Const CF_BITMAP                     As Long = 2
Private Const CF_METAFILEPICT               As Long = 3
Private Const CF_SYLK                       As Long = 4
Private Const CF_DIF                        As Long = 5
Private Const CF_TIFF                       As Long = 6
Private Const CF_OEMTEXT                    As Long = 7
Private Const CF_DIB                        As Long = 8
Private Const CF_PALETTE                    As Long = 9
Private Const CF_PENDATA                    As Long = 10
Private Const CF_RIFF                       As Long = 11
Private Const CF_WAVE                       As Long = 12
Private Const CF_UNICODETEXT                As Long = 13
Private Const CF_ENHMETAFILE                As Long = 14
Private Const CF_HDROP                      As Long = 15
Private Const CF_LOCALE                     As Long = 16
Private Const CF_MAX                        As Long = 17
Private Const CF_OWNERDISPLAY               As Long = &H80
Private Const CF_DSPTEXT                    As Long = &H81
Private Const CF_DSPBITMAP                  As Long = &H82
Private Const CF_DSPMETAFILEPICT            As Long = &H83
Private Const CF_DSPENHMETAFILE             As Long = &H8E
Const RegHtml As String = "HTML Format"
 
'=== end clipboard stuff
 
 
' --------------------------------------------------------------------
'  Public Methods
' --------------------------------------------------------------------
Public Function GetFormat(ByVal Format As Long) As Boolean
   ' Check if the requested format is available
   ' on the clipboard. (Same behavior as standard
   ' VB Clipboard object)
   If OpenClipboard(0&) Then
      If IsClipboardFormatAvailable(Format) Then
         GetFormat = True
      End If
      Call CloseClipboard
   End If
End Function
' --------------------------------------------------------------------
Public Function GetPriorityFormat(ParamArray Formats()) As Long
   Dim Fmts() As Long
   Dim i As Long
   Dim nFmt As Long
 
   ' Bail, if no formats were requested
   If UBound(Formats) < 0 Then Exit Function
 
   ' Transfer desired formats into a non-variant array
   ReDim Fmts(0 To UBound(Formats)) As Long
   For i = 0 To UBound(Formats)
      ' Double conversion, to be safer.
      ' Could error trap, but that'd mean the
      ' user was a hoser, and we wouldn't want
      ' to insinuate *that*, would we?
      Fmts(i) = CLng(Val(Formats(i)))
   Next i
 
   ' Try opening clipboard...
   If OpenClipboard(0&) Then
      ' Check to see which format is highest in list
      nFmt = GetPriorityClipboardFormat(Fmts(0), UBound(Fmts) + 1)
      Call CloseClipboard
   Else
      ' Clipboard may already be open by another
      ' routine in same process, try anyway to see
      ' if we can get a successful result.  Not
      ' clean, but worth a shot
      nFmt = GetPriorityClipboardFormat(Fmts(0), UBound(Fmts) + 1)
   End If
 
   ' Return results
   GetPriorityFormat = nFmt
End Function
' --------------------------------------------------------------------
Public Function GetText() As String
   Dim nFmt As Long
   Dim hData As Long
   Dim lpData As Long
 
   ' Check for desired format
   'nFmt = Me.GetPriorityFormat(CF_TEXT, CF_OEMTEXT, CF_DSPTEXT)
   nFmt = GetPriorityFormat(CF_TEXT, CF_OEMTEXT, CF_DSPTEXT)
 
   ' -1=None requested, 0=Empty
   If nFmt > 0 Then
      ' Grab text from clipboard, if available
      If OpenClipboard(0&) Then
         hData = GetClipboardData(nFmt)
         ' Slurp characters from global memory
         If hData Then
            lpData = GlobalLock(hData)
               GetText = PointerToStringA(lpData)
            Call GlobalUnlock(hData)
         End If
         Call CloseClipboard
      End If
   End If
End Function
 
' --------------------------------------------------------------------
Public Function SetText(ByVal NewVal As String) As Boolean
   
   Dim hData As Long
   Dim lpData As Long
   Dim Buffer() As Byte
 
   '=== Try to set text onto clipboard
   If OpenClipboard(0&) Then
      Call EmptyClipboard
      '=== Convert data to ANSI byte array
      Buffer = StrConv(NewVal & vbNullChar, vbFromUnicode)
      '=== Allocate enough memory for buffer
      hData = GlobalAlloc(GMEM_FIXED, UBound(Buffer) + 1)
      If hData Then
         '=== Copy data to alloc'd memory
         lpData = GlobalLock(hData)
         Call CopyMemory(ByVal lpData, Buffer(0), UBound(Buffer) + 1)
         Call GlobalUnlock(hData)
         '=== Hand data off to clipboard
         SetText = CBool(SetClipboardData(CF_TEXT, hData))
      End If
      Call CloseClipboard
   End If
End Function
Public Function SetHtml(ByVal NewVal As String) As Boolean
   
   Dim hData As Long
   Dim lpData As Long
   Dim Buffer() As Byte
   Dim CF_HTML As Long
   Dim n As String
   Dim o As Variant
   Dim p As Variant
   Dim q As Variant
   Dim r As String
   Dim i As Integer
   Dim s As String
   
'=== replace all special caracters 128+ ascii with a code for html code
i = 1
While i < Len(NewVal)
   s = Mid(NewVal, i, 1)
   r = Asc(s)
   If r > 128 Then
      NewVal = Replace(NewVal, s, "&#" & Trim(CStr(r)) & ";")
      i = i + 3 + Len(Trim(CStr(r)))
   Else
      i = i + 1
   End If
Wend
 
'=== build html structure for clipboard
n = "Version:0.9" & vbCrLf
n = n & "StartHTML:00000000" & vbCrLf
n = n & "EndHTML:00000000" & vbCrLf
n = n & "StartFragment:00000000" & vbCrLf
n = n & "EndFragment:00000000" & vbCrLf
n = n & "StartSelection:00000000" & vbCrLf
n = n & "EndSelection:00000000" & vbCrLf
n = n & "<html><body>" & vbCrLf
n = n & "<!--StartFragment-->" & vbCrLf
n = n & NewVal & vbCrLf
n = n & "<!--EndFragment-->" & vbCrLf
n = n & "</BODY></HTML>" & vbCrLf
 
'Version: vv version number of the clipboard. Starting version is 0.9.
'StartHTML: bytecount from the beginning of the clipboard to the start of the context, or -1 if no context.
'EndHTML: bytecount from the beginning of the clipboard to the end of the context, or -1 if no context.
'StartFragment: bytecount from the beginning of the clipboard to the start of the fragment.
'EndFragment: bytecount from the beginning of the clipboard to the end of the fragment.
'StartSelection: bytecount from the beginning of the clipboard to the start of the selection.
'EndSelection: bytecount from the beginning of the clipboard to the end of the selection.
 
'=== once the string is done, we can chek where are the chekpoints
'=== then write it in the string itself, padding with "0"
q = "<html>"
p = Trim(CStr(InStr(LCase(n), q) - 1))
o = String(8 - Len(p), "0") & p
n = Replace(n, "StartHTML:00000000", "StartHTML:" & o, 1, 1)
 
q = ""
p = Trim(CStr(Len(n)))
o = String(8 - Len(p), "0") & p
n = Replace(n, "EndHTML:00000000", "EndHTML:" & o, 1, 1)
 
q = "<!--startfragment-->"
p = Trim(CStr(InStr(LCase(n), q) + Len(q) - 1))
o = String(8 - Len(p), "0") & p
n = Replace(n, "StartFragment:00000000", "StartFragment:" & o, 1, 1)
 
q = "<!--endfragment-->"
p = Trim(CStr(InStr(LCase(n), q) - 1))
o = String(8 - Len(p), "0") & p
n = Replace(n, "EndFragment:00000000", "EndFragment:" & o, 1, 1)
   
   '=== Try to set text onto clipboard
   If OpenClipboard(0&) Then
      Call EmptyClipboard
      '=== Convert data to ANSI byte array
      Buffer = StrConv(n & vbNullChar, vbFromUnicode)
      '=== Allocate enough memory for buffer
      hData = GlobalAlloc(GMEM_FIXED, UBound(Buffer) + 1)
      If hData Then
         '=== Copy data to alloc'd memory
         lpData = GlobalLock(hData)
         Call CopyMemory(ByVal lpData, Buffer(0), UBound(Buffer) + 1)
         Call GlobalUnlock(hData)
         '=== Hand data off to clipboard
         CF_HTML = RegisterClipboardFormat(RegHtml)
         'MsgBox (CF_HTML)
         SetHtml = CBool(SetClipboardData(CF_HTML, hData))
      End If
      Call CloseClipboard
   End If
End Function
 
' --------------------------------------------------------------------
'  Private Methods
' --------------------------------------------------------------------
Private Function PointerToStringA(ByVal lpStringA As Long) As String
   Dim Buffer() As Byte
   Dim nLen As Long
 
   If lpStringA Then
      nLen = lstrlenA(ByVal lpStringA)
      If nLen Then
         ReDim Buffer(0 To (nLen - 1)) As Byte
         CopyMemory Buffer(0), ByVal lpStringA, nLen
         PointerToStringA = StrConv(Buffer, vbUnicode)
      End If
   End If
End Function
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:

Select allOpen in new window

 

by: wildboy85Posted on 2008-04-08 at 12:55:53ID: 21308960

Right now i can copy from excel and paste in solidwork a html table
i know this is the reverse solution of what you search for

but you might be able ot use this macro in solidwork to copy a selection of a material bill in windows clipboard, then paste it where you want it as a html table

in the code snippet, chek the last sub, its what you should execute with a "copy" button
Sub sel_to_htmcli()
(excel selection to html format windows clipboard)

Option Explicit
 
'=== 2008-03-11 9:35 SEF
 
' --------------------------------------------------------------------
Private Const sMODULE_NAME As String = "clsClipboard"
' --------------------------------------------------------------------
'   VBA doesn't provide any method to get or set data in the Windows
'   Clipboard.  This class provides some basic methods for Clipboard
'   operation.
' --------------------------------------------------------------------
'   Notice:
'   This example code is provided as-is by LA Solutions Ltd with no
'   warranty of fitness for purpose and with no support.  This
'   example is derived from published information found at this URL:
'   http://vb.mvps.org/articles/ap200106.asp
'
'   You are free to use and adapt this code for personal or commercial use
'   provided that this notice is retained in full
'   End of notice
' --------------------------------------------------------------------
' Clipboard Manager Functions
' --------------------------------------------------------------------
Private Declare Function OpenClipboard Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function RegisterClipboardFormat Lib "user32" Alias "RegisterClipboardFormatA" (ByVal lpString As String) As Long
Private Declare Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As Long) As Long
Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long
Private Declare Function EmptyClipboard Lib "user32" () As Long
Private Declare Function IsClipboardFormatAvailable Lib "user32" (ByVal wFormat As Long) As Long
Private Declare Function GetPriorityClipboardFormat Lib "user32" (lpPriorityList As Long, ByVal nCount As Long) As Long
' --------------------------------------------------------------------
' Other useful APIs
' --------------------------------------------------------------------
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pTo As Any, uFrom As Any, ByVal lSize As Long)
Private Declare Function lstrlenA Lib "kernel32" (ByVal lpString As Long) As Long
' --------------------------------------------------------------------
Private Const GMEM_FIXED                    As Long = &H0
' --------------------------------------------------------------------
' Predefined Clipboard Formats
' --------------------------------------------------------------------
Private Const CF_TEXT                       As Long = 1
Private Const CF_BITMAP                     As Long = 2
Private Const CF_METAFILEPICT               As Long = 3
Private Const CF_SYLK                       As Long = 4
Private Const CF_DIF                        As Long = 5
Private Const CF_TIFF                       As Long = 6
Private Const CF_OEMTEXT                    As Long = 7
Private Const CF_DIB                        As Long = 8
Private Const CF_PALETTE                    As Long = 9
Private Const CF_PENDATA                    As Long = 10
Private Const CF_RIFF                       As Long = 11
Private Const CF_WAVE                       As Long = 12
Private Const CF_UNICODETEXT                As Long = 13
Private Const CF_ENHMETAFILE                As Long = 14
Private Const CF_HDROP                      As Long = 15
Private Const CF_LOCALE                     As Long = 16
Private Const CF_MAX                        As Long = 17
Private Const CF_OWNERDISPLAY               As Long = &H80
Private Const CF_DSPTEXT                    As Long = &H81
Private Const CF_DSPBITMAP                  As Long = &H82
Private Const CF_DSPMETAFILEPICT            As Long = &H83
Private Const CF_DSPENHMETAFILE             As Long = &H8E
Const RegHtml As String = "HTML Format"
 
'=== end clipboard stuff
 
' --------------------------------------------------------------------
'  Public Methods
' --------------------------------------------------------------------
Public Function GetFormat(ByVal Format As Long) As Boolean
   ' Check if the requested format is available
   ' on the clipboard. (Same behavior as standard
   ' VB Clipboard object)
   If OpenClipboard(0&) Then
      If IsClipboardFormatAvailable(Format) Then
         GetFormat = True
      End If
      Call CloseClipboard
   End If
End Function
' --------------------------------------------------------------------
Public Function GetPriorityFormat(ParamArray Formats()) As Long
   Dim Fmts() As Long
   Dim i As Long
   Dim nFmt As Long
 
   ' Bail, if no formats were requested
   If UBound(Formats) < 0 Then Exit Function
 
   ' Transfer desired formats into a non-variant array
   ReDim Fmts(0 To UBound(Formats)) As Long
   For i = 0 To UBound(Formats)
      ' Double conversion, to be safer.
      ' Could error trap, but that'd mean the
      ' user was a hoser, and we wouldn't want
      ' to insinuate *that*, would we?
      Fmts(i) = CLng(Val(Formats(i)))
   Next i
 
   ' Try opening clipboard...
   If OpenClipboard(0&) Then
      ' Check to see which format is highest in list
      nFmt = GetPriorityClipboardFormat(Fmts(0), UBound(Fmts) + 1)
      Call CloseClipboard
   Else
      ' Clipboard may already be open by another
      ' routine in same process, try anyway to see
      ' if we can get a successful result.  Not
      ' clean, but worth a shot
      nFmt = GetPriorityClipboardFormat(Fmts(0), UBound(Fmts) + 1)
   End If
 
   ' Return results
   GetPriorityFormat = nFmt
End Function
' --------------------------------------------------------------------
Public Function GetText() As String
   Dim nFmt As Long
   Dim hData As Long
   Dim lpData As Long
 
   ' Check for desired format
   'nFmt = Me.GetPriorityFormat(CF_TEXT, CF_OEMTEXT, CF_DSPTEXT)
   nFmt = GetPriorityFormat(CF_TEXT, CF_OEMTEXT, CF_DSPTEXT)
 
   ' -1=None requested, 0=Empty
   If nFmt > 0 Then
      ' Grab text from clipboard, if available
      If OpenClipboard(0&) Then
         hData = GetClipboardData(nFmt)
         ' Slurp characters from global memory
         If hData Then
            lpData = GlobalLock(hData)
               GetText = PointerToStringA(lpData)
            Call GlobalUnlock(hData)
         End If
         Call CloseClipboard
      End If
   End If
End Function
 
' --------------------------------------------------------------------
Public Function SetText(ByVal NewVal As String) As Boolean
   
   Dim hData As Long
   Dim lpData As Long
   Dim Buffer() As Byte
 
   '=== Try to set text onto clipboard
   If OpenClipboard(0&) Then
      Call EmptyClipboard
      '=== Convert data to ANSI byte array
      Buffer = StrConv(NewVal & vbNullChar, vbFromUnicode)
      '=== Allocate enough memory for buffer
      hData = GlobalAlloc(GMEM_FIXED, UBound(Buffer) + 1)
      If hData Then
         '=== Copy data to alloc'd memory
         lpData = GlobalLock(hData)
         Call CopyMemory(ByVal lpData, Buffer(0), UBound(Buffer) + 1)
         Call GlobalUnlock(hData)
         '=== Hand data off to clipboard
         SetText = CBool(SetClipboardData(CF_TEXT, hData))
      End If
      Call CloseClipboard
   End If
End Function
 
 
Public Function SetHtml(ByVal NewVal As String) As Boolean
   
   Dim hData As Long
   Dim lpData As Long
   Dim Buffer() As Byte
   Dim CF_HTML As Long
   Dim n As String
   Dim o As Variant
   Dim p As Variant
   Dim q As Variant
   Dim r As String
   Dim i As Integer
   Dim s As String
   
'=== replace all special caracters 128+ ascii with a code for html code
i = 1
While i < Len(NewVal)
   s = Mid(NewVal, i, 1)
   r = Asc(s)
   If r > 128 Then
      NewVal = Replace(NewVal, s, "&#" & Trim(CStr(r)) & ";")
      i = i + 3 + Len(Trim(CStr(r)))
   Else
      i = i + 1
   End If
Wend
 
'=== build html structure for clipboard
n = "Version:0.9" & vbCrLf
n = n & "StartHTML:00000000" & vbCrLf
n = n & "EndHTML:00000000" & vbCrLf
n = n & "StartFragment:00000000" & vbCrLf
n = n & "EndFragment:00000000" & vbCrLf
n = n & "StartSelection:00000000" & vbCrLf
n = n & "EndSelection:00000000" & vbCrLf
n = n & "<html><body>" & vbCrLf
n = n & "<!--StartFragment-->" & vbCrLf
n = n & NewVal & vbCrLf
n = n & "<!--EndFragment-->" & vbCrLf
n = n & "</BODY></HTML>" & vbCrLf
 
'Version: vv version number of the clipboard. Starting version is 0.9.
'StartHTML: bytecount from the beginning of the clipboard to the start of the context, or -1 if no context.
'EndHTML: bytecount from the beginning of the clipboard to the end of the context, or -1 if no context.
'StartFragment: bytecount from the beginning of the clipboard to the start of the fragment.
'EndFragment: bytecount from the beginning of the clipboard to the end of the fragment.
'StartSelection: bytecount from the beginning of the clipboard to the start of the selection.
'EndSelection: bytecount from the beginning of the clipboard to the end of the selection.
 
'=== once the string is done, we can chek where are the chekpoints
'=== then write it in the string itself, padding with "0"
q = "<html>"
p = Trim(CStr(InStr(LCase(n), q) - 1))
o = String(8 - Len(p), "0") & p
n = Replace(n, "StartHTML:00000000", "StartHTML:" & o, 1, 1)
 
q = ""
p = Trim(CStr(Len(n)))
o = String(8 - Len(p), "0") & p
n = Replace(n, "EndHTML:00000000", "EndHTML:" & o, 1, 1)
 
q = "<!--startfragment-->"
p = Trim(CStr(InStr(LCase(n), q) + Len(q) - 1))
o = String(8 - Len(p), "0") & p
n = Replace(n, "StartFragment:00000000", "StartFragment:" & o, 1, 1)
 
q = "<!--endfragment-->"
p = Trim(CStr(InStr(LCase(n), q) - 1))
o = String(8 - Len(p), "0") & p
n = Replace(n, "EndFragment:00000000", "EndFragment:" & o, 1, 1)
   
   '=== Try to set text onto clipboard
   If OpenClipboard(0&) Then
      Call EmptyClipboard
      '=== Convert data to ANSI byte array
      Buffer = StrConv(n & vbNullChar, vbFromUnicode)
      '=== Allocate enough memory for buffer
      hData = GlobalAlloc(GMEM_FIXED, UBound(Buffer) + 1)
      If hData Then
         '=== Copy data to alloc'd memory
         lpData = GlobalLock(hData)
         Call CopyMemory(ByVal lpData, Buffer(0), UBound(Buffer) + 1)
         Call GlobalUnlock(hData)
         '=== Hand data off to clipboard
         CF_HTML = RegisterClipboardFormat(RegHtml)
         'MsgBox (CF_HTML)
         SetHtml = CBool(SetClipboardData(CF_HTML, hData))
      End If
      Call CloseClipboard
   End If
End Function
 
' --------------------------------------------------------------------
'  Private Methods
' --------------------------------------------------------------------
Private Function PointerToStringA(ByVal lpStringA As Long) As String
   Dim Buffer() As Byte
   Dim nLen As Long
 
   If lpStringA Then
      nLen = lstrlenA(ByVal lpStringA)
      If nLen Then
         ReDim Buffer(0 To (nLen - 1)) As Byte
         CopyMemory Buffer(0), ByVal lpStringA, nLen
         PointerToStringA = StrConv(Buffer, vbUnicode)
      End If
   End If
End Function
 
 
Function RangeToHTM(myrange As Range)
    ' This macro will convert an Excel range to a HTML Table.
    '
    ' Copywrite 1996 - 2000 by Charles Balch, mailto:charlie@balch.edu
    ' Original Source is at http://balch.org/charlie/hdoc
    '
    ' Care Ware!  The code is yours to use and adapt for free as long as
    ' you do something nice for anyone (that includes you).
    ' Please send me Email describing how you use this code and any
    ' adjustments that you have made. Redistribute at will.
    ' Please leave my name and the original source in the comments.
    '
    ' MyRange is an Excel range you wish to convert.
    ' DocDestination is the FileName and Path to send the document to.
    '
    Dim CalcState, RowStart, ColStart
    Dim ColCount, RowCount, RowEnd, ColEnd
    Dim StatusBarState
    Dim MyTitle, Row, HzA, MV, strcc
    Dim DocDestination, col, intp, cellv, strTemp
    Dim cella, cc, SFC1, sfc2, fc, ColSpan, sametitle
    Dim bgc
    
    Application.StatusBar = "Selection to HTM, converting..."
    CalcState = Application.Calculation
    StatusBarState = Application.DisplayStatusBar
    Application.Calculation = xlManual
    Calculate
    RowStart = myrange.Row
    ColStart = myrange.Column
    ColCount = myrange.Columns.Count
    RowCount = myrange.Rows.Count
    RowEnd = RowStart + RowCount - 1
    ColEnd = ColStart + ColCount - 1
    
    '=== string that will contain all html code - short name for convenience
    Dim s As String
    
    'create Code
    s = "<HTML>" & Chr$(13)
    s = s & "<HEAD>" & Chr$(13)
        'Establish Font in all areas
        s = s & "<STYLE TYPE=""text/css"">" & Chr$(13)
            s = s & "<!-- " & Chr$(13)
            s = s & "BODY, TD, TR, P, H1, H2, H3  { font-family: arial, helvetica, sans-serif; COLOR=""#00008B""; font-size: 100% }" & Chr$(13)
            s = s & "A { COLOR=""0000FF"" }" & Chr$(13)
            s = s & "A:hover { Color: #8F0000}" & Chr$(13)
            s = s & " -->" & Chr$(13)
        s = s & "</STYLE>" & Chr$(13)
    MyTitle = Cells(RowStart, ColStart)  ' Use first cell as title
    s = s & "<TITLE>" & MyTitle & "</TITLE>" & Chr$(13)
    s = s & "</HEAD>" & Chr$(13)
    s = s & "<BODY bgcolor=" & Chr(34) & "#9F9F9F" & Chr(34) & " >" & Chr$(13)
    s = s & "<FONT FACE=""arial, helvetica, sans-serif"">" & Chr$(13)
    s = s & "<CENTER><TABLE bgcolor=" & Chr(34) & "#FFFFFF" & Chr(34) & " Border=2>" & Chr$(13)
    's = s &  "<Caption><B><Font Size=+2>" & MyTitle & "<Font Size=-2></B></caption>" & Chr$(13)
    While Row < RowCount
        Row = Row + 1
        DoEvents
        Application.StatusBar = DocDestination & ": " & Str$(Int((Row / RowCount) * 100)) & "% Completed"
        If (Not myrange.Rows(Row).Hidden) Then
            MV = ""
            col = 0
            While col < ColCount
                col = col + 1
                cellv = ""
                If (Not myrange.Columns(col).Hidden) Then
                    strTemp = myrange.Cells(Row, col).Text
                    For intp = 1 To Len(strTemp)
                        strcc = Mid(strTemp, intp, 1)
                        If Asc(strcc) = 10 Then strcc = "<BR>"
                        cellv = cellv & strcc
                    Next intp
                    If cellv = "" Then cellv = "<BR>"
                    HzA = myrange.Cells(Row, col).HorizontalAlignment
                    cella = " Align=Right "
                    If HzA = -4108 Then cella = " Align=Center "
                    If HzA = -4131 Then cella = " Align=Left "
                    If HzA = -4152 Then cella = " Align=Right "
                    If myrange.Cells(Row, col).Font.Bold Then cellv = "<B>" & cellv & "</B>"
                    If myrange.Cells(Row, col).Font.Italic Then cellv = "<I>" & cellv & "</I>"
                    If HzA = 7 Or myrange.Cells(Row, col).MergeCells Then
                        ColSpan = 0
                        sametitle = True
                        While (myrange.Cells(Row, col).HorizontalAlignment = 7 Or myrange.Cells(Row, col).MergeCells) And sametitle
                            ' The following code must be changed for versions of Excel earlier than 97
                            If Not myrange.Columns(col).Hidden Then ColSpan = ColSpan + 1
                            col = col + 1
                            If (Len(myrange.Cells(Row, col).Text) > 1 Or myrange.Cells(Row, col).MergeCells = False) Then sametitle = False: col = col - 1
                        Wend
                        cella = cella & " ColSpan=" & ColSpan
                    End If
                    'find cell interior color
                    cc = myrange.Cells(Row, col).Interior.ColorIndex
                    bgc = ""
                    If cc = 1 Then bgc = "#000000" 'black"
                    If cc = 3 Or cc = 22 Then bgc = "#FFD0D0" 'Red"
                    If cc = 4 Or cc = 35 Then bgc = "#CCFFCC"  'green"
                    If cc = 6 Or cc = 19 Then bgc = "#FFFFCC" 'yellow"
                    If cc = 8 Or cc = 41 Or cc = 34 Or cc = 20 Then bgc = "#CCFFFF" 'blue
                    If cc = 9 Then bgc = "#8A0045" 'burgandy
                    If cc = 15 Or cc = 40 Then bgc = "#DFDED0" 'grey"
                    If cc = 39 Or cc = 24 Or cc = 39 Then bgc = "#FFCCFF"  'Purple
                    If Len(bgc) > 2 Then bgc = " bgcolor=" & Chr(34) & bgc & Chr(34)
                    
                    'find cell font color
                    fc = myrange.Cells(Row, col).Font.ColorIndex
                    SFC1 = ""
                    sfc2 = ""
                    If fc = 3 Then
                        SFC1 = "<FONT COLOR=""#FF0000"">"
                    ElseIf fc = 2 Then
                        SFC1 = "<FONT COLOR=""#FFFFFF"">"
                    End If
                    If Len(SFC1) > 2 Then sfc2 = "</FONT>"
                    'Replace chr(13) with <BR>
                    'Range(MyRange).Cells(Row, Col).Replace
                    
                    MV = MV & "<TD" & cella & bgc & ">" & SFC1 & cellv & sfc2 & "</TD>"
                End If
            Wend
            s = s & "<TR>" & MV & "</TR>" & Chr$(13)
        End If
    Wend
    s = s & "</TABLE></CENTER></BODY>" & Chr$(13)
    s = s & "<P>" & Chr$(13)
'    s = s & "This table was created by a free Excel macro written by <A HREF =" & Chr(34) & "MAILTO:charlie@balch.org" & Chr(34) & ">Charles Balch</A>.<BR>" & Chr$(13)
'    s = s & "Here's the <A HREF = " & Chr(34) & "http://charlie.balch.org/hdoc/exceltohtml.html" & Chr(34) & ">code</A>.  It is care ware - it's yours for free if do something nice for anyone (anyone includes you)."
    s = s & "</HTML>" & Chr$(13)
    Close
    DoEvents
    Application.Calculation = CalcState
    Application.StatusBar = ""
    Application.DisplayStatusBar = StatusBarState
 
RangeToHTM = s
 
End Function
 
 
Sub sel_to_htmcli()
'=== take excel selection and make a html table with it
'=== and put it in windows clipboard
 
Dim table As String
    
If TypeName(Selection) <> "Range" Then
   MsgBox "Select the range to be copied. A multiple selection is allowed."
   Exit Sub
End If
 
table = RangeToHTM(Selection)
'===put the table in windows clipboard in html format
Call SetHtml(table)
 
End Sub
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
341:
342:
343:
344:
345:
346:
347:
348:
349:
350:
351:
352:
353:
354:
355:
356:
357:
358:
359:
360:
361:
362:
363:
364:
365:
366:
367:
368:
369:
370:
371:
372:
373:
374:
375:
376:
377:
378:
379:
380:
381:
382:
383:
384:
385:
386:
387:
388:
389:
390:
391:
392:
393:
394:
395:
396:
397:
398:
399:
400:
401:
402:
403:
404:
405:
406:
407:
408:
409:
410:
411:
412:
413:
414:
415:
416:
417:
418:
419:
420:
421:
422:
423:
424:
425:
426:
427:
428:
429:
430:
431:
432:
433:
434:
435:
436:
437:
438:
439:

Select allOpen in new window

 

by: dsoderstromPosted on 2008-04-10 at 06:30:35ID: 21324508

Thanks for submitting your code for me to look at.  My goal was to use the Solidworks API's directly in VBA to extract Bills of Material.  I was finally able to figure it out and the attached code shows the subroutine  I came up with for doing this.

Sub GetComponents(Parent, user, password, server)
    On Error Resume Next
    Dim connection As PDMWConnection
    Dim adoc As PDMWDocument
    Dim strdirectory As String
    Dim drawing As String
    Dim msgboxstr As String
    Dim rs As DAO.Recordset
    Dim comp As String
    
    'Open PDMWorks and Login
    Set connection = CreateObject("PDMWorks.PDMWConnection")
    connection.Login user, password, server
    
    'Look for Parent Item Assembly
    drawing = Parent & ".sldasm"
    Set adoc = connection.GetSpecificDocument([drawing])
    Dim Cfg As PDMWConfiguration
    Set Cfg = adoc.Configurations(0)
    
    'If Assembly is found, look for components
    If LCase(Right(adoc.Name, 6)) = "sldasm" Then
        Set rs = CurrentDb.OpenRecordset("Parent-components")
        Dim componentslist As PDMWLinks
        Set componentslist = Cfg.References
        Dim L As PDMWLink
        
        'Add Components to Parent-Components table
        For Each L In componentslist
            If LCase(Right(L.Document.Name, 6)) = "sldprt" Then
                comp = L.Document.Name
                If comp <> "" Then
                    comp = Left(comp, InStr(comp, ".") - 1)
                    rs.AddNew
                    rs!Parent = Parent
                    rs!Component = comp
                    rs!Description = L.Document.Description
                    rs!Quantity = L.Quantity
                    rs.Update
                End If
            End If
        Next
    End If
    
    'Close Parent-Components table and log out of PDMWorks
    rs.Close
    connection.Logout
    
    
End Sub

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:

Select allOpen in new window

 

by: wildboy85Posted on 2008-04-11 at 06:04:35ID: 21333800

thx for sharing, maybe i will use this one day

you can refund yourself your points :P

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...