Open and Print a SLDDRW from within Excel
Open and Print a SLDDRW from within Excel
(OP)
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
'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






RE: Open and Print a SLDDRW from within Excel
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
RE: Open and Print a SLDDRW from within Excel
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.
RE: Open and Print a SLDDRW from within Excel
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)
RE: Open and Print a SLDDRW from within Excel
RE: Open and Print a SLDDRW from within Excel
RE: Open and Print a SLDDRW from within Excel
CODE
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: API implementation of Common Dialog. This will get you the file dialog window and I have used it to do similar things with multiple files
RE: Open and Print a SLDDRW from within Excel
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.
RE: Open and Print a SLDDRW from within Excel
How do i open it read only
How do i maximize the window on open
print the drawing
close
Thanks
RE: Open and Print a SLDDRW from within Excel
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
RE: Open and Print a SLDDRW from within Excel
Eric
RE: Open and Print a SLDDRW from within Excel
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
RE: Open and Print a SLDDRW from within Excel
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