Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open and Print a SLDDRW from within Excel

Status
Not open for further replies.

Russell67

Automotive
Nov 1, 2005
114
In the futile attempt to understand the programming i have an excel sheet that has the drawing and path of the item i want to print (variable "filename"). I have the following code that allows me to open solidworks but i cannot get the needed file to open and i have yet to work on the print.

'Opens SolidWorks
Set swApp = CreateObject("SldWorks.Application")
swApp.Visible = True
Set Part = swApp.ActiveDoc

' Load model and expand window
Set Part = swApp.OpenDoc6("filename.slddrw")
Set Part = swApp.ActivateDoc("filename.slddrw")
swApp.ActiveDoc.ActiveView.FrameLeft = 0
swApp.ActiveDoc.ActiveView.FrameTop = 0
swApp.ActiveDoc.ActiveView.FrameState = 1
swApp.ActiveDoc.ActiveView.FrameState = 1

Why will my "filename" not open?
and what code do i need to rebuild then print.

I am runnig 2007 sp4. Help is greatly appreciated. Thanks
 
Replies continue below

Recommended for you

Hi Russell,

While not a programmer myself; wouldn't you need to use a path (e.g. C:\TEMP\filename.slddrw)?

To rebuild you would use:

status = RegenNotify ( )

Cheers,

Joseph
 
Not really concerned by the file path at this point. What i am currently using for testing purposes is just to enter the values in a cell. At some point in the future this will be via a selection window like opening a file.

Dim txtfile As String
Dim dir As String
Dim file As String
dir = RANGE("d4").Value
txtfile = RANGE("d6").Value
file = dir + ("\") + txtfile

My excel program reads a txt file that is output from Lenny's AssemblyBOM macro, pastes that info into a spreadsheet adds the slddrw extension then searches the world looking for a match. I then sort and eliminate duplicates now i am left with a list of all the parts in my assembly that have a drawing. Next thing i want to do is to take that list and print out each drawing. It looks like i will need to elimnate some things prior to running some sort of print macro. In the future i would like to do the printing from task scheduler but baby steps for now.
 
Going by the variable you just mentioned, your opendoc method call would have to look like this:

Set Part = swApp.OpenDoc6(file)

Where the file variable is the full pathname to the document. However, for the call to work you will need to plug some additional information in to the call. From SW API Help:

retval = SldWorks.OpenDoc6 ( filename, type, options, configuration, &Errors, &Warnings )

So your call may actually look like this:

dim lErrors as long
dim lWarnings as long

Set Part = swApp.OpenDoc6(file, swDocDrawing, 0, "", lErrors, lWarnings)

 
You may not be concerned about full file path, but SW API is. You need to specify full path.
 
Actually i stand corrected. After my excel routine runs it spits out a list of files with there complete path. I was hoping to take that info and copy it from the cell to a variable and have SW API read that. Will that work or will i need to paste the long string in for each file? What would the code look like assuming i paste the string between quotes.

 
Opening a SolidWorks drawing from Excel VBA is not a problem. I suspect that you may not have enabled the SWX type libraries ("Tools" - "References") in the Excel VBA editor. Here is the code to open the drawing listed in cell A1. Of course, having the full path, name and extension is required.

Code:
Dim swApp               As SldWorks.SldWorks
Dim DwgDoc              As SldWorks.ModelDoc2
Dim strDrawing          As String
Dim lErrors             As Long
Dim lWarnings           As Long
    
Set swApp = CreateObject("SldWorks.Application")
strDrawing = Range("A1").Value
    
Set DwgDoc = swApp.OpenDoc6(strDrawing, swDocDRAWING,_ swOpenDocOptions_Silent, "", lErrors, lWarnings)

I think this entire excercise though is a bandaid solution for a poorly organized file structure. You really should not have to keep track of drawings and the parts that they reference in an Excel spreadsheet. You may find that it actually takes less work to develop something that moves each of these drawings into the same folder as the referenced model. This would accomplish two things:

1)It eliminates the spreadsheet
2)It would make it very easy for you to get what you really want - a routine that finds all the drawings for all the child parts in a parent assembly and prints them out.

Perhaps this is the better solution?

Or, if you do want to choose files to print based on selecting a series of files from a window, check out FAQ559-1164. This will get you the file dialog window and I have used it to do similar things with multiple files
 
Suggestion 2 is what i need. You are right it is a poorly organized file structure (prior to my arrival).

