Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

how to print to file using vb.net

Status
Not open for further replies.

Dtown266

Nuclear
Oct 31, 2006
47
have a macro written in vba that i am trying to convert to vb.net. The macro prints a drawing to a *.prn file. We then use acrobat distiller to create the pdf, add watermarks, and compile with other documents. I cannot seem to get the vb.net macro to output the *.prn file. I am using the Printout2 Method. Below is the code. Any help would be appreciated as I am new to vb.net.

The first block of Code is the vba macro, the second is the vb.net.

Thanks

Leyland

VBA CODE
Code:
Option Explicit
Dim swApp                   As SldWorks.SldWorks
Dim swModel                 As SldWorks.ModelDoc2
Dim swDocExt                As SldWorks.ModelDocExtension
Dim swDraw                  As SldWorks.DrawingDoc
Dim SWPrinter               As String
Dim PathName                As String
Dim PrintFileName           As String
Dim nPrintSheets(1)         As Long
Dim vPrintSheets            As Variant
Dim DefPrinter              As String
Dim i                       As Long
Dim ps                      As PageSetup
Dim ConvertPdf              As Integer
Const OutPutLocation        As String = "C:\print files\in\"



Sub main()
    
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
'Set swDocExt = swModel.Extension
'Set swDraw = swModel

SWPrinter = "\\HOLTEC-LVAN764\AdobePDF"

ConvertPdf = MsgBox("Would you like to covert the pdf after writing?", vbYesNo)


' Strip off SolidWorks file extension (.sldxxx)
Dim filename As String

'get file name
filename = GetFileName(swModel.GetPathName)
Debug.Print filename

'build filename and location to save files
Dim Answer As String
Dim Rev As String

'get file revisioin
Rev = InputBox("What is the revision of this file")

Answer = MsgBox("Is this a draft Drawing", vbQuestion + vbYesNo, "WELL?")

If Answer = vbYes Then
    PathName = OutPutLocation & filename & "R" & Rev & "_DRAFT_" & Date & ".prn"
    Debug.Print PathName
Else
PathName = OutPutLocation & filename & "R" & Rev & ".prn"
End If

Debug.Print PathName

'Don't know why this needs to stay but macro will not work without it
'---------------------------------------------------------
nPrintSheets(0) = 0
vPrintSheets = nPrintSheets
'---------------------------------------------------------
PrintFileName = PathName

'create poscriptfile with correct naming convention
swModel.Extension.PrintOut2 (vPrintSheets), 1, True, SWPrinter, PrintFileName
Debug.Print "im done"

If ConvertPdf = vbNo Then GoTo EndMe

Call OpenDistiller(Answer)
EndMe:

MsgBox ("i'm done")
End Sub

