Retrieve texte from a Masters - vba

I'm working on Visio 2013 for a project and I'm new to VBA and Visio.
First, I wrote a function which imports a CSV file in the current visio document by creating first a string table texte of the CSV and then adding it in a shape:
Dim sp As Visio.Shape
Set sp = ActiveDocument.Pages(1).Drop(Visio.ActiveDocument.Masters("Puce 120"), 4, 10)
sp.Characters.Text = texte
Now I want to write the reverse function, from the shape to a CSV file but I can't find a way to access the text in my shape "Puce 120".
I wrote this:
Dim vsoMasters As Visio.Masters
Dim intMasterCount As Integer
Dim intCounter As Integer
Dim vsoMaster As Visio.Master
Set vsoMasters = ActiveDocument.Masters
intMasterCount = vsoMasters.Count
If intMasterCount > 0 Then
For intCounter = 1 To intMasterCount
If vsoMasters.Item(intCounter).Name = "Puce 120" Then
Set vsoMaster = vsoMasters.Item(intCounter)
End If
Next intCounter
Else
Debug.Print " No masters in document"
End If
Dim shap As Visio.Shapes
Set shap = vsoMaster.Shapes
Dim ch As Visio.Characters
ch = shap.Characters
But I don't find my text in ch. Can someone explain me how to retrieve it?

You can put and get text of a shape using shape.Text property.
Is there any reason for all other fancy stuff? That is, you can set shape text like this:
shape.Text = "Hello"
And get it back like this:
myText = shape.Text

Related

Why would my Libreoffice Basic script fail to make PDFs with working links?

