Jump to content

Printing with VBA


-KarL-

Recommended Posts

I have created a batch program in VBA that opens drawings from a list and does an bunch of stuff to the drawing to get it ready to be issued. The last thing I want it to do is print an 11X17 copy of the drawing for our binders but I can not figure out how to do this in VBA.

 

Any help would be greatly appreciated!

Link to comment
Share on other sites

  • Replies 22
  • Created
  • Last Reply

Top Posters In This Topic

  • CmdrDuh

    9

  • ML0940

    7

  • -KarL-

    6

  • veranco

    1

Top Posters In This Topic

Posted Images

Karl

 

Have you looked into Plot Configurations in VBA?

 

I don't really have a good example but if you go to VBA Help and type that in, there will likely be an example that will get you going

 

ML

Link to comment
Share on other sites

OK, I just checked the help in VBA

 

You will likely need to add a plotconfig or possibly just apply this method to an existing plotconfig

 

This code is from The VBA Help Screen

 

Sub Example_GetPaperSize() 
'This example gets the width and height of the default paper size for 'your system. 

Dim PaperWidth As Double, PaperHeight As Double 

ThisDrawing.ActiveLayout.GetPaperSize PaperWidth, PaperHeight 

MsgBox "The default paper size is " & vbCrLf & _ 
 "Width: " & PaperWidth & vbCrLf & _ 
 "Height: " & PaperHeight
 End Sub

 

ML

Link to comment
Share on other sites

The help menu I have does not list AutoCAD commands, only basic VBA commands.

I am looking for a way to create a new plot configuration and then print the drawing using that configuration rather than one already in the drawing but I haven't been able to figure that part out.

Link to comment
Share on other sites

this should get you started. Are you wanting to hard code your paper size or pass it as an argument?

Public Sub SetupAndPlot(ByRef Plotter As String, CTB As String, SIZE As String, PSCALE As String, ROT As String)
     Dim Layout As AcadLayout
     On Error GoTo Err_Control
     Set Layout = ThisDrawing.ActiveLayout
     Layout.RefreshPlotDeviceInfo
     Layout.ConfigName = Plotter    ' CALL PLOTTER
     Layout.PlotType = acExtents
     Layout.PlotRotation = ROT    ' CALL ROTATION
     Layout.StyleSheet = CTB    ' CALL CTB FILE
     Layout.PlotWithPlotStyles = True
     Layout.PlotViewportBorders = False
     Layout.PlotViewportsFirst = True
     Layout.CanonicalMediaName = SIZE    ' CALL SIZE
     Layout.PaperUnits = acInches
     Layout.StandardScale = PSCALE    'CALL PSCALE
     Layout.ShowPlotStyles = False
     ThisDrawing.Plot.NumberOfCopies = 1

     Layout.CenterPlot = True
     If SIZE = "ARCH_expand_C_(24.00_x_18.00_Inches)" Then
           Layout.ScaleLineweights = True
     End If
     ThisDrawing.Regen acAllViewports
     ZoomExtents
     Set Layout = Nothing
     ThisDrawing.Save
Exit_Here:
     Exit Sub
Err_Control:
     Select Case Err.Number
     Case "-2145320861"
           MsgBox "Unable to Save Drawing- " & Err.Description
     Case "-2145386493"
           MsgBox "Drawing is setup for Named Plot Styles." & (Chr(13)) & (Chr(13)) & "Run CONVERTPSTYLES command", vbCritical, "Change Plot Style"
     Case Else
           MsgBox "Unknown Error " & Err.Number
     End Select
End Sub

Link to comment
Share on other sites

I am passing paper size, ctb, rotation, pc3 etc to this function which sets up and plots.

 

this is the function that pass the info

Public Sub Vendor1117()
     Call SetupAndPlot("11x17Draft.pc3", "11X17-CHECKSET.ctb", "ANSI_B_(11.00_x_17.00_Inches)", acScaleToFit, ac90degrees)
     ThisDrawing.Plot.PlotToDevice
           ThisDrawing.Close (True)
End Sub

Link to comment
Share on other sites

CmdrDuh

 

That looks about like what I need, just a few questions though:

1. Does it need a .pc3 file or can it be a network printer name?

2. Will it print all layouts in the drawing or only the active layout?

Link to comment
Share on other sites

