John sorry for a late reply, here's what I came up with:
Private Sub modifySubShapes(ByVal vShape As Visio.Shape)
Dim vChar As Visio.Characters
Dim cell As Visio.Cell
Dim sec As Visio.Section
Dim fieldCodeID As Short
Dim feildtype As Short
Dim AutoPath As String
Dim AutoDate As String
Dim t As Short
Dim num As Integer = 8
Dim intret As Integer
Dim sss As Short
Dim r As Integer
Try
Select Case AutoPathStr
Case "Mark with AUTOPATH"
AutoPath = "AUTOPATH"
Case "Blank"
AutoPath = ""
End Select
Select Case AutoDateStr
Case "Mark with AUTODATE"
AutoDate = "AUTODATE"
Case "Blank"
AutoDate = ""
Case "Mark with Date last modified"
AutoDate = VisioDateTimeLastModified
End Select
'unprotect textedit protected shapes.
UnlockTextEdit(vShape)
Application.DoEvents()
'getting the characters property of the vShape Shape, and
return
characters object.
vChar = vShape.Characters 'Characters object of shape object.
Dim chr As String = vChar.Text 'getting the text property of
the
characters object.
'isField if 0 contains characters in addition to the expanded
text of a field.
'isField if -1 represents only the expanded text of a field.
t = vChar.IsField
If t = -1 Then 'contains expanded text only of a field.
feildtype = vChar.FieldCategory
fieldCodeID = vChar.FieldCode
'Testing for field type(date\time) and specified field type
(current date field).
If feildtype = 1 And fieldCodeID = 2 Then 'visFCatDateTime
[visFCodeCurrentDate]
vChar.Text = AutoDate
ElseIf feildtype = 1 And fieldCodeID = 4 Then
'visCodeEditDate [last edit date]
vChar.Text = AutoDate
ElseIf feildtype = 1 And fieldCodeID = 6 Then
'visFCodePrintDate [print date]
vChar.Text = AutoDate
ElseIf feildtype = 1 And fieldCodeID = 3 Then
'visFCodeCurrentTime
vChar.Text = AutoDate
ElseIf feildtype = 1 And fieldCodeID = 5 Then
'visFCodeEditTime
vChar.Text = AutoDate
ElseIf feildtype = 1 And fieldCodeID = 7 Then
'visFCodePrintTime
vChar.Text = AutoDate
ElseIf feildtype = 2 And fieldCodeID = 2 Then
'visFCatDocument,directory
vChar.Text = AutoPath
ElseIf feildtype = 2 And fieldCodeID = 3 Then 'Field with
only fileName in it.
vChar.Text = AutoPath
'ElseIf feildtype = 2 And fieldCodeID = 0 Then
'Blanking
out creator field.
' vChar.Text = "AUTOCREATOR"
End If
ElseIf t = 0 Then
Application.DoEvents()
intret = vShape.CellsSRCExists(num, 0, 0, 0) 'search for a
cell by section, row, and column index,
'use the CellsSRCExists property.[looking for TextFeild
section,row 0,column 0, and field visFieldCell (0)]
'if intret = -1, then shape has some inserted fields, if 0,
there are no inserted fields.
If intret = -1 Then
'getting section object TextField section.
sec = vShape.Section(num) 'visSectionTextField 8:
Stores
an object's text fields.
'counting the rows in the textfield section.
sss = sec.Count
'looping through the shape's text block to replace
current date/time, directory and or filename.
For r = 0 To sss - 1
Application.DoEvents()
'check cell to see if field is current date/time,
directory or filename.
cell = vShape.CellsSRC(visSectionTextField, r,
visFieldCell)
Dim ss As String = cell.Formula
If ss = "NOW()" Then 'current date.
vShape.CellsSRC(visSectionTextField, r,
visFieldFormat).FormulaU = """=""""AUTODATE"""""""
ElseIf ss = "DOCLASTEDIT()" Then 'last edit date.
vShape.CellsSRC(visSectionTextField, r,
visFieldFormat).FormulaU = """=""""AUTODATE"""""""
ElseIf ss = "DOCLASTPRINT()" Then 'print date.
vShape.CellsSRC(visSectionTextField, r,
visFieldFormat).FormulaU = """=""""AUTODATE"""""""
ElseIf ss = "DOCLASTSAVE()" Then 'last save date.
vShape.CellsSRC(visSectionTextField, r,
visFieldFormat).FormulaU = """=""""AUTODATE"""""""
ElseIf ss = "DIRECTORY()" Then 'directory field.
vShape.CellsSRC(visSectionTextField, r,
visFieldFormat).FormulaU = """=""""AUTOPATH"""""""
ElseIf ss = "FILENAME()" Then 'file name field.
vShape.CellsSRC(visSectionTextField, r,
visFieldFormat).FormulaU = """=""""AUTOPATH"""""""
End If
Next r
End If 'end If intret = -1 Then
End If
Catch ex As System.Exception
Debug.Assert(False, ex.Message, ex.StackTrace)
Finally
vChar = Nothing
cell = Nothing
sec = Nothing
End Try
End Sub
This sub handles the text block with inserted fields along with additional
texts which i had trouble with. You had mentioned this method.
I get the TextField section of the shape, and since each row of the text
field section represents an inserted field, i check the formula to see if
it
meets any of my criteria checks of current fields and replace them
accordingly by resetting the formula in the shape sheet.
I haven't tried the response u posted about the characters method, but i
will.
Thank you very much John, I really appreciate your help, my questions
concerns have been answered for that case.
Since I am here now, i had another trouble when opening some org charts,
it
gave a prompt that says the shapes of the chart need to be converted to
the
current version of Visio before the document opened. These docs are
ignored,
thus slowing my processing as they have to be manually modified. Also,
prompts keep on appearing for some functional charts.
Is there a way I can get around these programatically.
Thank you very much.
John Goldsmith (Visio MVP) said:
Hello Biggaford,
Well I hope I'm understanding the problem correctly....you just want to
replace the automatic date fields with a straight text?
Basically an inserted field takes up a single character irrespective of
its
resulting text length, so you just need to loop through each character
and
check (as you've been doing) whether it is a field.
I've adapted your code a little to find the field, based on your
criteria.
The field is deleted and then replaced with the current date.
Note that I'm using VBA here but the VB won't be very different.
Private Sub Fields(ByVal vShape As Visio.Shape)
Dim vChar As Visio.Characters
Set vChar = vShape.Characters 'Characters object of shape object.
Dim chrIndex As Integer
For chrIndex = 1 To Len(vShape.Text)
vChar.Begin = chrIndex - 1
vChar.End = chrIndex
If vChar.IsField = -1 Then
If vChar.FieldCategory = Visio.visFCatDateTime Then
Select Case vChar.FieldCode
Case Visio.visFCodeCurrentDate, _
Visio.visFCodeEditDate, _
Visio.visFCodePrintDate, _
Visio.visFCodeCurrentTime, _
Visio.visFCodeEditTime, _
Visio.visFCodePrintTime
vChar.Delete
vChar.End = vChar.Begin
vChar.Text = Format(Now(), "dd/mm/yyyy")
'vChar.Text = "AUTODATE"
End Select
ElseIf vChar.FieldCategory = Visio.visFCatDocument Then
Select Case vChar.FieldCode
Case Visio.visFCodeDirectory, _
Visio.visFCodeFileName
'Add your path code
'vChar.Text = "AUTOPATH"
End Select
End If
End If
Next chrIndex
End Sub
Let me know if that helps or if I've gone off on the wrong track.
Best regards
John
John Goldsmith (Visio MVP)
www.visualSignals.typepad.co.uk
www.visualSignals.co.uk
Biggaford said:
Thank you John. I have written some code as shown bellow:
Private Sub Fields(ByVal vShape As Visio.Shape)
Dim vChar As Visio.Characters
Dim t As Short
Dim feildtype As Short
Dim fieldCodeID As Short
vChar = vShape.Characters 'Characters object of shape
object.
Dim chr As String = vChar.Text
'isField if 0 contains characters in addition to the expanded
text
of a field.
'isField if -1 represents only the expanded text of a field.
t = vChar.IsField
If t = -1 Then 'contains expanded text only of a field.
feildtype = vChar.FieldCategory
fieldCodeID = vChar.FieldCode
'Testing for field type(date\time) and specified field type
(current date field).
If feildtype = 1 And fieldCodeID = 2 Then 'visFCatDateTime
[visFCodeCurrentDate]
vChar.Text = "AUTODATE"
ElseIf feildtype = 1 And fieldCodeID = 4 Then
'visCodeEditDate
[last edit date]
vChar.Text = "AUTODATE"
ElseIf feildtype = 1 And fieldCodeID = 6 Then
'visFCodePrintDate
vChar.Text = "AUTODATE"
ElseIf feildtype = 1 And fieldCodeID = 3 Then
'visFCodeCurrentTime
vChar.Text = "AUTODATE"
ElseIf feildtype = 1 And fieldCodeID = 5 Then
'visFCodeEditTime
vChar.Text = "AUTODATE"
ElseIf feildtype = 1 And fieldCodeID = 7 Then
'visFCodePrintTime
vChar.Text = "AUTODATE"
ElseIf feildtype = 2 And fieldCodeID = 2 Then
'visFCatDocument,directory
vChar.Text = "AUTOPATH"
ElseIf feildtype = 2 And fieldCodeID = 3 Then 'Field with
only
fileName in it.
vChar.Text = "AUTOPATH"
'ElseIf feildtype = 2 And fieldCodeID = 0 Then 'Blanking
out
creator field.
' vChar.Text = "AUTOCREATOR"
End If
ElseIf t = 0 Then 'My problem here, text with field in one Text
Field, how do i search the string for the inserted fields.
vChar.text = RemoveNonFieldCurrentDates(chr )
End Sub
Private Function RemoveNonFieldCurrentDates(ByVal charsctersStr As
String)
As String
Dim tempSTR As String
Dim boo As Boolean
'Possible visio date formates.
Dim a As String = Date.Today '10/28/2008
Dim aindex As Integer = InStr(charsctersStr, a)
If aindex <> 0 Then
tempSTR = charsctersStr.ToString.Replace(a, "AUTODATE")
boo = True
End If
'if non of the above is in string.
If Not boo Then
tempSTR = charsctersStr
End If
Return tempSTR
End Function
what I am doing is to replace all occurences of current date/time with
the
string "Autodate".
I get the characters object of the shape object passed in the sub, then
i
use the isField property to determine if it is a field. BUT, my
struggle
arises when it is not just a field, but the text field has text and a
date/time field in it. I prefer to search the characters string for
inserted
fields and replace them accordingly; but how do i know when I reach a
field.
I tried changing the NOW() formula in the shape sheet of the Text
Fields
section, but its not allowing me to and its not guarded.
ANd definately, im happy using code, i just havent figured out the
right
thing to use as yet.
:
Hello Biggaford,
The text of the shape will only return a mix of the inserted field and
so
you would be able to check for the the 'current date/time' as opposed
to
just an abitary date using Edit / Find.
If you know the format that was used for the inserted fields you might
be
able to search using a pattern such as:
^?^?/^?^?/^?^?^?^?
....which will return a match for a date.
Otherwise you could seach through all of the shapes with code fairly
easily
searching for an inserted field (Text Field) that uses the Now()
function.
Let me know if you're happy using code and I'll help out if you need
it.
(You might find this useful if you've not tried any code before
http://visualsignals.typepad.co.uk/vislog/2007/10/just-for-starte.html
).
Hope that helps.
Best regards
John
John Goldsmith (Visio MVP)
www.visualSignals.typepad.co.uk
www.visualSignals.co.uk
How can i search a shape's text for occurrences of current date and
time
fields. Is there a way I can just search the text for a time field
and
a
date field of any format?
Thanks in advance.