乐博网lob.cn提示:调试环境为 vs2008 + windows2003 / windows2008 / xp / vista
VB.Net为word设置封面 Public Class ThisDocument Private MyFirstMenu As Office.CommandBarButton Private MySecondMenu As Office.CommandBarButton Private PictureContentControl1 As Microsoft.Office.Tools.Word.PictureContentControl Private PictureContentControl2 As Microsoft.Office.Tools.Word.PictureContentControl Private missing = System.Reflection.Missing.Value '在功能区中新增菜单组 Private Sub ThisDocument_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup Dim MyCommandBarPopup As Office.CommandBarPopup = Nothing Dim MyCommandBarMenu As Office.CommandBar = CType(Application.CommandBars.ActiveMenuBar, Office.CommandBar) Dim MyControlsCount As Integer = MyCommandBarMenu.Controls.Count MyCommandBarPopup = CType(MyCommandBarMenu.Controls.Add(Office.MsoControlType.msoControlPopup, missing, missing, MyControlsCount, True), Office.CommandBarPopup) If (MyCommandBarPopup IsNot Nothing) Then MyCommandBarPopup.Caption = "文件管理" MyFirstMenu = CType(MyCommandBarPopup.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, True), Office.CommandBarButton) MyFirstMenu.Caption = "设置第一本书封面" AddHandler MyFirstMenu.Click, AddressOf MyFirstMenuCommand_Click MySecondMenu = CType(MyCommandBarPopup.Controls.Add(Office.MsoControlType.msoControlButton, missing, missing, missing, True), Office.CommandBarButton) MySecondMenu.Caption = "设置第二本书封面" AddHandler MySecondMenu.Click, AddressOf MySecondMenuCommand_Click End If Me.Paragraphs(1).Range.InsertParagraphBefore() Me.Paragraphs(1).Range.Text = "第一本书封面:" & vbCrLf Dim range1 As Word.Range = Me.Paragraphs(1).Range.Characters.Last PictureContentControl1 = Me.Controls.AddPictureContentControl(range1, "PictureContentControl1") Me.Paragraphs(2).Range.InsertParagraphBefore() Me.Paragraphs(2).Range.Text = "第二本书封面:" & vbCrLf Dim range2 As Word.Range = Me.Paragraphs(2).Range.Characters.Last PictureContentControl2 = Me.Controls.AddPictureContentControl(range2, "PictureContentControl2") End Sub Private Sub ThisDocument_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown End Sub '设置第一本书封面 Private Sub MyFirstMenuCommand_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Dim MyDlg As New OpenFileDialog() MyDlg.Filter = "图像文件(JPeg,Gif,Bmp,etc.)|*.jpg;*.jpeg;*.gif; *.bmp; *.tif; *.tiff; *.png| JPeg 文件 (*.jpg;*.jpeg)|*.jpg;*.jpeg |GIF 文件 (*.gif)| *.gif |BMP 文件 (*.bmp)|*.bmp|Tiff 文件 (*.tif;*.tiff)|*.tif;*.tiff|Png 文件 (*.png)| *.png |所有文件(*.*)|*.*" MyDlg.Title = "选择第一本书封面" If (MyDlg.ShowDialog() = DialogResult.OK) Then Dim MyFileName As String = MyDlg.FileName Dim MyImage As New System.Drawing.Bitmap(MyFileName) PictureContentControl1.Image = MyImage End If End Sub '设置第二本书封面 Private Sub MySecondMenuCommand_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean) Dim MyDlg As New OpenFileDialog() MyDlg.Filter = "图像文件(JPeg,Gif,Bmp,etc.)|*.jpg;*.jpeg;*.gif; *.bmp; *.tif; *.tiff; *.png| JPeg 文件 (*.jpg;*.jpeg)|*.jpg;*.jpeg |GIF 文件 (*.gif)| *.gif |BMP 文件 (*.bmp)|*.bmp|Tiff 文件 (*.tif;*.tiff)|*.tif;*.tiff|Png 文件 (*.png)| *.png |所有文件(*.*)|*.*" MyDlg.Title = "选择第二本书封面" If (MyDlg.ShowDialog() = DialogResult.OK) Then Dim MyFileName As String = MyDlg.FileName Dim MyImage As New System.Drawing.Bitmap(MyFileName) PictureContentControl2.Image = MyImage End If End Sub End Class
|