Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-08-2015, 07:34 PM
Freedom20 Freedom20 is offline Using VBA to convert AutoCAD into WMF Windows 10 Using VBA to convert AutoCAD into WMF Office 2010 64bit
Novice
Using VBA to convert AutoCAD into WMF
 
Join Date: Sep 2015
Posts: 4
Freedom20 is on a distinguished road
Default Using VBA to convert AutoCAD into WMF

I am trying to write a macro to open an AutoCAD dxf drawing and convert it to wmf and then import into the current Word document. Below is the code that I have written. My comments to the code are in blue.



Sub Convert_dxf()
Dim iRow As Integer
Dim AcadApp As Object
Dim SS 'As AcadSelectionSet Word will not let me declare this as a selection set


On Error Resume Next
Set AcadApp = GetObject(, "AutoCAD.Application")
If Err.Number <> 0 Then
Set AcadApp = CreateObject("AutoCAD.Application")
End If
AcadApp.Visible = True

AcadApp.Documents.Open ("C:\drawing.dxf")

Set SS = AcadApp.ActiveDocument.SelectionSets.Add("SS")
Set SS = Nothing
'SS.AcadApp.SelectAll
AcadApp.ActiveDocument.Export "C:\drawing", "WMF", SS
'at this point AutoCAD waits for me to select items on drawing.
'according to AutoCAD's help if the selection set is Nothing it is supposed to select the entire drawing.

AcadApp.ActiveDocument.Close savechanges:=False
' If AcadApp.Documents.Count = 1 Then
' AcadApp.ActiveDocument.sendcommand "Exit" & vbCr
' is there a way to close AutoCAD without using SendCommand?

' End If
Set AcadApp = Nothing
End Sub
I am open to other methods as long as the produce the same results. I do not want to take a screen shot or similar due to line quality issues.


Thank you in advance for any assistance you can provide.
Reply With Quote
  #2  
Old 09-08-2015, 08:15 PM
macropod's Avatar
macropod macropod is offline Using VBA to convert AutoCAD into WMF Windows 7 64bit Using VBA to convert AutoCAD into WMF Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by Freedom20 View Post
Dim SS 'As AcadSelectionSet Word will not let me declare this as a selection set
You could only do that if you were using early binding, but you're using late binding.

Quote:
Originally Posted by Freedom20 View Post
Set SS = AcadApp.ActiveDocument.SelectionSets.Add("SS")
Set SS = Nothing
'SS.AcadApp.SelectAll
AcadApp.ActiveDocument.Export "C:\drawing", "WMF", SS
'at this point AutoCAD waits for me to select items on drawing.
'according to AutoCAD's help if the selection set is Nothing it is supposed to select the entire drawing.
Given that you've already 'Set SS = Nothing', there's nothing to export. You really should leave that line until after you've finished with the object.

Quote:
Originally Posted by Freedom20 View Post
' AcadApp.ActiveDocument.sendcommand "Exit" & vbCr
' is there a way to close AutoCAD without using SendCommand?
That all depends on what methods are available in AutoCAD (e.g. AcadApp.Quit might be a valid method) - it's not really a Word issue. Ordinarily, though, one would also expect the open file to be closed before you quit the application.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 09-09-2015, 04:21 PM
Freedom20 Freedom20 is offline Using VBA to convert AutoCAD into WMF Windows 10 Using VBA to convert AutoCAD into WMF Office 2010 64bit
Novice
Using VBA to convert AutoCAD into WMF
 
Join Date: Sep 2015
Posts: 4
Freedom20 is on a distinguished road
Default

Quote:
Originally Posted by macropod View Post
You could only do that if you were using early binding, but you're using late binding.
I am not familiar with early vs. late binding. I am self taught in VBA and have not seen those terms before. How would I set this up with early binding?
Reply With Quote
  #4  
Old 09-09-2015, 06:03 PM
macropod's Avatar
macropod macropod is offline Using VBA to convert AutoCAD into WMF Windows 7 64bit Using VBA to convert AutoCAD into WMF Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Here's an article about Early and Late Binding: https://support.microsoft.com/en-us/kb/245115.

An essential distinction between early binding and late binding is that, to use early binding, you must set a reference to the application (in this case AutoCAD) in the VBE via Tools|References. Once you do that, though, you can use all of the application's methods, procedures and properties much more easily than you can using late binding.

Using early binding, your code might become:
Code:
Sub Convert_dxf()
Dim AcadApp As AutoCAD.Application
Dim AcadDoc As AutoCAD.Document
Dim SS As AutoCAD.AcadSelectionSet
Set AcadApp = New AutoCAD.Application
With AcadApp
  .Visible = False
  Set AcadDoc = .Documents.Open("C:\drawing.dxf")
  With AcadDoc
    Set SS = .SelectionSets("SS")
    .Export "C:\drawing.wmf", "WMF", SS
    .Close SaveChanges:=False
  End With
  .Quit
End With
Set SS = Nothing: Set AcadDoc = Nothing: Set AcadApp = Nothing
End Sub
Note: I don't have AutoCAD, so I don't know what it's VB object model looks like or what methods, procedures and properties are available. Accordingly, you'll have to do your own work on that front. For testing purposes, you'll also want to comment-out the line:
.Visible = False
otherwise you could end up with a plethora of hidden and orphaned AutoCAD sessions running in the background.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 09-10-2015, 04:37 PM
Freedom20 Freedom20 is offline Using VBA to convert AutoCAD into WMF Windows 10 Using VBA to convert AutoCAD into WMF Office 2010 64bit
Novice
Using VBA to convert AutoCAD into WMF
 
Join Date: Sep 2015
Posts: 4
Freedom20 is on a distinguished road
Default

Thank you for the reply. However, I was not able to get the code to run.

Quote:
Originally Posted by macropod View Post
Using early binding, your code might become:
Code:
Sub Convert_dxf()
Dim AcadApp As AutoCAD.Application
Dim AcadDoc As AutoCAD.Document
Dim SS As AutoCAD.AcadSelectionSet
Set AcadApp = New AutoCAD.Application
With AcadApp
  .Visible = False
  Set AcadDoc = .Documents.Open("C:\drawing.dxf")
  With AcadDoc
    Set SS = .SelectionSets("SS")
    .Export "C:\drawing.wmf", "WMF", SS
    .Close SaveChanges:=False
  End With
  .Quit
End With
Set SS = Nothing: Set AcadDoc = Nothing: Set AcadApp = Nothing
End Sub
The code was getting hung up on the following line
Code:
Set AcadDoc = .Documents.Open("C:\drawing.dxf")


I found another solution. I placed the export code into AutoCAD and called it from Word.

For anyone who might need it, the solution I found is below.

Code in Word:
Code:
Sub Convert_dxf()
Dim AcadApp As Object
        
    On Error Resume Next
    Set AcadApp = GetObject(, "AutoCAD.Application")
    If Err.Number <> 0 Then
        Set AcadApp = CreateObject("AutoCAD.Application")
    End If
    AcadApp.Visible = False
        
    AcadApp.Documents.Open ("C:\drawing.dxf")
    AcadApp.RunMacro "Convert_drawing"
     AcadApp.ActiveDocument.Close savechanges:=False
    If AcadApp.Documents.Count = 1 Then
        AcadApp.ActiveDocument.Close savechanges:=False
        AcadApp.Quit
    End If
    Set AcadApp = Nothing
End Sub
The code in AutoCAD:
Code:
Sub Convert_drawing()
Dim SS As AcadSelectionSet
 
Set SS = ActiveDocument.SelectionSets.Add("SSet")
SS.Select acSelectionSetAll
ActiveDocument.Export "C:\drawing", "WMF", SS
Set SS = Nothing
 
End Sub
Note that if AutoCAD is left running, you might get an error stating that the selectionset already exists.
Reply With Quote
Reply

Tags
auto export, autocad, vba

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do you convert to .MOV? pmr8888 PowerPoint 0 11-30-2011 04:32 PM
Outlook 2010 attach autocad drawing knorre Outlook 0 10-31-2011 12:46 AM
Pasting into worksheet from Autocad for Mac davisb Excel 0 08-29-2011 01:59 PM
Access, Visio & Autocad oo0tommyk0oo Visio 0 05-16-2011 02:46 PM
Using VBA to convert AutoCAD into WMF embed autocad drawing in word document prints very light gbehm@broan.com Drawing and Graphics 1 03-15-2011 08:05 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:06 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft