?
Solved

Create pdf from AutoCAD using VBA

Posted on 2005-05-16
17
Medium Priority
?
5,611 Views
1 Endorsement
Last Modified: 2008-01-09
Hi!  I am working on a VBA application to run in AutCAD that will create a pdf without prompting the user for a save location.  Eventually this will be part of a batch program that will allow a user to plot multiple drawings to pdf at once, without stopping between each drawing and prompting for a save location.

Any help would be greatly appreciated!

Thanks,
Christy
1
Comment
Question by:ccbailey
  • 9
  • 8
17 Comments
 
LVL 14

Expert Comment

by:Tommy Kinard
ID: 14012961
Hi ccbailey,

You need to have a plotconfiguration for the pdf I used "PlotConfigForPDF"

Sub PlotToPdf()
    Dim SaveLoc As String
    Dim Aname As String
    SaveLoc = "C:\acad\"
    Aname = Replace(ThisDrawing.Name, "dwg", "pdf")
    If InStr(1, Aname, "DWG") > 0 Then
        Aname = Replace(ThisDrawing.Name, "DWG", "PDF")
    End If
    ThisDrawing.Plot.PlotToFile SaveLoc & Aname, "PlotConfigForPDF" 'this will need to be a valid plot configuration
End Sub

HTH
dragontooth

0
 

Author Comment

by:ccbailey
ID: 14013657
Great - I'm almost there.  When I run this code, however, I get the following error message:

When you create ta PostScript files you ahve to send the host fonts.  Please go to the printer properties, "Adobe PDF Settings" page and turn OFF the option "DO not send fonts to Distiller".

Then when I click "OK", I get:

The handle is invalid.

It creates the pdf, but it is corrupt and can't be opened.  Any ideas?

Thanks,
Christy

0
 
LVL 14

Expert Comment

by:Tommy Kinard
ID: 14013734
I no longer have distiller on my PC so I can't do anything but guess at this point.

I would open the printer I am plotting to in the printer control panel. Pick properties and see if there is an Adobe PDF Setting if htere is do as instructed in the error message.
The most likely cause (I think ) is Acad uses .shx file not .ttf so acad would need to send the fonts to distiller.

You can plot manually to a pdf file right?

dragontooth

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ccbailey
ID: 14013986
I tried that - no luck.  I went to the "Printers" and found the setting for my Adobe PDF printer, but I still get the error.  But when I plot in AutoCAD, I use a pc3 file that is set up to print to adobe.  I can't find that setting for my pc3 files (there are a couple of different ones).  Any ideas there?

Thanks,
Christy
0
 
LVL 14

Accepted Solution

by:
Tommy Kinard earned 2000 total points
ID: 14014426
I used the code above with a "FreePDF"  I tried to plottofile it didn't like it. I tried PlotToDevice it loved it but I couldn't get it to save to another location. It was always on my desktop. So the only way I can see to do it is to filecopy the pdf to where you want it. So in effect I let the thing plot where it wanted to (since I have no control :( ) then moved it where I wanted and named it what I wanted then deleted the old one :).

" Model (1).pdf" is a default name given by FreePDF yours may be different. The FreePDF is a pc3 file I have setup on my PC.

Sub PlotToPdf()
    Dim SaveLoc As String
    Dim Aname As String
    SaveLoc = "C:\acad\"
    Aname = Replace(ThisDrawing.Name, ".dwg", vbNullString)
    If InStr(1, Aname, "DWG") > 0 Then
        Aname = Replace(ThisDrawing.Name, ".DWG", vbNullString)
    End If
    ThisDrawing.Plot.PlotToDevice "FreePDF"
    DoEvents   'system needs time to work
    FileCopy "C:\Documents and Settings\Administrator\Desktop\" & Aname & " Model (1).pdf", SaveLoc & Aname & ".pdf"
    Kill "C:\Documents and Settings\Administrator\Desktop\" & Aname & " Model (1).pdf"
End Sub

HTH
dragontooth

0
 

Author Comment

by:ccbailey
ID: 14021667
OK, I fixed the problem with the error message I referred to above (I had to go into the printing preferences of the pc3 file and check that box).  However, now I've run into a different problem.  My program seems to save the pdf correctly, and it does create a pdf in the correct location, however when I try to open it, it won't open and I get the following message:

Acrobat could not open 'DrawingTest.pdf' because it is either not a supported file type or because the file has been corrupted (for example, it was sent as an email attachment and wasn't correctly decoded).
To create an Adobe PDF document, go to the source application.  Then print the document to Adobe PDF.

Any ideas?  I might be able to look into the "FreePDF" solution later, but for now I've been asked to try to make it work with Acrobat, since that is a program our company already owns.

Your help is greatly appreciated.

Thanks,
Christy
0
 
LVL 14

Expert Comment

by:Tommy Kinard
ID: 14021837
http://shbox.de/ is the url for FreePDF.

Yes I get those errors also. The only way around it as far as I can tell is the way I posted earlier. Let the distiller plot the file where it wants to and move it to where you want it. As far as I can tell it gets confused with the "new" location.

Can you create the pdf by plotting by hand? If so you can duplicate it through code if not you can't. That is where I figured out the work around :(.

The "FreePDF" is the name of my pc3 file. I put in the DoEvents so the system would have time to write the file before I used it. So with the code posted above change the "FreePDF" to the name of your pc3 file. If this can generate a valid pdf  then we are at least half way :).

