D
Dennis
Using Visio 2003
I have pieced together some VBA code from prior postings in Visio.general
newsgroup. Apparantly, I should have questioned the VBA in this newsgroup.
When it comes to VBA especially related to Visio, I am very dangerous and
fankly incompetent.
The VBA code below was developed by Al Edlund and he was kind enough to
share it in 2005 and 2003 with many who would like to have a Copy / Past
Utility approximating what is available in Excel (copy tab or "sheets").
When Al shared the code he was clear that the code was excerpted. That
said, I would like to be able to use it if possible.
In his code shared in May 2005, two functions were absent,
funcReplaceStr() and
funcGetTokens.
I was advised to change funcReplaceStr(), with apparently, a generic
Replace() function.
Wen I did that the following line it fails with a ByRef type mismatch error:
tgtPage.Name = Left(funcGetTokens (strDocName, "Guideline", 1), 23) &
curPageIndx & "Exmpl"
That line is fed with the code above:
strDocName = funcReplaceStr(srcDoc.Name, " ", "", 1)
-which was-
strDocName = Replace(srcDoc.Name, " ", "", 1)
My guess is that Replace() may not provide what funcGetTokens wants to see.
Can anyone help me get this code to function?
Dennis
*****************************************************
' Al Edlund copy/paste process as posted 5/23/2005
Sub myCopyPaste() 'This line was added by me
' Process the pages
strCurStep = "start process other files"
Set pagsObj = srcDoc.Pages
' iterate through the collection
For curPageIndx = 1 To pagsObj.Count
' retrieve the page object at the current index
Set srcPage = pagsObj.Item(curPageIndx)
' Check whether the current page is a background page
' Display the name of all the foreground pages
If srcPage.Background = False Then
strCurStep = "work on foreground pages"
' take spaces out of the document name
strDocName = Replace(srcDoc.Name, " ", "", 1)
' create the new page
Set tgtPage = tgtDoc.Pages.Add
' the target page name is the source document and page number
tgtPage.Name = Left(funcGetTokens(strDocName, "Guideline", 1), 23) &
curPageIndx & "Exmpl"
tgtPage.BackPage = "Background General"
tgtPage.Background = False
blnResult = funcCopyPageFormat(tgtPage, srcPage)
If blnResult = False Then Debug.Print "Result Copy Page format";
failed; ""
strCurStep = "copy the page"
Visio.Application.ScreenUpdatiÂng = False
blnResult = funcCopyPage(tgtPage, srcPage)
If blnResult = False Then Debug.Print "Result Copy Page failed"
Visio.Application.ScreenUpdatiÂng = True
tgtWin.Activate
ActivePage.Name = tgtPage.Name
ActivePage.CenterDrawing
Else
' Process the background page or delete this section to ignore the
background pages
End If
Next curPageIndx
End Sub 'This line was added by me
Private Function funcCopyPage(tgtPage As Visio.Page, srcPage As Visio.Page)
As Boolean
Dim iObjCnt As Integer
Dim iConCtr As Integer
Dim iWinCtr As Integer
Dim strPageName As String
Dim curWin As Visio.Window
Dim strCurStep As String
On Error GoTo CopyPage_Err
' go to the source window
strCurStep = "activate source win"
srcWin.Activate
ActivePage.Name = srcPage.Name
' brute force method of copy
strCurStep = "Copy source"
ActiveWindow.SelectAll
ActiveWindow.Group
ActiveWindow.Copy
' now go and paste it
strCurStep = "activate target win"
tgtWin.Activate
strCurStep = "set target apctive page"
ActivePage.Name = tgtPage.Name
ActivePage.Paste
funcCopyPage = True
CopyPage_Exit:
DoEvents
Exit Function
CopyPage_Err:
Debug.Print "Error CopyPage Cur Step = "; strCurStep
Debug.Print "Error CopyPage " & Err.Number & ": " & Err.Description
funcCopyPage = False
Resume CopyPage_Exit
End Function
' We want to copy page formats to target pages for a number of reasons
' which include common looks as well as maintaining integrity of copied
' pages.
Private Function funcCopyPageFormat(tgtPage As Visio.Page, srcPage As
Visio.Page) As Boolean
Dim tgtPageSheet As Visio.Shape
Dim srcPageSheet As Visio.Shape
On Error GoTo CopyPageFormat_Err
Set tgtPageSheet = tgtPage.PageSheet
Set srcPageSheet = srcPage.PageSheet
'Debug.Print "change size type"
tgtPageSheet.Cells("DrawingSizÂeType").FormulaU =
srcPageSheet.Cells("DrawingSizÂeType").FormulaU
'Debug.Print "change scale type"
tgtPageSheet.Cells("DrawingScaÂleType").FormulaU =
srcPageSheet.Cells("DrawingScaÂleType").FormulaU
'Debug.Print " drawing scale"
tgtPageSheet.Cells("DrawingScaÂle").FormulaU =
srcPageSheet.Cells("DrawingScaÂle").FormulaU
'Debug.Print " page scale "
tgtPageSheet.Cells("PageScaleÂ").FormulaU =
srcPageSheet.Cells("PageScaleÂ").FormulaU
'Debug.Print "width"
tgtPageSheet.Cells("PageWidthÂ").FormulaU =
srcPageSheet.Cells("PageWidthÂ").FormulaU
'Debug.Print "height"
tgtPageSheet.Cells("PageHeightÂ").FormulaU =
srcPageSheet.Cells("PageHeightÂ").FormulaU
'Debug.Print "route"
tgtPageSheet.Cells("RouteStyleÂ").FormulaU =
srcPageSheet.Cells("RouteStyleÂ").FormulaU
funcCopyPageFormat = True
CopyPageFormat_Exit:
DoEvents
Exit Function
CopyPageFormat_Err:
Debug.Print "Error CopyPageFormat " & Err.Number & ": " &
Err.Description
funcCopyPageFormat = False
Resume CopyPageFormat_Exit
End Function
Public Function funcGetTokens(strTemp As String, strDelimiter As String,
lngIndex As Long) As String
' funcGetTokens is a general parser that the user supplies
' an input string, a delimiter (1 or more characters) and an index
' into the input string to get a subset of the string
' if csv we watch for parens (i.e. embedded lists)
Dim lngTokenCount As Long
Dim lngCount As Long
Dim lngTempPos As Long
Dim lngSPos As Long
Dim lngEPos As Long
Dim intLenDel As Integer
Dim boolEmbeddedList As Boolean
Dim intListStart As Integer
Dim intListStop As Integer
Dim boolCSV As Boolean
' is this a csv parse ?
If strDelimiter = "," Then
boolCSV = True
Else
boolCSV = False
End If
intListStart = InStr(1, strTemp, "(", vbBinaryCompare)
intListStop = InStr(1, strTemp, ")", vbBinaryCompare)
If intListStop > intListStart Then
boolEmbeddedList = True
Else
boolEmbeddedList = False
End If
lngTokenCount = funcCountTokens(strTemp, strDelimiter)
If lngIndex < 1 Or lngIndex > lngTokenCount Then
funcGetTokens = 0
Exit Function
End If
intLenDel = Len(strDelimiter)
lngCount = 1
lngSPos = 1
If lngIndex = 1 Then
If boolCSV = True And boolEmbeddedList = True Then
' recheck where the list is from this spot
intListStart = InStr(1, strTemp, "(", vbBinaryCompare)
intListStop = InStr(1, strTemp, ")", vbBinaryCompare)
lngEPos = InStr(1, strTemp, strDelimiter) - intLenDel
' don't point into a list
If lngEPos < intListStart Then
funcGetTokens = Mid(strTemp, 1, lngEPos - 1 + intLenDel)
Exit Function
End If
Else
lngEPos = InStr(1, strTemp, strDelimiter) - intLenDel
funcGetTokens = Mid(strTemp, 1, lngEPos - 1 + intLenDel)
Exit Function
End If
End If
For lngCount = 2 To lngIndex
If boolCSV = True And boolEmbeddedList = True Then
' recheck where the list is from this spot
intListStart = InStr(lngSPos, strTemp, "(", vbBinaryCompare)
intListStop = InStr(lngSPos, strTemp, ")", vbBinaryCompare)
lngTempPos = InStr(lngSPos, strTemp, strDelimiter, 0) -
intLenDel
' don't point into a list
If lngTempPos < intListStart Or lngTempPos > intListStop Then
lngSPos = lngTempPos
lngCount = lngCount + 1
End If
Else
lngSPos = InStr(lngSPos, strTemp, strDelimiter, 0) + intLenDel
DoEvents
End If
Next lngCount
DoEvents
lngEPos = InStr(lngSPos, strTemp, strDelimiter, 0) - intLenDel
If lngEPos <= 0 Then
lngEPos = Len(strTemp)
End If
funcGetTokens = Mid(strTemp, lngSPos, lngEPos - lngSPos + intLenDel)
End Function
Public Function funcCountTokens(strTemp As String, strDelimiter As
String) As Long
'
' Counts strings separated by input string delimter
' if the delimiter is a comma then check for embedded lists
'
Dim lngTokenCount As Long
Dim lngIndex As Long
Dim lngTempIndex As Long
Dim lngDelLen As Long
Dim lngInput As Long
Dim boolEmbeddedList As Boolean
Dim intListStart As Integer
Dim intListStop As Integer
Dim boolCSV As Boolean
' is this a csv parse ?
If strDelimiter = "," Then
boolCSV = True
Else
boolCSV = False
End If
intListStart = InStr(1, strTemp, "(", vbBinaryCompare)
intListStop = InStr(1, strTemp, ")", vbBinaryCompare)
If intListStop > intListStart Then
boolEmbeddedList = True
Else
boolEmbeddedList = False
End If
lngInput = Len(strTemp)
If VarType(strTemp) <> vbString Or Len(strTemp) = 0 Then
funcCountTokens = 0
Exit Function
End If
lngDelLen = Len(strDelimiter)
' first check to see if there is a delimter in the string
lngIndex = InStr(strTemp, strDelimiter)
lngTokenCount = 1
Do While ((lngIndex > 0) And (lngIndex <= lngInput))
' since we got here we must have found a token
If boolCSV = True And boolEmbeddedList = True Then
' recheck where the list is from this spot
intListStart = InStr(1, strTemp, "(", vbBinaryCompare)
intListStop = InStr(1, strTemp, ")", vbBinaryCompare)
lngTempIndex = InStr(lngIndex + lngDelLen, strTemp,
strDelimiter, 0)
If lngTempIndex > intListStart Or lngTempIndex < intListStop
Then
lngTokenCount = lngTokenCount + 1
lngIndex = lngTempIndex
Else
lngIndex = lngIndex + 1
End If
Else
lngTokenCount = lngTokenCount + 1
lngIndex = InStr(lngIndex + lngDelLen, strTemp,
strDelimiter, 0)
End If
DoEvents
Loop
' suspect I need some logic for what happens if no delimter found
funcCountTokens = lngTokenCount
End Function
I have pieced together some VBA code from prior postings in Visio.general
newsgroup. Apparantly, I should have questioned the VBA in this newsgroup.
When it comes to VBA especially related to Visio, I am very dangerous and
fankly incompetent.
The VBA code below was developed by Al Edlund and he was kind enough to
share it in 2005 and 2003 with many who would like to have a Copy / Past
Utility approximating what is available in Excel (copy tab or "sheets").
When Al shared the code he was clear that the code was excerpted. That
said, I would like to be able to use it if possible.
In his code shared in May 2005, two functions were absent,
funcReplaceStr() and
funcGetTokens.
I was advised to change funcReplaceStr(), with apparently, a generic
Replace() function.
Wen I did that the following line it fails with a ByRef type mismatch error:
tgtPage.Name = Left(funcGetTokens (strDocName, "Guideline", 1), 23) &
curPageIndx & "Exmpl"
That line is fed with the code above:
strDocName = funcReplaceStr(srcDoc.Name, " ", "", 1)
-which was-
strDocName = Replace(srcDoc.Name, " ", "", 1)
My guess is that Replace() may not provide what funcGetTokens wants to see.
Can anyone help me get this code to function?
Dennis
*****************************************************
' Al Edlund copy/paste process as posted 5/23/2005
Sub myCopyPaste() 'This line was added by me
' Process the pages
strCurStep = "start process other files"
Set pagsObj = srcDoc.Pages
' iterate through the collection
For curPageIndx = 1 To pagsObj.Count
' retrieve the page object at the current index
Set srcPage = pagsObj.Item(curPageIndx)
' Check whether the current page is a background page
' Display the name of all the foreground pages
If srcPage.Background = False Then
strCurStep = "work on foreground pages"
' take spaces out of the document name
strDocName = Replace(srcDoc.Name, " ", "", 1)
' create the new page
Set tgtPage = tgtDoc.Pages.Add
' the target page name is the source document and page number
tgtPage.Name = Left(funcGetTokens(strDocName, "Guideline", 1), 23) &
curPageIndx & "Exmpl"
tgtPage.BackPage = "Background General"
tgtPage.Background = False
blnResult = funcCopyPageFormat(tgtPage, srcPage)
If blnResult = False Then Debug.Print "Result Copy Page format";
failed; ""
strCurStep = "copy the page"
Visio.Application.ScreenUpdatiÂng = False
blnResult = funcCopyPage(tgtPage, srcPage)
If blnResult = False Then Debug.Print "Result Copy Page failed"
Visio.Application.ScreenUpdatiÂng = True
tgtWin.Activate
ActivePage.Name = tgtPage.Name
ActivePage.CenterDrawing
Else
' Process the background page or delete this section to ignore the
background pages
End If
Next curPageIndx
End Sub 'This line was added by me
Private Function funcCopyPage(tgtPage As Visio.Page, srcPage As Visio.Page)
As Boolean
Dim iObjCnt As Integer
Dim iConCtr As Integer
Dim iWinCtr As Integer
Dim strPageName As String
Dim curWin As Visio.Window
Dim strCurStep As String
On Error GoTo CopyPage_Err
' go to the source window
strCurStep = "activate source win"
srcWin.Activate
ActivePage.Name = srcPage.Name
' brute force method of copy
strCurStep = "Copy source"
ActiveWindow.SelectAll
ActiveWindow.Group
ActiveWindow.Copy
' now go and paste it
strCurStep = "activate target win"
tgtWin.Activate
strCurStep = "set target apctive page"
ActivePage.Name = tgtPage.Name
ActivePage.Paste
funcCopyPage = True
CopyPage_Exit:
DoEvents
Exit Function
CopyPage_Err:
Debug.Print "Error CopyPage Cur Step = "; strCurStep
Debug.Print "Error CopyPage " & Err.Number & ": " & Err.Description
funcCopyPage = False
Resume CopyPage_Exit
End Function
' We want to copy page formats to target pages for a number of reasons
' which include common looks as well as maintaining integrity of copied
' pages.
Private Function funcCopyPageFormat(tgtPage As Visio.Page, srcPage As
Visio.Page) As Boolean
Dim tgtPageSheet As Visio.Shape
Dim srcPageSheet As Visio.Shape
On Error GoTo CopyPageFormat_Err
Set tgtPageSheet = tgtPage.PageSheet
Set srcPageSheet = srcPage.PageSheet
'Debug.Print "change size type"
tgtPageSheet.Cells("DrawingSizÂeType").FormulaU =
srcPageSheet.Cells("DrawingSizÂeType").FormulaU
'Debug.Print "change scale type"
tgtPageSheet.Cells("DrawingScaÂleType").FormulaU =
srcPageSheet.Cells("DrawingScaÂleType").FormulaU
'Debug.Print " drawing scale"
tgtPageSheet.Cells("DrawingScaÂle").FormulaU =
srcPageSheet.Cells("DrawingScaÂle").FormulaU
'Debug.Print " page scale "
tgtPageSheet.Cells("PageScaleÂ").FormulaU =
srcPageSheet.Cells("PageScaleÂ").FormulaU
'Debug.Print "width"
tgtPageSheet.Cells("PageWidthÂ").FormulaU =
srcPageSheet.Cells("PageWidthÂ").FormulaU
'Debug.Print "height"
tgtPageSheet.Cells("PageHeightÂ").FormulaU =
srcPageSheet.Cells("PageHeightÂ").FormulaU
'Debug.Print "route"
tgtPageSheet.Cells("RouteStyleÂ").FormulaU =
srcPageSheet.Cells("RouteStyleÂ").FormulaU
funcCopyPageFormat = True
CopyPageFormat_Exit:
DoEvents
Exit Function
CopyPageFormat_Err:
Debug.Print "Error CopyPageFormat " & Err.Number & ": " &
Err.Description
funcCopyPageFormat = False
Resume CopyPageFormat_Exit
End Function
Public Function funcGetTokens(strTemp As String, strDelimiter As String,
lngIndex As Long) As String
' funcGetTokens is a general parser that the user supplies
' an input string, a delimiter (1 or more characters) and an index
' into the input string to get a subset of the string
' if csv we watch for parens (i.e. embedded lists)
Dim lngTokenCount As Long
Dim lngCount As Long
Dim lngTempPos As Long
Dim lngSPos As Long
Dim lngEPos As Long
Dim intLenDel As Integer
Dim boolEmbeddedList As Boolean
Dim intListStart As Integer
Dim intListStop As Integer
Dim boolCSV As Boolean
' is this a csv parse ?
If strDelimiter = "," Then
boolCSV = True
Else
boolCSV = False
End If
intListStart = InStr(1, strTemp, "(", vbBinaryCompare)
intListStop = InStr(1, strTemp, ")", vbBinaryCompare)
If intListStop > intListStart Then
boolEmbeddedList = True
Else
boolEmbeddedList = False
End If
lngTokenCount = funcCountTokens(strTemp, strDelimiter)
If lngIndex < 1 Or lngIndex > lngTokenCount Then
funcGetTokens = 0
Exit Function
End If
intLenDel = Len(strDelimiter)
lngCount = 1
lngSPos = 1
If lngIndex = 1 Then
If boolCSV = True And boolEmbeddedList = True Then
' recheck where the list is from this spot
intListStart = InStr(1, strTemp, "(", vbBinaryCompare)
intListStop = InStr(1, strTemp, ")", vbBinaryCompare)
lngEPos = InStr(1, strTemp, strDelimiter) - intLenDel
' don't point into a list
If lngEPos < intListStart Then
funcGetTokens = Mid(strTemp, 1, lngEPos - 1 + intLenDel)
Exit Function
End If
Else
lngEPos = InStr(1, strTemp, strDelimiter) - intLenDel
funcGetTokens = Mid(strTemp, 1, lngEPos - 1 + intLenDel)
Exit Function
End If
End If
For lngCount = 2 To lngIndex
If boolCSV = True And boolEmbeddedList = True Then
' recheck where the list is from this spot
intListStart = InStr(lngSPos, strTemp, "(", vbBinaryCompare)
intListStop = InStr(lngSPos, strTemp, ")", vbBinaryCompare)
lngTempPos = InStr(lngSPos, strTemp, strDelimiter, 0) -
intLenDel
' don't point into a list
If lngTempPos < intListStart Or lngTempPos > intListStop Then
lngSPos = lngTempPos
lngCount = lngCount + 1
End If
Else
lngSPos = InStr(lngSPos, strTemp, strDelimiter, 0) + intLenDel
DoEvents
End If
Next lngCount
DoEvents
lngEPos = InStr(lngSPos, strTemp, strDelimiter, 0) - intLenDel
If lngEPos <= 0 Then
lngEPos = Len(strTemp)
End If
funcGetTokens = Mid(strTemp, lngSPos, lngEPos - lngSPos + intLenDel)
End Function
Public Function funcCountTokens(strTemp As String, strDelimiter As
String) As Long
'
' Counts strings separated by input string delimter
' if the delimiter is a comma then check for embedded lists
'
Dim lngTokenCount As Long
Dim lngIndex As Long
Dim lngTempIndex As Long
Dim lngDelLen As Long
Dim lngInput As Long
Dim boolEmbeddedList As Boolean
Dim intListStart As Integer
Dim intListStop As Integer
Dim boolCSV As Boolean
' is this a csv parse ?
If strDelimiter = "," Then
boolCSV = True
Else
boolCSV = False
End If
intListStart = InStr(1, strTemp, "(", vbBinaryCompare)
intListStop = InStr(1, strTemp, ")", vbBinaryCompare)
If intListStop > intListStart Then
boolEmbeddedList = True
Else
boolEmbeddedList = False
End If
lngInput = Len(strTemp)
If VarType(strTemp) <> vbString Or Len(strTemp) = 0 Then
funcCountTokens = 0
Exit Function
End If
lngDelLen = Len(strDelimiter)
' first check to see if there is a delimter in the string
lngIndex = InStr(strTemp, strDelimiter)
lngTokenCount = 1
Do While ((lngIndex > 0) And (lngIndex <= lngInput))
' since we got here we must have found a token
If boolCSV = True And boolEmbeddedList = True Then
' recheck where the list is from this spot
intListStart = InStr(1, strTemp, "(", vbBinaryCompare)
intListStop = InStr(1, strTemp, ")", vbBinaryCompare)
lngTempIndex = InStr(lngIndex + lngDelLen, strTemp,
strDelimiter, 0)
If lngTempIndex > intListStart Or lngTempIndex < intListStop
Then
lngTokenCount = lngTokenCount + 1
lngIndex = lngTempIndex
Else
lngIndex = lngIndex + 1
End If
Else
lngTokenCount = lngTokenCount + 1
lngIndex = InStr(lngIndex + lngDelLen, strTemp,
strDelimiter, 0)
End If
DoEvents
Loop
' suspect I need some logic for what happens if no delimter found
funcCountTokens = lngTokenCount
End Function