I've written a script using Libreoffice Basic that pulls data from a calc file and puts it in a writer doc and then exports the doc as a PDF. While that works there is an unwanted effect, none of the links work in the PDF. If I manually export the PDF, the links work. How do I alter the script so that the PDFs created will have working links too?
Script follows (links to example PDFs at the end):
REM Making example pdfs based on writer file while pulling data from a calc file
Dim strPath As String
Dim objWriterDoc As Object
Dim objCalcDoc As Object
Dim objCalcSheet as Object
Dim objCalcCellForID as Object
Dim objCalcCellForName as Object
Dim rg as Object
Dim lngRows as Long
Dim args()
Dim PDFargs(1) As New "com.sun.star.beans.PropertyValue"
Dim Watermarkargs(4) As New "com.sun.star.beans.PropertyValue"
REM set PDF export args
PDFargs(0).Name = "FilterName"
PDFargs(0).Value = "writer_pdf_Export"
PDFargs(1).Name = "FilterData"
PDFargs(1).Value = Array(Array("UseLosslessCompression",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("Quality",0,70,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("ReduceImageResolution",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("MaxImageResolution",0,300,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("UseTaggedPDF",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("SelectPdfVersion",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("PDFUACompliance",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("ExportNotes",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("ViewPDFAfterExport",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("ExportBookmarks",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("OpenBookmarkLevels",0,1,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("UseTransitionEffects",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("IsSkipEmptyPages",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("ExportPlaceholders",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("IsAddStream",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("ExportFormFields",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("FormsType",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("AllowDuplicateFieldNames",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("HideViewerToolbar",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("HideViewerMenubar",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("HideViewerWindowControls",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("ResizeWindowToInitialPage",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("CenterWindow",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("OpenInFullScreenMode",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("DisplayPDFDocumentTitle",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("InitialView",0,1,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("Magnification",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("Zoom",0,100,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("PageLayout",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("FirstPageOnLeft",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("InitialPage",0,1,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("Printing",0,2,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("Changes",0,4,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("EnableCopyingOfContent",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("EnableTextAccessForAccessibilityTools",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("ExportLinksRelativeFsys",0,true,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("PDFViewSelection",0,0,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("ConvertOOoTargetToPDFTarget",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("ExportBookmarksToPDFDestination",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("SignPDF",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("_OkButtonString",0,"",com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("Watermark",0,"",com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("EncryptFile",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("PreparedPasswords",0,,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("RestrictPermissions",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("PreparedPermissionPassword",0,Array(),com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("SignatureLocation",0,"",com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("SignatureReason",0,"",com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("SignatureContactInfo",0,"",com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("SignaturePassword",0,"",com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("SignatureCertificate",0,,com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("SignatureTSA",0,"",com.sun.star.beans.PropertyState.DIRECT_VALUE),_
Array("UseReferenceXObject",0,false,com.sun.star.beans.PropertyState.DIRECT_VALUE))
REM open the writer file
strPath = ConvertToUrl("/home/wrg/Downloads/example.odt")
objWriterDoc = StarDesktop.loadComponentFromURL(strPath, "default", 0, args())
REM open the calc list
strPath = ConvertToUrl("/home/wrg/Downloads/example_list.ods")
objCalcDoc = StarDesktop.loadComponentFromURL(strPath, "default", 0, args())
objCalcSheet = objCalcDoc.Sheets(0)
REM select the cols of all the needed rows
PTListRanage = objCalcSheet.getCellRangeByName("A1:B10")
REM set watermark params
Watermarkargs(0).Name = "Text"
Watermarkargs(0).Value = "Init" ' Will change this to custom text based on the calc file '
Watermarkargs(1).Name = "Font"
Watermarkargs(1).Value = "Malgun Gothic Semilight"
Watermarkargs(2).Name = "Angle"
Watermarkargs(2).Value = 315 ' Degrees, int '
Watermarkargs(3).Name = "Transparency"
Watermarkargs(3).Value = 88 ' Percent, int '
Watermarkargs(4).Name = "Color"
Watermarkargs(4).Value = 12632256 ' FF0000 = Red; number only '
document = objWriterDoc.CurrentController.Frame
oDispHelper = createUnoService("com.sun.star.frame.DispatchHelper")
REM main loop of dataset
lngRows = PTListRanage.Rows.Count - 1
For r = 0 To lngRows
objCalcCellForID = objCalcSheet.getCellByPosition(0,r)
objCalcCellForName = objCalcSheet.getCellByPosition(1,r)
REM process the row if there is a name to process
If objCalcCellForName.String <> "" Then
REM set the WhoFor text field
if objWriterDoc.getTextFieldMasters.hasByName("com.sun.star.text.fieldmaster.SetExpression.WhoFor") then
oVar = objWriterDoc.getTextFieldMasters.getByName("com.sun.star.text.fieldmaster.SetExpression.WhoFor")
oVar.DependentTextFields(0).content = objCalcCellForName.String
else
msgbox "WhoFor not found"
end if
REM set the WhoForID text field
if objWriterDoc.getTextFieldMasters.hasByName("com.sun.star.text.fieldmaster.SetExpression.WhoForID") then
oVar = objWriterDoc.getTextFieldMasters.getByName("com.sun.star.text.fieldmaster.SetExpression.WhoForID")
oVar.DependentTextFields(0).content = objCalcCellForID.String
else
msgbox "WhoForID not found"
end if
REM add text watermark
Watermarkargs(0).Value = "For ID "+objCalcCellForID.String ' Which text will be shown as the watermark '
oDispHelper.executeDispatch(document, ".uno:Watermark", "", 0, Watermarkargs())
REM export to PDF format
strPath = ConvertToURL("/home/wrg/Downloads/example("+objCalcCellForID.String+").pdf")
objWriterDoc.storeToURL(strPath, PDFargs())
End If
Next r
REM close all the files we opened
objCalcDoc.close(True)
objWriterDoc.close(True)
Links to example PDFs, manually made with working links and script made links don't work. Also, here is a zip that includes PDFs, writer file, calc file, and bas file.

CATIA VB.net create chanfer by selected face

I'm trying to create a chamfer using pre selected faces in a macro. But i'm no having much sucess
what I have tried:
The faces are previously selected.
chamfer2 = shapeFactory1.AddNewChamfer(reference1, catTangencyChamfer, catLengthAngleChamfer, catNoReverseChamfer, 1, 45.0#)
Dim Num_Faces As Integer = selection1.count
Dim Faces_ref(Num_Faces)
For i = 1 To Num_Faces
Dim MyBRepName = (selection1.Item(i).Value.Name)
MyBRepName = Replace(MyBRepName, "Selection_", "")
MyBRepName = Left(MyBRepName, InStrRev(MyBRepName, "));"))
MyBRepName = MyBRepName + ");WithPermanentBody;WithoutBuildError;WithSelectingFeatureSupport;MFBRepVersion_CXR15)"
MsgBox(MyBRepName)
reference1 = Part1.CreateReferenceFromName(MyBRepName)
chamfer1.AddElementToChamfer(reference1)
Next
the error appears here:
chamfer1.AddElementToChamfer(reference1)
Try to remove brackets from reference1 like this:
chamfer1.AddElementToChamfer reference1

Array Out of bounds error VB

Sorry for the terrible wording on my last question, I was half asleep and it was midnight. This time I'll try to be more clear.
I'm currently writing some code for a mini barcode scanner and stock manager program. I've got the input and everything sorted out, but there is a problem with my arrays.
I'm currently trying to extract the contents of the stock file and sort them out into product tables.
This is my current code for getting the data:
Using fs As StreamReader = New StreamReader("The File Path (Is private)")
Dim line As String = "ERROR"
line = fs.ReadLine()
While line <> Nothing
Dim pos As Integer = 0
Dim split(3) As String
pos = products.Length
split = line.Split("|")
productCodes(productCodes.Length) = split(0)
products(products.Length, 0) = split(1)
products(products.Length, 1) = split(2)
products(products.Length, 2) = split(3)
line = fs.ReadLine()
End While
End Using
I have made sure that the file path does, in fact, go to the file. I have looked through debug to find that all the data is going through into my "split" table. The error throws as soon as I start trying to transfer the data.
This is where I declare the two tables being used:
Dim productCodes() As String = {}
Dim products(,) As Object = {}
Can somebody please explain why this is happening?
Thanks in advance
~Hydro
By declaring the arrays like you did:
Dim productCodes() As String = {}
Dim products(,) As Object = {}
You are assigning size 0 to all your arrays, so during your loop, it will eventually try to access a position that haven't been previously declared to the compiler. It is the same as declaring an array of size 10 Dim MyArray(10) and try to access the position 11 MyArray(11) = something.
You should either declare it with a proper size, or redim it during execution time:
Dim productCodes(10) As String
or
Dim productCodes() As String
Dim Products(,) As String
Dim Position as integer = 0
'code here
While line <> Nothing
Redim Preserve productCodes(Position)
Redim Preserve products(2,Position)
Dim split(3) As String
pos = products.Length
split = line.Split("|")
productCodes(Position) = split(0)
products(0,Position) = split(1)
products(1,Position) = split(2)
products(2,Position) = split(3)
line = fs.ReadLine()
Position+=1
End While

Getting the cell value of two string variables

I have a 2D chart in Excel. I need to get the value of a cell using two string variables. The chart looks like this:
Document person1 person2
Text1 5 8
Text2 2 1
Text3 9 6
After looking online I am finding this difficult because:
the values are strings, not integers;
the strings will change depending on which person and document combination comes up.
This should be the only code that is relevant:
Dim document as string
Dim person as string
Dim oExcel as excel.application
Dim oWB as workbook
Set oExcel = New Excel.application
Set oWB = oExcel.Workbooks.open. ("C:")
oExcel.Visible = True
oWB.Sheets ("sheet1").Cells(documemt, person)
Assuming that document and person are string variables that hold string representations of integers (e.g. document = "1", person = "2") then something like
oWB.Sheets ("sheet1").Cells(val(document), val(person))
will work. If the contents of the string variables are more complicated then you would need to do some parsing of those strings.
Assuming by "2d Chart" you mean a table in a Worksheet, and that person would be the full text "person1", or "person2", etc. and likewise for document, then perhaps this function will do the trick.
Function FindDocPerson(person As String, document As String) As Variant
Const MatchExact As Integer = 0
Dim ws As Excel.Worksheet
Set ws = ActiveWorkbook.Worksheets("Sheet1")
Dim table As Excel.Range
Set table = ws.UsedRange
Dim docRange As Excel.Range
Set docRange = table.Columns(1).Offset(1, 0).Resize(table.Columns(1).Rows.Count - 1)
Dim personRange As Excel.Range
Set personRange = table.Rows(1).Offset(0, 1).Resize(1, table.Columns.Count - 1)
Dim personIndex As Long
Dim docIndex As Long
On Error GoTo errHandler
personIndex = Application.WorksheetFunction.Match(person, personRange, MatchExact) + 1
docIndex = Application.WorksheetFunction.Match(document, docRange, MatchExact) + 1
FindDocPerson = table.Cells(docIndex, personIndex).Value2
Exit Function
errHandler:
FindDocPerson = VBA.CVErr(Excel.xlErrNA)
End Function
calling syntax:
Dim result As Variant
result = FindDocPerson("person2", "text1")
If Application.WorksheetFunction.IsError(result) Then
' handle it
Else
' found it
End If
There is a typo in your code,
oWB.Sheets ("sheet1").Cells(documemt, person)
documemt should be document
All that aside though it is unclear what you want to do, can you give a little more description please?
All we know is you need to get the value of a cell using two string variables and that it could be a string or a number. The code you posted doesn't give much more of a hint to your goal.
To convert between strings and numbers you can use CLng to convert to a long number or CStr to convert to a string. eg CLng("3") = 3 and CStr(3) = "3"
In your code this:
Set oWB = oExcel.Workbooks.open. ("C:")
Doesn't work because you are trying to open a workbook without specifying a name, I also note the ("C:") is spaced far to the right of the command call which leads me to believe this is has been typed freestyle ie not in the VBE. This makes it even harder to decode into your requirements.
Lastly, this code:
Set oExcel = New Excel.application
Why are you starting another session of Excel from Excel VBA code? Is this code somewhere other than Excel ie Outlook / Access / PowerPoint / Word / Business Objects etc etc.

How to copy a Visio shapesheet section between shapes in VBA

Is there a method available for copying a section out of a shape to another shape using VBA? I'm specifically trying to copy all the custom properties and user cells from one pagesheet to another page.
Unfortunately there isn't a simple method to do this. You will have to loop over all the rows in the source sheet and create the same rows in the destination sheet. E.g.:
Dim oPageSheet1 As Visio.Shape
Dim oPageSheet2 As Visio.Shape
Dim rowName As String
Dim i As Integer
Set oPageSheet1 = Visio.ActiveDocument.Pages.Item(1).PageSheet
Set oPageSheet2 = Visio.ActiveDocument.Pages.Item(2).PageSheet
i = visRowUser
While oPageSheet1.CellsSRCExists(visSectionUser, i, visUserValue, False)
oPageSheet2.AddNamedRow visSectionUser, oPageSheet1.Section(visSectionUser).Row(i).NameU, 0
oPageSheet2.Section(visSectionUser).Row(i).Name = oPageSheet1.Section(visSectionUser).Row(i).Name
oPageSheet2.CellsSRC(visSectionUser, i, visUserValue).FormulaU = oPageSheet1.CellsSRC(visSectionUser, i, visUserValue).FormulaU
oPageSheet2.CellsSRC(visSectionUser, i, visUserPrompt).FormulaU = oPageSheet1.CellsSRC(visSectionUser, i, visUserPrompt).FormulaU
i = i + 1
Wend
If you have to copy a large number of rows and performance is a consideration you should investigate using AddRows and SetFormulas.