Message 1 of 3
Modify vba code to VB.NET code for find and replace
Not applicable
01-06-2020
03:26 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
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