It can be the network printer name, and as written, only the active layout. However, it can be modified to print all layouts, BUT, you have to know that all the layouts are good. What I mean by that is the default template (at least on my machine) has 2 layout tabs when I start a new drawing. I usually don't use the second layout, and I delete it. HOWEVER, if you were to print all layouts, you might get a blank page sent to the printer

Link to comment
Share on other sites

If you look at this picture, you can see I highlighted a network printer. I turned on the Locals window to see the current configuration as I step through the code.

 

 

Can I attach a PNG file?

print.jpg

Link to comment
Share on other sites

Hey CmdrDuh,

 

Good to see you again.

 

Karl, in The VBA Editor help, there is a button for Object browser; after you click that, there will be a text box. In there, type plotconfig, that is you will see the code examples and methods.

I am not saying that every thing you will need is there but it would def. get you started in the right direction; not only for this but many other things as well.

 

Cool, I think I will be able to use this later on as well.

 

Cmd, if you were going to use the function, in what part of the first sub would you call it from?

 

Cmd, didn't you have code for setting the default printer?

Could that also be used with this?

 

To expound on Karl's question; Karl CMD is using the active layout in his code, do you have a specific layout name?

If so, this code could be set to that layout, specifically. I think that may be what you are looking for?

 

ML

Link to comment
Share on other sites

it actually gets called from the second shorter piece of code

 

I have code to set the default printer, but its for windows not acad.

 

As for calling the layout name, you could test for any layout NOT "layout*" and print that, assuming you name your layouts

Link to comment
Share on other sites

It can be the network printer name, and as written, only the active layout. However, it can be modified to print all layouts, BUT, you have to know that all the layouts are good. What I mean by that is the default template (at least on my machine) has 2 layout tabs when I start a new drawing. I usually don't use the second layout, and I delete it. HOWEVER, if you were to print all layouts, you might get a blank page sent to the printer

 

Our templates are setup with only one layout tab so if there are more layout tabs in the drawings they will also need to be printed.

Link to comment
Share on other sites

CMD

 

Please correct me if I am wrong; that is interesting.

It looks like you are calling the first sub up from the second sub.

 

So, would you actually run the Public Sub Vendor1117 Sub first then?

 

I suppose I can do a test drawing and try it, I was just curious after looking at it

 

Thanks

 

ML

Link to comment
Share on other sites

yes, the CALL statement calls the second sub and passes the arguments to the second sub. That way I have 1 routine to print any number of ways. I have like 12 of the short versions, which all call the main sub and pass the settings I want.

Link to comment
Share on other sites

I give the code a try a see if I can get it to work, otherwise I will be back.

 

Thanks a lot for all your help!

Link to comment
Share on other sites

That is a cool approach CMD with the function part

 

OK, so we could change it to be for all layouts NOT named Model, print

 

Yes, that is what I would do as well.

 

If the layouts are named, then one gets deleted, "I think" a new one called Layout1 will be recreated. So, filtering out the name Layout may not be the best way to approach it.

 

Filtering out Model and looping through all layouts (in my opinion) would probably be the best attack, if you truly want to print all layouts.

 

If the layouts are named, I would set a reference to the specific layout(s) name.

 

ML

Link to comment
Share on other sites

I got it to work, here is how I got around the multiple layouts

 

     For x = 0 To Drawing.Layouts.Count - 1
       Set Layout = Drawing.Layouts(x)
       Layout.RefreshPlotDeviceInfo
       Layout.ConfigName = Plotter         ' CALL PLOTTER
       Layout.PlotType = acExtents
       Layout.PlotRotation = ROT           ' CALL ROTATION
       Layout.StyleSheet = CTB             ' CALL CTB FILE
       Layout.PlotWithPlotStyles = True
       Layout.PlotViewportBorders = False
       Layout.PlotViewportsFirst = True
       Layout.CanonicalMediaName = SIZE    ' CALL SIZE
       Layout.PaperUnits = acInches
       Layout.StandardScale = PSCALE       ' CALL PSCALE
       Layout.ShowPlotStyles = False
       Layout.ScaleLineweights = True
       Layout.CenterPlot = True
       Drawing.ActiveLayout = Layout
       Drawing.Plot.NumberOfCopies = 1
       Drawing.Regen acAllViewports
       Drawing.Plot.PlotToDevice
     Next x
     Set Layout = Nothing

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...