Public Function GetFileName(FlName As String) As String
    
    'Get the filename without the path or extension.
    'Input Values:
    '   flname - path and filename of file.
    'Return Value:
    '   GetFileName - name of file without the extension.
    
    Dim posn As Integer, i As Integer
    Dim fName As String
    
    posn = 0
    'find the position of the last "\" character in filename
    For i = 1 To Len(FlName)
          If (Mid(FlName, i, 1) = "\") Then
            posn = i
            Debug.Print (i)
            Debug.Print (posn)
        End If
    Next i

    'get filename without path
    fName = Right(FlName, Len(FlName) - posn)
    Debug.Print (FlName)
    Debug.Print (fName)

    'strip off solidworks extension Solidworks file extensions are 7 characters
    'long including the period
    fName = Left(fName, Len(fName) - 7)
     Debug.Print fName
    GetFileName = fName
End Function

VB.net Code
Code:
Option Explicit On
Imports SolidWorks.Interop.sldworks
Imports SolidWorks.Interop.swconst
Imports System
Imports System.IO

Partial Class SolidWorksMacro

    Public Sub main()
        Dim swDoc As ModelDoc2 = Nothing


        swDoc = swApp.ActiveDoc
        swFileName = swDoc.GetPathName
        Debug.Print(swFileName)

        'cheCk if file has be saved at least once
        If swFileName = "" Then
            RetVal = MsgBox("File Must Be Saved. Please Save File And Try Again.", MsgBoxStyle.Exclamation)
            Exit Sub
        End If

        'Check to see if file is a drawing
        Dim FileType As String
        FileType = CheckFileType(swFileName)
        If FileType <> "DWG" Then
            MsgBox("The Current Document Open In Solidworks Is Not A Drawing. Please Open A Drawing File.", MsgBoxStyle.Exclamation)
            Exit Sub
        End If
        'BUILD FILE NAME
        Dim FileName As String = GetFileName(swFileName)
        Dim Rev As String = InputBox("What is the revision of this file")
        Dim Answer As String = MsgBox("Is this a draft Drawing", vbQuestion + vbYesNo, "WELL?")
        Dim pathname As String
        Dim Mydate As System.DateTime = System.DateTime.Today
        'Dim mydate As System.DateTime = System.DateTime.Now
        Debug.Print(mydate)

        If Answer = vbYes Then
            pathname = OutPutLocation & FileName & "R" & Rev & "_DRAFT_" & mydate & ".prn"
            Debug.Print(pathname)
        Else
            pathname = OutPutLocation & FileName & "R" & Rev & ".prn"
        End If
        'Don't know why this needs to stay but macro will not work without it
        '---------------------------------------------------
        Dim nPrintSheets(1) As Long
        nPrintSheets(0) = 0
        Dim vPrintSheets As Object
        vPrintSheets = nPrintSheets
        '---------------------------------------------------
        Dim PrintFileName As String
        PrintFileName = pathname
        'create poscriptfile with correct naming convention
        swDoc.Extension.PrintOut2((vPrintSheets), 1, True, swPrinter, PrintFileName)

        'Call CreateDirectory(PlotFileDirectory)

        'Call CheckIfFileExist("3996.PDF")

    End Sub
    Public Function CreateDirectory(ByRef MyDirectory As String) As Boolean
        'Check for Directory
        CreateDirectory = False
        Dim MyDir As New DirectoryInfo(MyDirectory)
        If Not MyDir.Exists Then
            MkDir(MyDirectory)
            Debug.Print("Created directory:" & MyDirectory)
            CreateDirectory = True
        Else
            Debug.Print("Directory Found. directory was last accessed on: " & MyDir.LastAccessTime)
            CreateDirectory = True
        End If
    End Function
    Public Function CheckIfFileExist(ByRef Myfile As String) As Boolean
        CheckIfFileExist = False
        Myfile = PlotFileDirectory & Myfile
        Dim CheckFile As New FileInfo(Myfile)

        If Not CheckFile.Exists Then
            RETVAL = MsgBox("FILE" & Myfile & " NOT FOUND. WOULD YOU LIKE TO WAIT AND TRY AGAIN?", MsgBoxStyle.YesNo)
            If RETVAL = vbYes Then
                CheckIfFileExist(Myfile)
            Else
                Exit Function
            End If
        Else
            Debug.Print("FILE: " & Myfile & " FOUND. FILE WAS CREATED ON: " & CheckFile.CreationTime)
        End If
    End Function
    Public Function CheckFileType(ByRef ModelFilePath As String) As String

        Dim ModelPath As String

        ModelPath = Right(ModelFilePath, 6)

        ModelPath = UCase(ModelPath)
        Debug.Print(ModelPath)
        Select Case ModelPath

            Case "SLDPRT"
                CheckFileType = "PART"
            Case "SLDASM"
                CheckFileType = "ASSY"
            Case "SLDDRW"
                CheckFileType = "DWG"
        End Select

    End Function
    Public Function GetFileName(ByVal FlName As String) As String

        'Get the filename without the path or extension.
        'Input Values:
        '   flname - path and filename of file.
        'Return Value:
        '   GetFileName - name of file without the extension.

        Dim posn As Integer, i As Integer
        Dim fName As String

        posn = 0
        'find the position of the last "\" character in filename
        For i = 1 To Len(FlName)
            If (Mid(FlName, i, 1) = "\") Then
                posn = i
                Debug.Print(i)
                Debug.Print(posn)
            End If
        Next i

        'get filename without path
        fName = Right(FlName, Len(FlName) - posn)
        Debug.Print(FlName)
        Debug.Print(fName)

        'strip off solidworks extension Solidworks file extensions are 7 characters
        'long including the period
        fName = Left(fName, Len(fName) - 7)
        Debug.Print(fName)
        GetFileName = fName
    End Function

    ''' <summary>
    ''' The SldWorks swApp variable is pre-assigned for you.
    ''' </summary> 

    Public swApp As SldWorks
    Public RetVal As Integer

    Const swPrinter As String = "\\HOLTEC-LVAN764\AdobePDF"
    Dim swFileName As String

    Const PlotFileDirectory As String = "c:\Print Doctor\Plot Files\"
    Const OutPutLocation As String = "c:\print files\in\"
End Class
 
Replies continue below

Recommended for you

I'm sorry I can't answer your question, but instead of posting huge gobs of code, just ZIP and post the files via the ENGINEERING.com upload link below. Those who are interested can then download, review and repost.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor