Community
AutoCAD Forum
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Excel VBA to Autocad, Find and Replace

1 REPLY 1
Reply
Message 1 of 2
dasd3213e21
3781 Views, 1 Reply

Excel VBA to Autocad, Find and Replace

Hi!

 

I need to create a rather simple function (I thought) but I haven't found anything really useful on the internet.

 

We have a excel document that we uses for project management (mechanical and electrical drawings).

Based on the data in the excel file I need to make some text changes in the dwg files.

 

Right now we are doing it manually via the find m (search / replace) but it´s to time ineffective.

 

 

So what I need to do from excel.

 

1. Enter the search and replace values (let´s say column A and B)

2. Press my Go button

3. A dwg is opend (my template)

4. Get all objects and find texts and attributes that fits the search

5. Replace them with the text from excel

 

I know how to open the dwg but I have no idea how to perform the search and replace.

 

 

 

Option Explicit

Sub FindReplace()
  
    'Declaring the necessary variables.
    Dim acadApp                 As Object
    Dim acadDoc                 As Object
    Dim searchString			As String
	Dim replaceString			As String
	
	searchString = "xRefObjectNumber"
	replaceString = "M200"
	
    'Check if AutoCAD application is open. If is not opened create a new instance and make it visible.
    On Error Resume Next
    Set acadApp = GetObject(, "AutoCAD.Application")
    If acadApp Is Nothing Then
        Set acadApp = CreateObject("AutoCAD.Application")
        acadApp.Visible = True
    End If
    
    'Check (again) if there is an AutoCAD object.
    If acadApp Is Nothing Then
        MsgBox "Sorry, it was impossible to start AutoCAD!", vbCritical, "AutoCAD Error"
        Exit Sub
    End If
    On Error GoTo 0
    
    'If there is no active drawing create a new one.
    On Error Resume Next
    Set acadDoc = acadApp.ActiveDocument
    If acadDoc Is Nothing Then
        Set acadDoc = acadApp.Documents.Add
    End If
    On Error GoTo 0

   
	' Now What????!!! :)

   
   
    
    'Release the objects.
    Set acadDoc = Nothing
    Set acadApp = Nothing
    
End Sub

 

 

Please help me.

 

Best Regards

 

1 REPLY 1
Message 2 of 2

Hi,

 

>> Now What????!!! 🙂

Scan through all blockdefinitions in the blocktable ( acaddoc.Blocks )

Within each blockdefinition (that is no reference object like XRef) scan through all elements and see if it's a text, a mtext, a dimension or a blockreference containing attributes ... and if it's such an objects search for the textcontent and see if there is anything to change.

 

At least one additional info: there is a special forum for VBA >>>there<<< (for your next questions if you have any) 😉

 

Good luck, - alfred -

------------------------------------------------------------------------------------
Alfred NESWADBA
Ingenieur Studio HOLLAUS ... www.hollaus.at ... blog.hollaus.at ... CDay 2024
------------------------------------------------------------------------------------
(not an Autodesk consultant)

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost