Find Replace VBA

Find Replace VBA

Anonymous
Not applicable
1,559 Views
4 Replies
Message 1 of 5

Find Replace VBA

Anonymous
Not applicable
So far I have had good luck finding snippets of code here by searching the forums. Although I am having a hard time finding any examples of how to use find and replace in VBA for AutoCAD 2007. It seems that most of the links people have posted up are outdated and don't work anymore.

Can someone help me out here and just post a little example or tell me what to search for to find and replacing text inside of drawings.

Thanks
Zuccus
0 Likes
1,560 Views
4 Replies
Replies (4)
Message 2 of 5

Anonymous
Not applicable
Here is one I 've collected from this NG
Tested on A2005 only but I am sure
that it will works for you too

Make sure to reference on current
version ObjectDBX Library

Option Explicit

'' Requires: (include usual DLLs)
'' AutoCAD/ObjectDBX Common 17.0 Type Library
'' Microsoft Scripting Runtime

'' |||||||||||||||||||||||''
'' |Tested on A2005 only |''
'' |||||||||||||||||||||||''

Public Function BrowseForFolderF(ByVal msg As String) As String
Dim oBrowser, folderObj, folderAcpt As Object
Dim folderStr As String

Set oBrowser = ThisDrawing.Application.GetInterfaceObject("Shell.Application")
Set folderAcpt = oBrowser.BrowseForFolder(vbOKOnly, msg, vbDefaultButton3, 0)

With folderAcpt
Set folderObj = .Self
folderStr = folderObj.path
End With
Set folderObj = Nothing
Set folderAcpt = Nothing
Set oBrowser = Nothing
BrowseForFolderF = folderStr

End Function
Public Function CheckFolder(ByVal strPath As String) As Variant
Dim objFolder ''As Scripting.Folder
Dim objFile ''As Scripting.File
Dim objSubdirs ''As Scripting.Folders
Dim objLoopFolder ''As Scripting.Folder
Dim varFs() As Variant
Dim m_objFSO, n, m_lngFileCount
Debug.Print "Checking directory " & strPath

Set m_objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = m_objFSO.GetFolder(strPath)
'
' Check files in this directory
'
n = -1
For Each objFile In objFolder.Files
If UCase$(Right$(objFile.ShortPath, 4)) = ".DWG" Then
m_lngFileCount = m_lngFileCount + 1
n = n + 1
ReDim Preserve varFs(n)
varFs(n) = objFile.path
End If
Next objFile

' Loop through all subdirectories and
' do the same thing.
'
Set objSubdirs = objFolder.SubFolders
For Each objLoopFolder In objSubdirs
CheckFolder objLoopFolder.path
Next objLoopFolder

Set objSubdirs = Nothing
Set objFolder = Nothing
CheckFolder = varFs
End Function
Sub BatchReplace()
Dim oText As AcadText
Dim objFnd As Object
Dim indx As Integer
Dim iFiles() As Variant
Dim m_objFSO
Dim fold, DwgName, a, b, c, d, e, nm As String

fold = BrowseForFolderF("Where are my dwg files?")
Set m_objFSO = CreateObject("Scripting.FileSystemObject")

iFiles = CheckFolder(fold)

a = InputBox(vbCrLf & "Enter a string to search:", "String to search")
b = InputBox(vbCrLf & "Enter an old string" & vbNewLine & _
"(must be the same as previous) :", "String to find")
c = InputBox(vbCrLf & "Enter a new string:", "String to find")

Dim oDbx As New AxDbDocument

Set oDbx = GetInterfaceObject("ObjectDBX.AxDbDocument.17") '' or 16 for A2005-6

On Error Resume Next
For indx = LBound(iFiles) To UBound(iFiles)
DwgName = iFiles(indx)
oDbx.Open DwgName
For Each objFnd In oDbx.ModelSpace
nm = objFnd.ObjectName
If nm = "AcDbText" Then
Set oText = objFnd
d = oText.TextString

If StrComp(d, a, 1) = 0 Then
e = replaceStr(a, b, c, False)
oText.TextString = e
oText.Update
End If
End If
Next objFnd

oDbx.SaveAs DwgName

Debug.Print Err.Number & Err.Description
Set oDbx = Nothing

Next indx
End Sub

' borrowed by www.ActiveDWG.com
Public Function replaceStr(ByVal searchStr As String, ByVal oldStr As String, ByVal newStr As String, ByVal firstOnly As Boolean) As String

If searchStr = "" Then Exit Function
If oldStr = "" Then Exit Function

replaceStr = ""
Dim i As Integer, oldStrLen As Integer, holdStr As String, StrLoc As Integer
oldStrLen = Len(oldStr)
StrLoc = InStr(searchStr, oldStr)
While StrLoc > 0
holdStr = holdStr & Left(searchStr, StrLoc - 1) & newStr
searchStr = Mid(searchStr, StrLoc + oldStrLen)
StrLoc = InStr(searchStr, oldStr)
If firstOnly Then replaceStr = holdStr & searchStr: Exit Function
Wend
replaceStr = holdStr & searchStr

End Function

~'J'~
0 Likes
Message 3 of 5

Anonymous
Not applicable
Forgot to say about
test it on copy of your folder first

~'J'~
0 Likes
Message 4 of 5

Anonymous
Not applicable
Fatty,

Thanks this will get me going in the right direction. I ran it on some test files, and it didn't actually find anything to change. It seems to run perfect with no error messages. Neverless it is what I am looking for.

Zuccus
0 Likes
Message 5 of 5

Anonymous
Not applicable
There are might be other solution how
to find strings
Do you want to substitute the whole word
say separate word (whole text string) or
you want to substitute the substing in the text
string?
And one more question:
Is there need to find/ replace string in all
layouts or in the model space only?

See you tomorrow

~'J'~
0 Likes