Reply

Printing a .dft using vba in excel

[ Edited ]

hi

 

I have trouble writing a code that can print a dft.

heres my code:

 

Option Explicit

 

Sub PrintDraft()

Dim objApp As SolidEdgeFramework.Application

Dim objDoc As SolidEdgeDraft.DraftDocument

Dim objSheet As SolidEdgeDraft.Sheet

 

Dim FilePath_VA As String

Dim PrinterName_VA As String

Dim SEprinter_VA As String

 

'sets the filename to be printed = draftfile called "drawing.dft" located in the same folder as this excelsheet

FilePath_VA = ThisWorkbook.Path & "\drawing.dft"

 

'sets the printer to the default systemprinter

PrinterName_VA = Application.ActivePrinter

 

On Error Resume Next

'opens solid edge

Set objApp = GetObject(, "SolidEdge.Application")

 

If Err Then

Err.Clear

Set objApp = CreateObject("SolidEdge.Application")

 

Set objDoc = objApp.Documents.Open(FilePath_VA)

End If

objApp.Visible = True '*****change false to true if you want to see the drawing being generated******

objApp.DisplayAlerts = False

 

Set objSheet = objDoc.ActiveSheet

 

SEprinter_VA = objApp.ActivePrinter

 

'*************************************I'm doing something wrong here? it doesn't work?*****

objDoc.PrintOut (SEprinter_VA)

 

'*********************************************************************************

 

 

 

Call objDoc.Close

Call objApp.Quit

 

Set objApp = Nothing

Set objDoc = Nothing

Set objSheet = Nothing

 

End Sub

 

 

Posted by: casper jensen
Post date: 7/21/2006 2:25:10 AM

2 REPLIES

RE: Printing a .dft using vba in excel

[ Edited ]

I used this code to fix my problem:

 

 

Option Explicit

 

' API function to find the default printer

Private Declare Function GetDefaultPrinter Lib "winspool.drv" Alias "GetDefaultPrinterA" (ByVal sPrinterName As String, lPrinterNameBufferSize As Long) As Long

 

' Function to find the default printer by "GetDefaultPrinter"

Public Function GetDefaultPrinterName() As String

 

Dim sLen As Long

Dim hResult As Long

 

GetDefaultPrinterName = Strings.Space$(255)

sLen = 255

hResult = GetDefaultPrinter(ByVal GetDefaultPrinterName, sLen)

If hResult 0 Then

GetDefaultPrinterName = Strings.Left(GetDefaultPrinterName, sLen - 1)

Else

GetDefaultPrinterName = ""

End If

End Function

 

 

 

Sub PrintDraft()

Dim objApp As SolidEdgeFramework.Application

Dim objDoc As SolidEdgeDraft.DraftDocument

Dim objSheet As SolidEdgeDraft.Sheet

 

Dim FilePath_VA As String

Dim PrinterName_VA As String

Dim SEprinter_VA As String

 

 

'sets the filename to be printed = draftfile called "drawing.dft" located in the same folder as this excelsheet

FilePath_VA = ThisWorkbook.Path & "\drawing.dft"

 

'sets the printer to the default systemprinter

PrinterName_VA = Application.ActivePrinter

 

On Error Resume Next

'opens solid edge

Set objApp = GetObject(, "SolidEdge.Application")

 

If Err Then

Err.Clear

Set objApp = CreateObject("SolidEdge.Application")

 

Set objDoc = objApp.Documents.Open(FilePath_VA)

End If

objApp.Visible = True '*****change false/true if you want to see the drawing ******

objApp.DisplayAlerts = False

 

Set objSheet = objDoc.ActiveSheet

 

 

' Find the default printer using the API

SEprinter_VA = GetDefaultPrinterName

 

 

 

objDoc.PrintOut SEprinter_VA, Orientation:=2

 

 

 

 

Call objDoc.Close

Call objApp.Quit

 

Set objApp = Nothing

Set objDoc = Nothing

Set objSheet = Nothing

 

End Sub

 

 

 

 

Posted by: casper jensen
Post date: 7/30/2006 9:07:15 PM

Printing a .dft using vba in excel

[ Edited ]

Hi,

tried the previous code, but some DFT s won't open... I can't figure out why, it happens to random dfts.

And if one dft doesnt work the first time it wont the 2nd and 3rd....

The code is pretty much the same as the last post, with a loop.

Option Explicit

' API function to find the default printer
Private Declare Function GetDefaultPrinter Lib "winspool.drv" Alias "GetDefaultPrinterA" (ByVal sPrinterName As String, lPrinterNameBufferSize As Long) As Long

' Function to find the default printer by "GetDefaultPrinter"
Public Function GetDefaultPrinterName() As String

Dim sLen As Long
Dim hResult As Long

GetDefaultPrinterName = Strings.Space$(255)
sLen = 255
hResult = GetDefaultPrinter(ByVal GetDefaultPrinterName, sLen)
If hResult = 0 Then
GetDefaultPrinterName = Strings.Left(GetDefaultPrinterName, sLen - 1)
Else
'GetDefaultPrinterName = ""
End If
End Function



Sub PrintDraft()
Dim objApp As SolidEdgeFramework.Application
Dim objDoc As SolidEdgeDraft.DraftDocument
Dim objSheet As SolidEdgeDraft.Sheet

Dim FilePath_VA As String
Dim PrinterName_VA As String
Dim SEprinter_VA As String
Dim x As Integer



'sets the printer to the default systemprinter
PrinterName_VA = Application.ActivePrinter
SEprinter_VA = GetDefaultPrinterName

On Error Resume Next
'opens solid edge
Set objApp = GetObject(, "SolidEdge.Application")

If Err Then
Err.Clear

Set objApp = CreateObject("SolidEdge.Application")
objApp.DisplayAlerts = False

If Worksheets("Print List").Range("Q1").Value > 0 Then

For x = Worksheets("Print List").Range("Q1").Value To Worksheets("Print List").Range("R1").Value

If Worksheets("Print List").Range("P" & x).Value <> "" Then

FilePath_VA = Worksheets("Print List").Range("P" & x).Value

Set objDoc = objApp.Documents.Open(FilePath_VA)


objApp.Visible = True '*****change false/true if you want to see the drawing ******


'Set objSheet = objDoc.ActiveSheet

' Find the default printer using the API

objDoc.PrintOut SEprinter_VA, Orientation:=2
Call objDoc.Close

End If

Next

End If


End If

Call objApp.Quit

Set objApp = Nothing
Set objDoc = Nothing
Set objSheet = Nothing

End Sub


 

Posted by: Marc Turcotte
Post date: 1/17/2013 10:23:38 AM