Modify vba code to VB.NET code for find and replace

Modify vba code to VB.NET code for find and replace

Anonymous
Not applicable
1,504 Views
2 Replies
Message 1 of 3

Modify vba code to VB.NET code for find and replace

Anonymous
Not applicable

I am trying to avoid get values from excel file for a vba code. Here I posted my code which altered from vba  to vb.net . Replace function not happening. Please tell your views to solve this...

VBA:
Sub ReplaceText()C
Dim obj As AcadObject
Dim ent As AcadEntity
Dim text As AcadText
DDOC = ThisDrawing.Name
DOC_PATH = ThisDrawing.Path
Set APPX = GetObject(, "Excel.Application")
tot_ent = ThisDrawing.ModelSpace.count
For i1 = 1 To 1000
If APPX.Cells(i1, 2).Value = "" Then GoTo 1000

For i2 = 0 To tot_ent - 1
type_ent = ThisDrawing.ModelSpace.Item(i2).ObjectName
If type_ent <> "AcDbText" Then GoTo 80
45 AUX04 = ThisDrawing.ModelSpace.Item(i2).handle
Set text = ThisDrawing.HandleToObject(AUX04)
If APPX.Cells(i1, 2).Value <> text.TextString Then GoTo 80

text.TextString = APPX.Cells(i1, 3).Value

TAG, LEFT DOC
80 ThisDrawing.Application.Update
Next i2
100 Next i1
1000 ThisDrawing.SaveAs DOC_PATH & "\REPLACE_TEXT.DWG" 'LOOK IN THE FOLDER (PATH)
MsgBox "SAVE THE DOC"
End Sub
vb.net:
<CommandMethod("NewDrawing", CommandFlags.Session)> _
    Public Sub ReplaceText()

        Dim oAutocad As AcadApplication = New AcadApplication
        Dim text As AcadText = Nothing
        Dim DDOC As AutoCAD.AcadDocument
        Dim DOC_PATH As String
        Dim tot_ent As Integer
        Dim type_ent As Object
        Dim obtype As String

        Dim find As String = "FIND ME"
        Dim replace As String = "REPLACE ME"

        oAutocad.Application.Documents.Open("D:\Test.dwg")
        DDOC = oAutocad.ActiveDocument
        DOC_PATH = oAutocad.ActiveDocument.Path

        tot_ent = DDOC.ModelSpace.Count
        For i1 = 1 To 1000
            For i2 = 0 To tot_ent - 1
                type_ent = DDOC.ModelSpace.Item(i2).ObjectName
                obtype = type_ent.GetType.ToString

                If obtype = "AcDbText" Then

                    If text.TextString = find Then
                        text.TextString = replace
                        DDOC.Application.Update()
                    End If
                End If
            Next i2
        Next i1
End Sub
0 Likes
1,505 Views
2 Replies
Replies (2)
Message 2 of 3

norman.yuan
Mentor
Mentor

I assume you are somewhat new in AutoCAD programming. Using VB.NET (or C#) as language is not important, rather it is which sets of AutoCAD API you use is the dictating factor, and if you choose VB.NET/C#, naturally, you should do AutoCAD .NET API programming, as opposed to using AutoCAD COM API with AutoCAD VBA. From the first line of your VB.NET code (<CommandMethod(....)> _), I can see you are doing AutoCAD .NET API DLL project. However, the rest of your code uses AutoCAD COM API to do things (obviously you added references to AutoCAD COM type libraries). Yes, you can mix AutoCAD COM API with AutoCAD .NET API, but it should be avoid at ALL COST. Especially when you just started AutoCAD .NET API programming, you SHOULD forget AutoCAD COM API at all. As matterof fact, the first line of code in the command method does not make sense at all:

 

Dim oAutocad As AcadApplication = New AcadApplication

Your code runs inside AutoCAD, where you NETLOADs your DLL, and then enter command "NewDrawing", then why you want to start another AutoCAD instance with that code?

 

Anyway, you should learn how to do things with AutoCAD .NET API (follow through AutoCAD .NET API labs, which you can search the net for it) before starting doing something meaningful. Again, forget AutoCAD COM API code.

 

Norman Yuan

Drive CAD With Code

EESignature

Message 3 of 3

Anonymous
Not applicable

Thanks for the reply and it guide me to get the right way..

Yes I am new to autocad programming.

Suggest me basics to learn AutoCAD .NET API.

Again Thanks in advance.

0 Likes