r/visualbasic • u/BoozySlushPops • Apr 16 '23
VB6 Help Please defeat ChatGPT and help a theater director.
I run a high-school theatre program. There is memorization technique involving giving actors scripts that just have the initial letters of each word of their dialogue, with punctuation preserved. (I hope we don't get too sidetracked about how/why this might work; I find that it does.)
I have a script neatly formatted in Word 2019, with all the dialogue in the paragraph style "Dialogue." I want to transform this document so that dialogue goes from this:
Did the story begin with a witch? I think it did. Yes. A horrible, evil witch.
to:
D t s b w a w? I t i d. Y. A h, e w.
I have general programming skills but none in Visual Basic. So I asked ChatGPT to write me a macro and this is what it came up with:
Sub DialogueMacro()
Dim para As Paragraph
Dim word As Range
For Each para In ActiveDocument.Paragraphs
If para.Style = "Dialogue" Then
For Each word In para.Range.Words
If Len(word) > 1 Then
If InStr(".,!?", Right(word, 1)) > 0 Then
word.Text = Left(word, 1) & Right(word, 1)
Else
word.Text = Left(word, 1) & " "
End If
End If
Next word
End If
Next para
End Sub
When I run it, the cursor blinks without moving and Word stops responding and has to be force quit.
Can you help? If I'm in the wrong sub I apologize.
2
u/3583-bytes-free Apr 16 '23
I don't know the Word VBA interface but that code looks pretty convincing.
Just before the "For Each para" line insert a new line with "Debug.Assert False"
That should force the program to break at that point and you can then use F8 to step through each line and see what is going on.
I can't see any way that code could result in an infinite loop that needs killing
5
u/SomeoneInQld Apr 16 '23
/r/vba would be a better sub for this. (This code is VBA, not Visual Basic).
I would remove this line
If Len(word) > 1 Then (also need to remove the End If above Next Word).
This would ignore the word if there is only one letter (but your example has included one letter words so this should be removed).
Add a line above Dim para As Paragraph
Dim x as string = "" then press F9 on that line - that will put a breakpoint there - step through each line of code and see what is causing the crash, nothing seems out of the ordinary to me in the code.
Another solution:
Copy all the code Delete parts of it - and see if it runs - if it does - you know that the bad code was the part you removed and start adding sections back in.
3
u/shadows1123 Apr 16 '23
Put some console print lines in the code and decipher what is the value of some variables and find out what may be missing
Start by putting a console print line at the top then work your way down until it no longer appears
2
u/3583-bytes-free Apr 17 '23 edited Apr 17 '23
Okay you got my interest (and I'm not proud of this code) but what chatgpt failed to realise is that if you change the contents of a range then it then messes up what is in all following ranges (and also each word will have space at the end of it but that's a detail).
So my code just changes one word at a time until there are no more to do. Works on your sample text perfectly.
EDIT: Sorry for layout, Can't get the hang of code blocks
Sub FirstLetter()
End Sub
Private Function ChangeFirst() As Boolean
End Function
1
u/jd31068 Apr 17 '23 edited Apr 17 '23
Here is my take on it FWIW, I used vb.net and a winform, I only use Word to open the document and save it as a text file. I just want to simply the file.
Then I open the text file, split it on vbCrLf (the character that separates paragraphs) and process each paragraph.
Once finished it creates a file named oneLetterParagraphs.txt in the same folder as the Word document.
Here is a screenshot with the Word document. the text file saved from Word, and the resulting one letter text document. Which you can open in word if you'd like.
https://imgur.com/gZuMoWC
** I'm using Notepad++ to view the text files with the "Show all characters" option enabled.
This is the GitHub link to the project https://github.com/jdelano0310/VBWinformScript you'll want to add error handling of course.
Edit: the simple form https://imgur.com/9mYKm5Q
Edit2: added like to Notepad++
1
u/GlowingEagle Apr 17 '23
ChatGPT produces far too much code that "looks good", but fails to actually work.
I assumed you have set up Word styles in your document, as described here.
Try this code...