"Compenent" elements seen as "ProductDocument" in CATIA VBA - vba

I have a problem. I can import feature types in catia vba with the following codes
Set document1 = CATIA.Documents.Item("Product1.CATProduct")
Set products1 = document1.Product.Products
For i = 1 To products1.Count
MsgBox TypeName(products1.Item(i).ReferenceProduct.Parent)
Next i
but it also shows "Compenent" elements as "ProductDocument". If component is, I want to make a separate operation.
How can I find out that an element is a "Component" ?
enter image description here

I found solution
For Each prd In products1
If prd.ReferenceProduct.Parent.Product.PartNumber <> prd.PartNumber Then
'... this is a component
End If
Next

Related

Loop to get the name of every class selenium vba

Actually Im trying to go through every "class" defined, and get his content (text), I got a code that works fine, but it just take the text from one class, and it doesn't continues with the other one.
Its repeats the action 7 times, but just with the first one class and doesn't show any error or something that can give me a clue of what's wrong on the code.
Im kinda new on this, but is part of my hobby learn about VBA.
Im practicing with https://www.tecnoglobal.cl/tiendaonline/webapp/componentes-pc/placas-madre/246
Here is the code:
Dim bot As New WebDriver, myproducts As WebElements, myproduct As WebElement, i As Integer, productnum As String, clicknum As Integer, mysheet As Worksheet
i = 2
Set mysheet = Sheets("example2")
Set myproducts = bot.FindElementsByXPath("//h1[#class='minificha__nombre-producto']")
For Each myproduct In myproducts
If myproduct.FindElementByXPath("//span[#class='minificha__sku ng-binding']").Text <> "" Then
mysheet.Cells(i, 1).Value = myproduct.FindElementByXPath("//span[#class='minificha__sku ng-binding']").Text
i = i + 1
End If
Next
MsgBox ("complete")
End Sub
Thanks you very much in advance.
I think you want following sibling. The element you are after is not a child of myproduct. It is the adjacent sibling (same level next element).
myproduct.FindElementByXPath("following-sibling::span").Text

How to use multiple conditions in VBA

In the following code I am trying to look for a text box in an open word document. Once that text box has been found I would also like to check to make sure the correct text is inside. This is the code that does not work and I could not figure out why.
Dim i as Integer
for i = 1 to Application.ActiveDocument.Shapes.Count
if Application.ActiveDocument.Shapes(i).Name = "Text Box 2" and _
instr(Application.ActiveDocument.Shapes(i).TextFrame.TextRange.Text, "[Grab your reader") then
` Execute
end if
next i
Now if I replace the and with another if statement it works.
Dim i as Integer
for i = 1 to Application.ActiveDocument.Shapes.Count
if Application.ActiveDocument.Shapes(i).Name = "Text Box 2" then
if instr(Application.ActiveDocument.Shapes(i).TextFrame.TextRange.Text, "[Grab your reader") then
` Execute
end if
end if
next i
From what I understand the and should work exactly the same as adding a new if statement. If anyone could tell me why this happens that would be great.

How to pull data from a website with Excel VBA

I am trying to write VBA codes to pull the price of a product from a website. In order to this, I turned on the "Microsoft HTML Object Library" and "Microsoft Internet Controls" in VBA References. However, when I get up to the point to search the of the item that attaches the price, the codes failed. Appreciate if anyone can provide a solution for it.
Below is the link to the sample webpage that I would like to copy price from.
Link
Below is my initial codes:
Sub Update()
Dim IE As New InternetExplorer
IE.Visible = False
IE.navigate "http://www.chemistwarehouse.com.au/buy/36985/Reach-Dentotape-Waxed-20m"
Do
DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Dim Doc As HTMLDocument
Set Doc = IE.document
Dim getprice As String
getprice = Trim(Doc.getElementsByTagName("div class="Price" itemprop="price"").innerText)
Worksheets("Sheet1").Range(C1).Value = getprice
End Sub
The function getElementsByTagName() requires a tag name only as parameter:
e.g. getElementsByTagName("div")
Try getElementsByClassName() instead:
getprice = Trim(Doc.getElementsByClassName("Price").Item.innerText)
There were a few issues with the above code.
Issue 1
getprice = Trim(Doc.getElementsByTagName("div class="Price" itemprop="price"").innerText)
This:
div class="Price" itemprop="price" isn't a TagName. TagNames are things like Input, IMG, Anchors, etc. However, we can see the Class attribute for the price element you are interested in. We can change how we select this element by doing:
getprice = Trim(Doc.getElementsByClassName("Price")(0).innerText)
You may notice (0) at the end of the element selection. This is to indicate which element is being selected of the Price ClassName collection. getElementsByClassName returns multiple elements, the first element being 0.
Issue 2
Worksheets("Sheet1").Range(C1).Value = getprice
I don't C1 referenced anywhere. One way to reference a specific cell, is to use a String to represent the range. From your code this becomes:
Worksheets("Sheet1").Range("C1").Value = getprice

