AnsweredAssumed Answered

Why does macro change note formatting?

Question asked by Keaton Warn on Jun 7, 2019
Latest reply on Jun 11, 2019 by Keaton Warn

I have some VBA code that I'm using to find the tolerance values in a non-custom-property-linked tolerance note on drawings I'm converting title blocks on. The problem is that it seems like the code destroys some kinds of formats in various notes as it's searching for the text string.

 

Here is the main offender before and after running the macro:

 

 

 

Here's the code. It's messy and a part of a larger module but I think this should run on its own.

Sub SearchNotesForTolerances()

Const NumStrings As Long = 3
Dim initString(NumStrings) As String
Dim newString(NumStrings) As String

Dim swAnn As SldWorks.Annotation

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc
Dim swDraw As SldWorks.DrawingDoc
Dim swView As SldWorks.View
Dim swNote As SldWorks.Note
Dim sNoteTExt As String
Dim nTextCount As Long
Dim i As Long

Dim blPlusMinus As Boolean

'InitStrings

Set swApp = CreateObject("SldWorks.Application")
Set swModel = swApp.ActiveDoc
Set swDraw = swModel
'swModel.EditTemplate
Set swView = swDraw.GetFirstView ' This is the drawing template

'STRING TO FIND:
Dim strFind As String
strFind = ".XXX"
'NAME OF NOTE THAT CONTAINS strFind:
Dim strNoteName As String
Dim strNoteTexttoChange As String

While Not swView Is Nothing
Set swNote = swView.GetFirstNote
While Not swNote Is Nothing
If swNote.IsCompoundNote Then

nTextCount = swNote.GetTextCount
For i = 1 To nTextCount
sNoteTExt = swNote.GetTextAtIndex(i)
'DoReplaceString sNoteText
Debug.Print sNoteTExt
Debug.Print " "
swNote.SetTextAtIndex i, sNoteTExt

If InStr(1, sNoteTExt, strFind, vbTextCompare) <> 0 Then
Debug.Print "Found It 1"
Set swAnn = swNote.GetAnnotation
Debug.Print swAnn.GetName
strNoteName = swAnn.GetName
strNoteTexttoChange = sNoteTExt

If InStr(1, sNoteTExt, "+/-", vbTextCompare) <> 0 Then
blPlusMinus = True
Else
blPlusMinus = False
End If
Else
Debug.Print "Didn't Find It 1"
End If
Next i

Else
sNoteTExt = swNote.GetText
'DoReplaceString sNoteText
Debug.Print sNoteTExt
swNote.SetText sNoteTExt

If InStr(1, sNoteTExt, strFind, vbTextCompare) <> 0 Then
Debug.Print "Found It 2"
Set swAnn = swNote.GetAnnotation
Debug.Print swAnn.GetName
strNoteName = swAnn.GetName
strNoteTexttoChange = sNoteTExt

If InStr(1, sNoteTExt, "+/-", vbTextCompare) <> 0 Then
blPlusMinus = True
Else
blPlusMinus = False
End If

Else
Debug.Print "Didn't Find It 2"
End If
Debug.Print " "
End If

Set swNote = swNote.GetNext
Wend

Set swView = swView.GetNextView
Wend

'swModel.EditTemplate

'For Drawing Conversion:
Dim position As Long
Dim trimLength As Long

' 'Method One:
' strXXMain = Mid(strNoteTexttoChange, 98, 5)
' strXXXMain = Mid(strNoteTexttoChange, 145, 5)
' Debug.Print "strXXMain: "; strXXMain
' Debug.Print "strXXXMain: "; strXXXMain

'Method Two:
If blPlusMinus = False Then 'aka if ± is there
position = InStr(1, strNoteTexttoChange, ".XX", vbTextCompare)
position = position + 15
strXXMain = Mid(strNoteTexttoChange, position, 6)

position = InStr(1, strNoteTexttoChange, ".XXX", vbBinaryCompare)
position = position + 14
strXXXMain = Mid(strNoteTexttoChange, position, 6)
ElseIf blPlusMinus = True Then
position = InStr(1, strNoteTexttoChange, ".XX", vbTextCompare)
position = position + 9
strXXMain = Mid(strNoteTexttoChange, position, 6)

position = InStr(1, strNoteTexttoChange, ".XXX", vbBinaryCompare)
position = position + 9
strXXXMain = Mid(strNoteTexttoChange, position, 6)
Else
Debug.Print "± or +/- strings not found"
End If

Debug.Print "blPlusMinus is: "; blPlusMinus
Debug.Print "strXXMain: "; strXXMain
Debug.Print "strXXXMain: "; strXXXMain
Debug.Print "~~~~~~~~~~~~~~~~~~~~~~~~~~~"

End Sub

Outcomes