If it is still giving problems post your code and I'll see if I can "work around it" and get it to work. btw I am using 2000i.

Let me know how it works.
dragontooth

0
 

Author Comment

by:ccbailey
ID: 14021988
OK, I tried it this way, but when I get to the line: ThisDrawing.Plot.PlotToDevice "PBK-PDF(full)Temp.pc3", it prompts me for the location.  Do I have to use the FreePDF program to do this?
Thanks,
Christy
0
 
LVL 14

Expert Comment

by:Tommy Kinard
ID: 14022338
Not that I am aware of. I think you should be able to open the pc3 file and place a default location.

Give me a few minutes I found a PC with acad and distiller let me look at that.

dragontooth

0
 
LVL 14

Expert Comment

by:Tommy Kinard
ID: 14022490
Ok in the printers pick the distiller, right click and pick properties. in one of the tabs there is a place to select that states (as close as I can remember) prompt for file name, uncheck this option. This should stop the prompt. If it does not, open the shortcut you are using to open Acad, goto the properties and in the Start In box put "C:\acad" (as an example) save and close. This should fix the prompt. The pdf will be saved on the users desktop.

Let me know if you get this far and we'll go from there.
dragontooth

0
 

Author Comment

by:ccbailey
ID: 14022593
OK, I got that far, but mine doesn't save on the desktop - it saves in the user's "My Documents" folder.  How do I code it so that the program looks in a different folder for each person?
0
 
LVL 14

Expert Comment

by:Tommy Kinard
ID: 14022671
Are you using win 2000 or win xp?

There are different ways depending on the OS let me know. We are on the downhill swing :)

dragontooth

0
 

Author Comment

by:ccbailey
ID: 14022728
Well, I'm using 2000, but many people in the company are using xp.  Since everyone will be using the program, it needs to do it for both operating systems.  Is there a way to check the version of the OS and then do it accordingly?

Thanks again,
Christy
0
 
LVL 14

Expert Comment

by:Tommy Kinard
ID: 14022752
If you are using win 2000 and above this will work. Also " Model (1).pdf" was the extra information that was default in the distiller if this does not apply ignore.

Function WhoAmI() As String
    WhoAmI = Environ("USERNAME")
End Function

Sub PlotToPdf()
    Dim SaveLoc As String
    Dim Aname As String
    SaveLoc = "C:\acad\"  'change as required
    Aname = Replace(ThisDrawing.Name, ".dwg", vbNullString)
    If InStr(1, Aname, "DWG") > 0 Then
        Aname = Replace(ThisDrawing.Name, ".DWG", vbNullString)
    End If
    ThisDrawing.Plot.PlotToDevice "PBK-PDF(full)Temp.pc3"
    DoEvents   'system needs time to work
    FileCopy "C:\Documents and Settings\" & WhoAmI & "\My Documents\" & Aname & " Model (1).pdf", SaveLoc & Aname & ".pdf"
    Kill "C:\Documents and Settings\" & WhoAmI & "\My Documents\" & Aname & " Model (1).pdf"
End Sub


dragontooth

0
 

Author Comment

by:ccbailey
ID: 14022938
That did it!  I had to do a little tweaking because our "My Documents" folder is not strictly the username, but I got around it.  Thanks so much!

Hey - I have another question.  I can post it as a different question for more points, but I was wondering if you might be able to help me.  Some of the users have Acrobat 5.0, and others have Acrobat 6.0.  They can't use the same pc3 files, so I need a way to detect which version the user has so I can use the correct pc3 file for their machine.  Can you help with this?  Let me know and I'll set up a different question.

Thanks again!
Christy
0
 
LVL 14

Expert Comment

by:Tommy Kinard
ID: 14023263
Thanks for the Points and the Grade!

I would guess the login name?

If the pc3 files were for 5.0 and the 6.0 pc3 file was not there or vice versa that would make it easy. Otherwise I would have to do a directory search for the version (I don't like to doing regestry but can) which would just make it a little harder.

dragontooth

0
 

Author Comment

by:ccbailey
ID: 14026944
The pc3 files are all there for everybody, so I guess it's the directory search option.  There's no way to do it without registry?  I was thinking there might be a command in the Acrobat library that contains the version number or something.

I'll create another question in this same section - thanks for your help!

Christy
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month17 days, 9 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question