Getting .value property when using a string and variable

I am creating a form in Access which will be used as an order sheet for classroom materials. I have the available resources listed and a text box next to the resource where the user inputs the quantity they desire.
My VBA code checks to see if any entries have been made by using the following. (I am using Nz() to allow for Null results):
QuantCheck = Nz(Box1.Value, 0) + Nz(Box2.Value, 0) + Nz(Box3.Value, 0)
Where "QuantCheck" is the variable I am using in the IF statement which begins the workflow:
If QuantCheck > 0 Then
I would like to clean this up by using some kind of loop statement, however I am not able to extract the .value from a string. I would love something like the following which I could incorporate into a loop:
"Box"&VariableNumber.Value
From what I can tell, I am not able to use a string (concatenated or otherwise) as the base for the .value call.
It is interesting that there is a way to accomplish this when using a SQL statement. I have this elsewhere in the code which works nicely:
SQLStr = "INSERT INTO OrderRequests VALUES (cbSchool, txtName, Title" & x & ".caption, Box" & x & ")"
Here I have a variable "x" which increases with each loop to change the Title line, and the Box line.
Any help is appreciated.
I suggest you use the Tag property of the controls. Put "QuantCheck" in the Tag property of any control you want to include. Then
Function QuantitiesExist(frm As Form) As Boolean
Dim Ctrl As Control
Const sQUANTCHK As String = "QuantCheck"
For Each Ctrl In frm.Controls
If Ctrl.Tag = sQUANTCHK Then
If Nz(Ctrl.Value) > 0 Then
QuantitiesExist = True
Exit For
End If
End If
Next Ctrl
End Function
Now you get self documenting code
If QuantitiesExist(Me) Then
And when you add/delete/change controls, you don't have to edit your code. Just set up new controls with the proper tags.
You could loop through the control on the for checking the names and then if it is the one you wanted take an action on it, is this what you was thinking of?
Dim Ctrl As Control
For Each Ctrl In Me.Controls
If Ctrl.Name = "TxtPath" Then ' "Box" & VariableNumber Then
MsgBox Ctrl.Value
End If
Next

how to use Microsoft.Office.Interop.Word to get a list of externally linked objects with vb.net?

I am trying to read a word document and get a list of ole objects / or any exteranlly linked object (with VB.Net) that is being used in that word document.. Along with that I want to replace it with my own static text.. any idea? I have already tried to use ContentControls but perhaps i am not using it right? any suggestions?
Time to resurrect the dead....
For Each oCtl As Shape In thisDocument.Shapes
If oCtl.Type = Microsoft.Office.Core.MsoShapeType.msoTextBox Then
Msgbox("Textbox")
ElseIf oCtl.Type = Microsoft.Office.Core.MsoShapeType.msoEmbeddedOLEObject Then
Msgbox("Embedded Object")
ElseIf oCtl.Type = Microsoft.Office.Core.MsoShapeTypemso.LinkedOLEObject Then
Msgbox("Linked Object")
End If
Next
Here is a list of all the MSO shapes...
https://msdn.microsoft.com/en-us/library/microsoft.office.core.msoshapetype.aspx