Setting Options Programmatically in Visio 2010 for Publishing to SharePoint Server 2010
You can use the Visual Basic for Applications (VBA) object model exposed by Microsoft Visio 2010 to save Visio 2010 diagrams as web drawings (.vdw files). To do so, use the SaveAs method of the Document object, assigning the file the .vdw extension.
Note:
This capability is available only in Microsoft Visio Professional 2010 and Microsoft Visio Premium 2010.
In addition, you can set options for programmatically publishing these files to Visio Services in Microsoft SharePoint Server 2010 with Enterprise Client Access License. This article explains how to use the methods and properties of the new ServerPublishOptions object to set publishing options for the active Visio document. To get a ServerPublishOptions object for any document, including documents other than the active document, you can use the ServerPublishOptions property of the Document object.
Note:
You can also set options for publishing to Visio Services in the Visio 2010 user interface by clicking the File tab, clicking Save As, clicking Web Drawing (*.vdw) in the Save as type list, and then clicking Options.
Creating a Visio File to Use with the Code Examples
For the sake of simplicity, the code examples in this article are designed to be used with a Visio 2010 file that contains four pages, each containing a single geometric shape, each of which is linked to data in a row in a Microsoft Excel spreadsheet. You can, of course, modify the examples, as necessary, to work with your own files and data sources.
To create a Visio 2010 file that works with these examples without further modification, and to create an Excel workbook to use as the data source for the file, use the general procedures that follow. (Details about how to perform each step are beyond the scope of this article—see end-user Help and the links at the end of this article for specifics about any particular step.)
To create a Visio file to use with the code examples
1. Create a new Visio 2010 file from the Basic Diagram template
2. Add three pages to the file.
3. Add a single shape to each page: an octagon to one, a circle to another, a star to a third, and a pentagon to a fourth. The page order is unimportant, as is the location of each shape on the page.
4. Name each page to correspond to the shape it contains.
5. Use the Data Selector wizard to link the drawing to data in an Excel spreadsheet. The next procedure explains how to create this spreadsheet.
Note:
To start the Data Selector wizard, on the Data tab, click Link Data to Shapes.
6. Link each shape to the corresponding data row in the External Data window.
7. Add the code examples in this article to the ThisDocument project in the Visual Basic Editor window in the file.
8. Optionally, edit the data graphic applied to the shapes to add a new Color by Value data field, associating the appropriate fill color with each Color value.
9. Save the file to the location of your choice, naming it however you like.
To create an Excel workbook as a data source for the Visio 2010 file
1. Create a new Excel file.
2. Add the following text to the first five rows of the first two columns of Sheet1.
Shape Color
Circle Red
Star Blue
Octagon Green
Pentagon Yellow
3. Save the file, naming it however you like, to a document library on the SharePoint Server 2010 site where you want to display your Visio drawing.
Note:
You can connect your Visio drawing to an Excel workbook saved in any location. However, if you want the Visio 2010 diagram in Visio Services to reflect any changes to the data in the Excel workbook when the diagram is refreshed, save the Excel workbook to a SharePoint document library, link your diagram to the Excel workbook in the SharePoint library, and then save your diagram to the same SharePoint library.
Saving a Visio 2010 Diagram as a Web Drawing (.vdw File) Programmatically
The following code shows how to save the active Visio 2010 diagram as a web drawing (.vdw file) for use with Visio Services. Modify the name of the file and the location at which you want to save the file.
Public Sub SaveAsVDW()
Visio.ActiveDocument.SaveAs ("C:\WebDrawingName.vdw")
End Sub
Setting Page-Publishing Options
If the document that you want to save as a web drawing (.vdw file) has more than a single page, you can specify exactly which pages you want to include in the publication. The SetPagesToPublish method allows you to publish either all pages in the document or selected pages that you specify by page name. The GetPagesToPublish method lets you determine which pages are currently set to be published. The IncludePage method includes a specific page in the set of pages to be published, and the ExcludePage method excludes a specific page. The IsPublishedPage property tells you if a specific page is already set to be published.
Specifying the Pages to Publish
You can specify a set of pages to be published. The following code example shows how to use the SetPagesToPublish method to specify exactly which pages to publish. The code creates an array of strings, each of which corresponds to the name of a page to publish, and then passes that array to the method. Other parameters passed to the method specify that only selected pages are to be published, and that the page names in the array are universal names.
Public Sub SetPagesToPublish_Example()
Dim aryNamesArray() As String
ReDim aryNamesArray(1 To 2)
aryNamesArray(1) = "Octagon"
aryNamesArray(2) = "Circle"
Visio.ActiveDocument.ServerPublishOptions.SetPagesToPublish visPublishPageSelect, aryNamesArray, visLangUniversal
End Sub
Determining Which Pages Are Going to Be Published
You can use the GetPagesToPublish method to determine which pages are set to be published. The following code shows how to use this method to get these pages. The method takes a constant value from the VisLangFlags enumeration that specifies whether the names of the pages in the array returned are local or universal. It returns two values in the form of the two out parameters that are passed to it:
• A constant value from the VisPublishPages enumeration that indicates whether all pages or only selected pages are to be published.
• An array of strings that is populated with the names of pages to be published.
Public Sub GetPagesToPublish_Example()
Dim aryNamesArray() As String
Dim cnstLangFlag As VisLangFlags
Dim cnstPagesToPublish As VisPublishPages
cnstLangFlag = visLangUniversal
Visio.ActiveDocument.ServerPublishOptions.GetPagesToPublish cnstLangFlag, cnstPagesToPublish, aryNamesArray
Dim intCounter As Integer
For intCounter = LBound(aryNamesArray) To UBound(aryNamesArray)
Debug.Print aryNamesArray(intCounter)
Next intCounter
If cnstPagesToPublish = visPublishPageAll Then
Debug.Print "Print all pages"
Else: Debug.Print "Print selected pages"
End If
End Sub
Including and Excluding Specific Pages
The following code shows how to use the IncludePage method to include a specific page in the set of pages to be published. It also shows how to use the IsPublishedPage property to determine whether the page you want to include is already included.
Public Sub IncludePage_Example()
If Not Visio.ActiveDocument.ServerPublishOptions.IsPublishedPage("Star", visLangUniversal) Then
Visio.ActiveDocument.ServerPublishOptions.IncludePage "Star", visLangUniversal
Else
Debug.Print "Star is already set to be published."
End If
End Sub
The following code shows how to use the ExcludePage method to exclude a specific page from the set of pages to be published.
Public Sub ExcludePage_Example()
Visio.ActiveDocument.ServerPublishOptions.ExcludePage "Circle", visLangUniversal
End Sub
Setting Data Recordset-Publishing Options
If the document that you want to save as a web drawing (.vdw file) to be used with Visio Services contains data in the form of data recordsets, you can specify exactly which data recordsets you want to be connected to the web drawing file, so that the drawing is refreshed in the browser as data changes. The SetRecordSetsToPublish method allows you to publish either all recordsets in the document, or selected data recordsets that you specify by ID. The GetRecordSetsToPublish method lets you determine which data recordsets are currently set to be published.
Specifying the Data Recordsets to Publish
The following code shows how to use the SetRecordSetsToPublish method to specify which data recordsets in the active document are published when the document is saved as a web drawing. The code passes an array of Long values that represent the IDs of the data recordsets to be included.
Public Sub SetRecordsetsToPublish_Example()
Dim aryDataRecordsetIDs() As Long
ReDim aryDataRecordsetIDs(1 To ActiveDocument.DataRecordsets.Count)
aryDataRecordsetIDs(1) = 1
Visio.ActiveDocument.ServerPublishOptions.SetRecordsetsToPublish visPublishDataRecordsetSelect, aryDataRecordsetIDs
End Sub
Determining Which Data Recordsets Are Going to Be Published
The following code shows how to use the GetRecordSetsToPublish method to determine which data recordsets in the active document are currently set to publish. The method returns two out parameters that indicate whether no data recordsets, all data recordsets, or selected data recordsets are to be published; and in any but the first case, which data recordsets are the ones to be published.
Public Sub GetRecordSetsToPublish_Example()
Dim aryDataRecordsetIDs() As Long
Dim cnstPublishFlag As VisPublishDataRecordsets
Dim vsoDataRecordsets As DataRecordsets
Visio.ActiveDocument.ServerPublishOptions.GetRecordsetsToPublish cnstPublishFlag, aryDataRecordsetIDs
If Not cnstPublishFlag = visPublishDataRecordsetNone Then
Dim intCounter As Integer
For intCounter = LBound(aryDataRecordsetIDs) To UBound(aryDataRecordsetIDs)
Debug.Print ActiveDocument.DataRecordsets.ItemFromID(aryDataRecordsetIDs(intCounter)).Name
Next intCounter
Else
Debug.Print "No data recordsets are set to be published."
End If
End Sub
Note:
This capability is available only in Microsoft Visio Professional 2010 and Microsoft Visio Premium 2010.
In addition, you can set options for programmatically publishing these files to Visio Services in Microsoft SharePoint Server 2010 with Enterprise Client Access License. This article explains how to use the methods and properties of the new ServerPublishOptions object to set publishing options for the active Visio document. To get a ServerPublishOptions object for any document, including documents other than the active document, you can use the ServerPublishOptions property of the Document object.
Note:
You can also set options for publishing to Visio Services in the Visio 2010 user interface by clicking the File tab, clicking Save As, clicking Web Drawing (*.vdw) in the Save as type list, and then clicking Options.
Creating a Visio File to Use with the Code Examples
For the sake of simplicity, the code examples in this article are designed to be used with a Visio 2010 file that contains four pages, each containing a single geometric shape, each of which is linked to data in a row in a Microsoft Excel spreadsheet. You can, of course, modify the examples, as necessary, to work with your own files and data sources.
To create a Visio 2010 file that works with these examples without further modification, and to create an Excel workbook to use as the data source for the file, use the general procedures that follow. (Details about how to perform each step are beyond the scope of this article—see end-user Help and the links at the end of this article for specifics about any particular step.)
To create a Visio file to use with the code examples
1. Create a new Visio 2010 file from the Basic Diagram template
2. Add three pages to the file.
3. Add a single shape to each page: an octagon to one, a circle to another, a star to a third, and a pentagon to a fourth. The page order is unimportant, as is the location of each shape on the page.
4. Name each page to correspond to the shape it contains.
5. Use the Data Selector wizard to link the drawing to data in an Excel spreadsheet. The next procedure explains how to create this spreadsheet.
Note:
To start the Data Selector wizard, on the Data tab, click Link Data to Shapes.
6. Link each shape to the corresponding data row in the External Data window.
7. Add the code examples in this article to the ThisDocument project in the Visual Basic Editor window in the file.
8. Optionally, edit the data graphic applied to the shapes to add a new Color by Value data field, associating the appropriate fill color with each Color value.
9. Save the file to the location of your choice, naming it however you like.
To create an Excel workbook as a data source for the Visio 2010 file
1. Create a new Excel file.
2. Add the following text to the first five rows of the first two columns of Sheet1.
Shape Color
Circle Red
Star Blue
Octagon Green
Pentagon Yellow
3. Save the file, naming it however you like, to a document library on the SharePoint Server 2010 site where you want to display your Visio drawing.
Note:
You can connect your Visio drawing to an Excel workbook saved in any location. However, if you want the Visio 2010 diagram in Visio Services to reflect any changes to the data in the Excel workbook when the diagram is refreshed, save the Excel workbook to a SharePoint document library, link your diagram to the Excel workbook in the SharePoint library, and then save your diagram to the same SharePoint library.
Saving a Visio 2010 Diagram as a Web Drawing (.vdw File) Programmatically
The following code shows how to save the active Visio 2010 diagram as a web drawing (.vdw file) for use with Visio Services. Modify the name of the file and the location at which you want to save the file.
Public Sub SaveAsVDW()
Visio.ActiveDocument.SaveAs ("C:\WebDrawingName.vdw")
End Sub
Setting Page-Publishing Options
If the document that you want to save as a web drawing (.vdw file) has more than a single page, you can specify exactly which pages you want to include in the publication. The SetPagesToPublish method allows you to publish either all pages in the document or selected pages that you specify by page name. The GetPagesToPublish method lets you determine which pages are currently set to be published. The IncludePage method includes a specific page in the set of pages to be published, and the ExcludePage method excludes a specific page. The IsPublishedPage property tells you if a specific page is already set to be published.
Specifying the Pages to Publish
You can specify a set of pages to be published. The following code example shows how to use the SetPagesToPublish method to specify exactly which pages to publish. The code creates an array of strings, each of which corresponds to the name of a page to publish, and then passes that array to the method. Other parameters passed to the method specify that only selected pages are to be published, and that the page names in the array are universal names.
Public Sub SetPagesToPublish_Example()
Dim aryNamesArray() As String
ReDim aryNamesArray(1 To 2)
aryNamesArray(1) = "Octagon"
aryNamesArray(2) = "Circle"
Visio.ActiveDocument.ServerPublishOptions.SetPagesToPublish visPublishPageSelect, aryNamesArray, visLangUniversal
End Sub
Determining Which Pages Are Going to Be Published
You can use the GetPagesToPublish method to determine which pages are set to be published. The following code shows how to use this method to get these pages. The method takes a constant value from the VisLangFlags enumeration that specifies whether the names of the pages in the array returned are local or universal. It returns two values in the form of the two out parameters that are passed to it:
• A constant value from the VisPublishPages enumeration that indicates whether all pages or only selected pages are to be published.
• An array of strings that is populated with the names of pages to be published.
Public Sub GetPagesToPublish_Example()
Dim aryNamesArray() As String
Dim cnstLangFlag As VisLangFlags
Dim cnstPagesToPublish As VisPublishPages
cnstLangFlag = visLangUniversal
Visio.ActiveDocument.ServerPublishOptions.GetPagesToPublish cnstLangFlag, cnstPagesToPublish, aryNamesArray
Dim intCounter As Integer
For intCounter = LBound(aryNamesArray) To UBound(aryNamesArray)
Debug.Print aryNamesArray(intCounter)
Next intCounter
If cnstPagesToPublish = visPublishPageAll Then
Debug.Print "Print all pages"
Else: Debug.Print "Print selected pages"
End If
End Sub
Including and Excluding Specific Pages
The following code shows how to use the IncludePage method to include a specific page in the set of pages to be published. It also shows how to use the IsPublishedPage property to determine whether the page you want to include is already included.
Public Sub IncludePage_Example()
If Not Visio.ActiveDocument.ServerPublishOptions.IsPublishedPage("Star", visLangUniversal) Then
Visio.ActiveDocument.ServerPublishOptions.IncludePage "Star", visLangUniversal
Else
Debug.Print "Star is already set to be published."
End If
End Sub
The following code shows how to use the ExcludePage method to exclude a specific page from the set of pages to be published.
Public Sub ExcludePage_Example()
Visio.ActiveDocument.ServerPublishOptions.ExcludePage "Circle", visLangUniversal
End Sub
Setting Data Recordset-Publishing Options
If the document that you want to save as a web drawing (.vdw file) to be used with Visio Services contains data in the form of data recordsets, you can specify exactly which data recordsets you want to be connected to the web drawing file, so that the drawing is refreshed in the browser as data changes. The SetRecordSetsToPublish method allows you to publish either all recordsets in the document, or selected data recordsets that you specify by ID. The GetRecordSetsToPublish method lets you determine which data recordsets are currently set to be published.
Specifying the Data Recordsets to Publish
The following code shows how to use the SetRecordSetsToPublish method to specify which data recordsets in the active document are published when the document is saved as a web drawing. The code passes an array of Long values that represent the IDs of the data recordsets to be included.
Public Sub SetRecordsetsToPublish_Example()
Dim aryDataRecordsetIDs() As Long
ReDim aryDataRecordsetIDs(1 To ActiveDocument.DataRecordsets.Count)
aryDataRecordsetIDs(1) = 1
Visio.ActiveDocument.ServerPublishOptions.SetRecordsetsToPublish visPublishDataRecordsetSelect, aryDataRecordsetIDs
End Sub
Determining Which Data Recordsets Are Going to Be Published
The following code shows how to use the GetRecordSetsToPublish method to determine which data recordsets in the active document are currently set to publish. The method returns two out parameters that indicate whether no data recordsets, all data recordsets, or selected data recordsets are to be published; and in any but the first case, which data recordsets are the ones to be published.
Public Sub GetRecordSetsToPublish_Example()
Dim aryDataRecordsetIDs() As Long
Dim cnstPublishFlag As VisPublishDataRecordsets
Dim vsoDataRecordsets As DataRecordsets
Visio.ActiveDocument.ServerPublishOptions.GetRecordsetsToPublish cnstPublishFlag, aryDataRecordsetIDs
If Not cnstPublishFlag = visPublishDataRecordsetNone Then
Dim intCounter As Integer
For intCounter = LBound(aryDataRecordsetIDs) To UBound(aryDataRecordsetIDs)
Debug.Print ActiveDocument.DataRecordsets.ItemFromID(aryDataRecordsetIDs(intCounter)).Name
Next intCounter
Else
Debug.Print "No data recordsets are set to be published."
End If
End Sub
Comments
Post a Comment