My macro attempt is a bandaid and a work around to suggestion 2. This is my current game plan;

1) Run AssemblyBom (Lenny's) export just the part numbers from a MultiLevel BOM to a text file.

2) Open Excel import the text file, sort it, delete dup's and known prefixes (i.e. HW-XXXX for hardware), assign the "slddrw" extension, search the network, list the complete file with path.

3) Still in process: Either export list as text file and have Solidworks read that file and print the list or open Solidworks from within excel and print the list.

I would prefer to do all this from within solidworks, however my coding skills with solidworks consist of about 0 hours and code sniplets are harder to find. Excel on the other hand is all over the internet, so it is just easier.

What is kinda nice is now i have a list of the location, so if i were to use task scheduler i know where to begin my search. Also just maybe i could put this list in the folder and the people up front could open the stuff with edrawings and print the project.

If i were to list my needs i get zero help, if i create a monster and have a problem with certain areas help is easier to find. Also if i do it i know what was done and i can tweak as i learn.
 
Thanks for the sniplet Stoker. I had no idea to enable the SWX libraries. After i did that it opens the document.

How do i open it read only
How do i maximize the window on open
print the drawing
close

Thanks
 
Figured out the read only and the close just need the print portion

Sub Print_File()
Dim swApp As SldWorks.SldWorks
Dim DwgDoc As SldWorks.ModelDoc2
Dim strDrawing As String
Dim lErrors As Long
Dim lWarnings As Long

Set swApp = CreateObject("SldWorks.Application")
swApp.Visible = True
strDrawing = RANGE("b10").Value

Set DwgDoc = swApp.OpenDoc6(strDrawing, swDocDRAWING, swOpenDocOptions_ReadOnly, "", lErrors, lWarnings)
swApp.QuitDoc strDrawing
 
In one of your other threads for this project, thread559-191542, Shaggy18VW posted directions to PrintDrawings.swp which should help you on your way.

Eric
 
Thanks EEnd. Unfortunatly i have poured over that code and can't understand enough of it to extract the portion that i can use to print. This is the code that i got so far. Runs quick probably will need to slap a timer in there depending on how fast it might print.

Opens Solidworks
Opens file view only
Someday print
Close solidworks
rinse and repeat

Sub Print_Drawing()

Dim swApp As SldWorks.SldWorks
Dim DwgDoc As SldWorks.ModelDoc2
Dim strDrawing As String
Dim lErrors As Long
Dim lWarnings As Long
Dim TotalRows As String

TotalRows = RANGE("A65536").End(xlUp).Row
Worksheets("Sheet1").Cells(2, 4) = TotalRows


For COUNTER = 1 To RANGE("D2").Value

Set swApp = CreateObject("SldWorks.Application")
swApp.Visible = False
strDrawing = Worksheets("Sheet1").Cells(COUNTER,2).Value
Set DwgDoc = swApp.OpenDoc6(strDrawing, swDocDRAWING,_ swOpenDocOptions_ViewOnly, "", lErrors, lWarnings)

'ENTER PRINT STATEMENT HERE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

swApp.QuitDoc strDrawing

Next COUNTER

End Sub
 
This is my completed sub-routine for printing my drawings (whatever extension i select) created by sorting and searching and other wonderful stuff in excel. Someday i hope to do this all in Solidworks.

Sub Print_Drawing()

Dim swApp As SldWorks.SldWorks
Dim DwgDoc As SldWorks.ModelDoc2
Dim strDrawing As String
Dim lErrors As Long
Dim lWarnings As Long


' This section sorts and counts the rows for the number of item to search for
RANGE("A1:C200").SORT Key1:=RANGE("a1"), Order1:=xlAscending, Header:= _
xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

Dim totalrows As String
totalrows = RANGE("A65536").End(xlUp).Row
Worksheets("Sheet1").Cells(2, 4) = totalrows

For COUNTER = 1 To RANGE("D2").Value

Set swApp = CreateObject("SldWorks.Application")
swApp.Visible = False
strDrawing = Worksheets("Sheet1").Cells(COUNTER, 2).Value
Set DwgDoc = swApp.OpenDoc6(strDrawing, swDocDRAWING, swOpenDocOptions_ViewOnly, "", lErrors, lWarnings)
DwgDoc.Extension.PrintOut2 vPageArray, copies, collate, "", ""
newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + 1
waitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait waitTime
swApp.QuitDoc strDrawing

Next COUNTER

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor