Using ChatGPT to write VBA code for Visio app

Started by Surrogate, March 10, 2023, 12:01:59 PM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

Yacine

#15
Now this is cool.

Convert the following python code into VBA:
L = [[shp, shp.ID, shp.Cells('PinX')] for shp in ActivePage.Shapes if shp.CellExists('prop.A',False)]
I have tried the following:
<code>Dim L As Variant
Dim shp As Shape
Dim shpID As String
Dim shpPinX As String

For Each shp In ActivePage.Shapes
    If shp.CellExists("prop.A", False) Then
        L = shp
        shpID = shp.ID
        shpPinX = shp.Cells("PinX")
    End If
Next shp
</code>
But I am getting an error on the line:
<code>L = shp
</code>
The error is:
<blockquote>
<p>Run-time error '13': Type mismatch</p>
</blockquote>
I am not sure how to fix this.


A:

You need to declare L as an array of arrays.
<code>Dim L As Variant
Dim shp As Shape
Dim shpID As String
Dim shpPinX As String

For Each shp In ActivePage.Shapes
    If shp.CellExists("prop.A", False) Then
        ReDim Preserve L


I tried first with the default engine text-davinci-003 and it got the answer wrong by re-assigning L to the inner shp only. Switching to code-davinci-002 showed the result above.

And, Paul, it works exactly as myself, try and error. (Yacine == impressed) = true

This is why I don't agree with you, it does generate. And everybody is copying anyway.


Mind, only the 2 first lines are mine!
Yacine

Yacine

#16
Build a Visio solution where a user can drop a master on the drawing then define it by choosing a value from a list. The list must be generated dynamically from an external database. The ID defining this value should be displayed in the shape.

1. Create a Visio solution with the necessary shapes and stencils.
2. Connect the Visio solution to an external database with the necessary information.
3. Create a dropdown list in the Visio solution to allow users to select values from the external database.
4. Create a master shape in the Visio solution and add the dropdown list to it.
5. Define the logic for the dropdown list so that the selected value is displayed in the master shape.
6. Store the ID of the selected value in the master shape for future reference.


Still quite vague, and with errors (point 6), but hey for a start not bad.
That goes with Thomas' vision.
Yacine

Yacine

#17
Write a shapesheet solution using the Index function and the lookup function to map two lists. You choose the nth element in one list and get the value of the nth element in the second list.

Start with creating two lists of items. For example, List1 is [A,B,C,D] and List2 is [1,2,3,4].

Then, use the Index and Lookup functions to map the two lists.

To get the value of the nth element in List2 using the nth element of List1, use the following formula:
=LOOKUP(INDEX(List1,n),List1,List2)

Where n is the index of the element.

For example, to get the value of the third element in List2 using the third element of List1, use the following formula:
=LOOKUP(INDEX(List1,3),List1,List2)

This will return the value 3.


Not bad neither, but I needed to tell it to use LOOKUP. Without the hint it proposes only INDEX.
Also the answer is wrong, as the second argument (List1) is not needed.
Yacine

Surrogate


wapperdude

#19
Ugh.  That was long.  Based upon Fig 1, the higher your education level, the greater your vulnerability.  There's a motivator for learning.  (Yeah, I like yanking chains. 😎)
Visio 2019 Pro

along

I just asked it to create a macro I needed for work, and it did so quite impressively.

My Prompt:
please write a VBA macro for visio, which collects the texts of all the shape in a page into a long string (with a separator between the text of different shapes: ||| ), creates a new textbox, and place the long string as the text of the textbox, in red

The only think it got wrong is that the line which attempted to make the text red didn't work, so I deleted it (wasn't very important anyway, just for me to easily detect).

Sub CollectShapesText()
   
    ' Declare variables
    Dim page As Visio.Page
    Dim shape As Visio.Shape
    Dim textBox As Visio.Shape
    Dim textString As String
   
    ' Get the current page
    Set page = ActivePage
   
    ' Loop through all shapes on the page
    For Each shape In page.Shapes
       
        ' Check if shape has text
        If shape.Characters.Text <> "" Then
           
            ' Add the shape's text to the string with separator
            textString = textString & shape.Characters.Text & "|||"
           
        End If
       
    Next shape
   
    ' Create a new text box on the page
    Set textBox = page.DrawRectangle(1, 1, 3, 3)
   
    ' Set the text box's text to the collected shape text in red
    With textBox
        .Characters.Text = textString
        .CellsSRC(visCharacter, visCharacterColor).FormulaU = "RGB(255,0,0)"
    End With
   
End Sub

Surrogate

#21
Quote from: along on March 23, 2023, 01:43:39 PM.CellsSRC(visCharacter, visCharacterColor).FormulaU = "RGB(255,0,0)"
CellsSRC property need 3 parameters: Section, Row, Column!
Correct synax is
.CellsSRC(visSectionCharacter, 0, visCharacterColor).FormulaU = "RGB(255,0,0)"

Surrogate

#22
Quote from: along on March 23, 2023, 01:43:39 PMMy Prompt:
please write a VBA macro for visio, which collects the texts of all the shape in a page into a long string (with a separator between the text of different shapes: ||| ), creates a new textbox, and place the long string as the text of the textbox, in red
I repeated your request and got a different answer. True, also with an error, but in a different place.
Public Sub CollectAndDisplayText()

    Dim currentPage As Visio.Page
    Set currentPage = ActivePage
   
    Dim shapeText As String
    shapeText = ""
   
    Dim shape As Visio.shape
    For Each shape In currentPage.Shapes
        shapeText = shapeText & shape.Text & "|||"
    Next shape
   
    ' Create a new textbox shape
    Dim textShape As Visio.shape
    Set textShape = currentPage.Drop(Application.Documents.Add("Text.vst").Masters("Text"), 5, 5) ' this line dont works (cant add new shape, from some template ?)
   
    ' Set the text of the new text shape to the concatenated shape text
    textShape.Text = shapeText
   
    ' Set the text color to red
    textShape.Cells("Char.Color").FormulaU = "RGB(255,0,0)" ' this line change font color correct
   
End Sub

Yacine

I clearly relies on examples found in the net. It is not yet clever enough to know that Text.vst is not available to everyone.
Our community is too small. Questions relating to Excel are certainly better answered.
Yacine

Surrogate

Quote from: Yacine on March 23, 2023, 03:49:06 PMOur community is too small.
You're right!
Quote from: Yacine on March 23, 2023, 03:49:06 PMQuestions relating to Excel are certainly better answered.
But we can teach ChatGPT!
I recently asked a question:

Surrogate: Strsame ShapeSheet.

ChatGPT: "Strsame" and "ShapeSheet" are two different things from different areas of programming.

"Strsame" is a C programming language function that is used to compare two strings for equality. It is prototyped by int strsame(char *s, char *t) and returns 1 if the strings s and t match, and 0 otherwise.

"ShapeSheet" is a technology that is used in Microsoft Visio and Microsoft Excel to create and edit graphical objects and diagrams. A ShapeSheet is a table that contains cells that can contain data about an object's shape, size, color, and other properties. ShapeSheet cells can be used to control the display of objects, as well as to make connections between objects.

I gave a link to an article about Visio's STRSAME function, asked what the article was about? didn't give a brief description.

Surrogate: no contradiction?

ChatGPT: My apologies, I made a mistake in my previous reply. Indeed, the link you provided describes the STRSAME function of the ShapeSheet language used to compare two lines. I apologize for the confusion and incorrect information in the previous post.


... Today I asked the same question


Surrogate: Strsame ShapeSheet

ChatGPT: The STRSAME function is one of the ShapeSheet functions in Microsoft Visio and is used to compare two lines to determine if they are identical. The syntax of the STRSAME function is as follows: STRSAME(string1, string2), where string1 and string2 are the strings being compared.

The function returns TRUE if the two strings are identical and FALSE if they are different. The comparison of strings is case-sensitive, that is, the string "abc" and the string "ABC" will be different.

For example, if you want to check if the string contained in the "Prop.Row_1" cell is identical to the "Hello" string, you can use the following expression in the ShapeSheet formula cell:
STRSAME(Prop.Row_1, "Hello")
This expression will return TRUE if the value of the cell "Prop.Row_1" is "Hello", and FALSE otherwise.


Visisthebest

If ChatGPT can give a clear, understandable-for-everyone explanation of SETATREF() it is a genius!
Visio 2021 Professional

Surrogate

Not always the "learning" stays in the memory.

About week ago i asked about our russian speaking Visio forum. Few times i get answers like visiofan.ru or visiofun.ru (these sites never existed).

Surrogate: https://visio.getbb.ru/index.php what is it about ?

ChatGPT: This site is a forum dedicated to Microsoft Visio. It is created for Visio users to communicate, share experience and knowledge about Visio. At the forum you can ask questions, share your findings, find solutions to problems related to Visio use. The site also features sections with information about news, updates, add-ons and useful hints for users.

Surrogate: how do you think it can be considered a Russian forum on Visio?

ChatGPT: Yes, forum https://visio.getbb.ru/index.php can be considered the Russian-speaking forum devoted to Visio subjects. On this forum users can communicate in Russian, ask questions, share experience and knowledge about Visio. There are also sections on the site with information about news, updates and useful tips on using the program.

Surrogate: remember it please

ChatGPT: Okay, I'll remember the forum https://visio.getbb.ru/index.php as a Russian-language forum dedicated to Visio topics. If you have any other questions, please feel free to contact me!


Now he offers visiofan.ru/visiofun.ru and forums on which contain Visio-related sections.

Surrogate

Quote from: Visisthebest on March 27, 2023, 10:26:09 AMit is a genius!
New experiments:

Surrogate: /ask most visited visio forum in the world

ChatGPT: It's difficult to determine the most popular Visio forum in the world, as there are many forums and online communities dedicated to Visio, and their popularity may depend on factors such as language, region, and user base. However, some of the most popular and active Visio forums include:

1. Microsoft Tech Community - Visio: A community forum for Visio users hosted by Microsoft.
2. Visio Guy: A forum for Visio users featuring tips, tutorials, and discussion on a range of topics.
3. Stack Overflow: A popular question-and-answer site for programming and software development, including Visio-related topics.
4. Reddit: The r/Visio subreddit is an active community for Visio users to share tips, ask questions, and discuss Visio-related topics.

These are just a few examples; there are many other Visio forums and communities online that you can explore.
I was especially amused that Microsoft Tech Community was at the top of the list!

Visisthebest

#28
ChatGPT obviously never develops for Visio because the Visguy forum is the one that rules them all!
Visio 2021 Professional

hidden layer

Quote from: Yacine on March 23, 2023, 03:49:06 PM
Our community is too small.
and as more precise chatGPT is, the smaller this community here will be, isnt it?