'API 宣告
Public Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Long, ByVal wMapType As Long) As Integer
Dim SCode As Integer
V_caps_lock_stats = My.Computer.Keyboard.CapsLock '是否按下caps lock
For Ii = 1 To V_data1.Length
tmp_str = Mid(V_data1, Ii, 1) '第幾個字
SCode = MapVirtualKey(Asc(tmp_str), 0) '找出scane code
If V_caps_lock_stats = False Then
keybd_event(&H14, 0, &H0, 0) 'caps lockey down
keybd_event(&H14, 0, &H2, 0)
End If
keybd_event(Asc(tmp_str), SCode, 0, 0) '送出字串以及scan code
keybd_event(Asc(tmp_str), SCode, 2, 0) '
If V_caps_lock_stats = False Then '
keybd_event(&H14, 0, &H0, 0) 'caps lockey down
keybd_event(&H14, 0, &H2, 0) '
End If
Next
'For Ii = 1 To 200
' Threading.Thread.Sleep(1)
' Application.DoEvents()
'Next
keybd_event(13, MapVirtualKey(13, 0), &H0, 0) 'enter lockey down
keybd_event(13, MapVirtualKey(13, 0), &H2, 0)
2009年4月9日 星期四
2009年4月6日 星期一
vb.net 播放音樂
Public Class Form1
' 宣告 API
Private Declare Function mciSendStringA Lib "winmm.dll" _
(ByVal lpstrCommand As String, ByVal lpstrReturnString As String, _
ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer
Private Sub Button1_Click(ByVal s As Object, ByVal e As EventArgs) Handles Button1.Click
PlayMidiFile("C:\死了都要愛.mp3") ' 播放 MP3 音樂
'或
'PlayMidiFile("C:\頑皮豹.mid") ' 播放 MIDI 音樂
End Sub
Private Sub Button2_Click(ByVal s As Object, ByVal e As EventArgs) Handles Button2.Click
StopMidi() ' 停止播放
End Sub
Private Sub Button3_Click(ByVal s As Object, ByVal e As EventArgs) Handles Button3.Click
PauseMidi() ' 暫停播放
End Sub
Private Sub Button4_Click(ByVal s As Object, ByVal e As EventArgs) Handles Button4.Click
ContinueMidi() ' 繼續播放
End Sub
Private Function PlayMidiFile(ByVal MusicFile As String) As Boolean
If System.IO.File.Exists(MusicFile) Then
mciSendStringA("stop music", "", 0, 0)
mciSendStringA("close music", "", 0, 0)
mciSendStringA("open " & MusicFile & " alias music", "", 0, 0)
PlayMidiFile = mciSendStringA("play music", "", 0, 0) = 0
End If
End Function
Private Function StopMidi() As Boolean
StopMidi = mciSendStringA("stop music", "", 0, 0) = 0
mciSendStringA("close music", "", 0, 0)
End Function
Private Function PauseMidi() As Boolean
Return mciSendStringA("pause music", "", 0, 0) = 0
End Function
Private Function ContinueMidi() As Boolean
Return mciSendStringA("play music", "", 0, 0) = 0
End Function
End Class
' 宣告 API
Private Declare Function mciSendStringA Lib "winmm.dll" _
(ByVal lpstrCommand As String, ByVal lpstrReturnString As String, _
ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer
Private Sub Button1_Click(ByVal s As Object, ByVal e As EventArgs) Handles Button1.Click
PlayMidiFile("C:\死了都要愛.mp3") ' 播放 MP3 音樂
'或
'PlayMidiFile("C:\頑皮豹.mid") ' 播放 MIDI 音樂
End Sub
Private Sub Button2_Click(ByVal s As Object, ByVal e As EventArgs) Handles Button2.Click
StopMidi() ' 停止播放
End Sub
Private Sub Button3_Click(ByVal s As Object, ByVal e As EventArgs) Handles Button3.Click
PauseMidi() ' 暫停播放
End Sub
Private Sub Button4_Click(ByVal s As Object, ByVal e As EventArgs) Handles Button4.Click
ContinueMidi() ' 繼續播放
End Sub
Private Function PlayMidiFile(ByVal MusicFile As String) As Boolean
If System.IO.File.Exists(MusicFile) Then
mciSendStringA("stop music", "", 0, 0)
mciSendStringA("close music", "", 0, 0)
mciSendStringA("open " & MusicFile & " alias music", "", 0, 0)
PlayMidiFile = mciSendStringA("play music", "", 0, 0) = 0
End If
End Function
Private Function StopMidi() As Boolean
StopMidi = mciSendStringA("stop music", "", 0, 0) = 0
mciSendStringA("close music", "", 0, 0)
End Function
Private Function PauseMidi() As Boolean
Return mciSendStringA("pause music", "", 0, 0) = 0
End Function
Private Function ContinueMidi() As Boolean
Return mciSendStringA("play music", "", 0, 0) = 0
End Function
End Class
2009年3月6日 星期五
在GAC 裡加入 ActiproSoftware.Wizard.dll(2.80.0.0)
在CMD 下
CD C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322
gacutil /i ActiproSoftware.Wizard.dll
CD C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322
gacutil /i ActiproSoftware.Wizard.dll
2008年12月25日 星期四
2008年12月11日 星期四
vb.net 動態call form
Private Sub ShowForm(ByVal FormName As String)
Dim ProjectName As String =
Reflection.Assembly.GetExecutingAssembly.GetName.Name
Try
Dim tyOfStringVariable As Type = Type.GetType(ProjectName & "." &
FormName)
Dim frmObject As Object = Activator.CreateInstance(tyOfStringVariable)
DirectCast(frmObject, Form).StartPosition =
FormStartPosition.CenterParent
DirectCast(frmObject, Form).ShowDialog()
Catch ex As Exception
' TODO
End Try
End Sub
Dim ProjectName As String =
Reflection.Assembly.GetExecutingAssembly.GetName.Name
Try
Dim tyOfStringVariable As Type = Type.GetType(ProjectName & "." &
FormName)
Dim frmObject As Object = Activator.CreateInstance(tyOfStringVariable)
DirectCast(frmObject, Form).StartPosition =
FormStartPosition.CenterParent
DirectCast(frmObject, Form).ShowDialog()
Catch ex As Exception
' TODO
End Try
End Sub
2008年12月2日 星期二
vb.net ->word
Public Class WordOpLib
Private oWordApplic As Word.Application
Private oDocument As Word.Document
Private oRange As Word.Range
Private oSelection As Word.Selection
Public Sub New()
'啟動com word介面
oWordApplic = New Word.Application
oWordApplic.Visible = True
End Sub
'設置選定文本
Public Sub SetRange(ByVal para As Integer)
oRange = oDocument.Paragraphs(para).Range
oRange.Select()
End Sub
Public Sub SetRange(ByVal para As Integer, ByVal sent As Integer)
oRange = oDocument.Paragraphs(para).Range.Sentences(sent)
oRange.Select()
End Sub
Public Sub SetRange(ByVal startpoint As Integer, ByVal endpoint As Integer, ByVal flag As Boolean)
If flag = True Then
oRange = oDocument.Range(startpoint, endpoint)
oRange.Select()
Else
End If
End Sub
'生成空的新文檔
Public Sub NewDocument()
Dim missing = System.Reflection.Missing.Value
Dim isVisible As Boolean = True
oDocument = oWordApplic.Documents.Add(missing, missing, missing, missing)
oDocument.Activate()
End Sub
'使用範本生成新文檔
Public Sub NewDocWithModel(ByVal FileName As String)
Dim missing = System.Reflection.Missing.Value
Dim isVisible As Boolean = True
Dim strName As String
strName = FileName
oDocument = oWordApplic.Documents.Add(strName, missing, missing, isVisible)
oDocument.Activate()
End Sub
'打開已有文檔
Public Sub OpenFile(ByVal FileName As String)
Dim strName As String
Dim isReadOnly As Boolean
Dim isVisible As Boolean
Dim missing = System.Reflection.Missing.Value
strName = FileName
isReadOnly = False
isVisible = True
oDocument = oWordApplic.Documents.Open(strName, missing, isReadOnly, missing, missing, missing, missing, missing, missing, missing, missing, isVisible, missing, missing, missing, missing)
oDocument.Activate()
End Sub
Public Sub OpenFile(ByVal FileName As String, ByVal isReadOnly As Boolean)
Dim strName As String
Dim isVisible As Boolean
Dim missing = System.Reflection.Missing.Value
strName = FileName
isVisible = True
oDocument = oWordApplic.Documents.Open(strName, missing, isReadOnly, missing, missing, missing, missing, missing, missing, missing, missing, isVisible, missing, missing, missing, missing)
oDocument.Activate()
End Sub
'退出Word
Public Sub Quit()
Dim missing = System.Reflection.Missing.Value
oWordApplic.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(oWordApplic)
oWordApplic = Nothing
End Sub
'關閉所有打開的文檔
Public Sub CloseAllDocuments()
oWordApplic.Documents.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
End Sub
'關閉當前的文檔
Public Sub CloseCurrentDocument()
oDocument.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
End Sub
'保存當前文檔
Public Sub Save()
Try
oDocument.Save()
Catch
MsgBox(Err.Description)
End Try
End Sub
'另存為文檔
Public Sub SaveAs(ByVal FileName As String)
Dim strName As String
Dim missing = System.Reflection.Missing.Value
strName = FileName
oDocument.SaveAs(strName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing)
End Sub
'保存為Html檔
Public Sub SaveAsHtml(ByVal FileName As String)
Dim missing = System.Reflection.Missing.Value
Dim strName As String
strName = FileName
Dim format = CInt(Word.WdSaveFormat.wdFormatHTML)
oDocument.SaveAs(strName, format, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing)
End Sub
'插入文本
Public Sub InsertText(ByVal text As String)
oWordApplic.Selection.TypeText(text)
End Sub
'插入一個空行
Public Sub InsertLineBreak()
oWordApplic.Selection.TypeParagraph()
End Sub
'插入指定行數的空行
Public Sub InsertLineBreak(ByVal lines As Integer)
Dim i As Integer
For i = 1 To lines
oWordApplic.Selection.TypeParagraph()
Next
End Sub
'插入表格
Public Sub InsertTable(ByRef table As DataTable)
Dim oTable As Word.Table
Dim rowIndex, colIndex, NumRows, NumColumns As Integer
rowIndex = 1
colIndex = 0
NumRows = table.Rows.Count + 1
NumColumns = table.Columns.Count
oTable = oDocument.Tables.Add(oWordApplic.Selection.Range(), NumRows, NumColumns)
'初始化列
Dim Row As DataRow
Dim Col As DataColumn
For Each Col In table.Columns
colIndex = colIndex + 1
oTable.Cell(1, colIndex).Range.InsertAfter(Col.ColumnName)
Next
'將行添入表格
For Each Row In table.Rows
rowIndex = rowIndex + 1
colIndex = 0
For Each Col In table.Columns
colIndex = colIndex + 1
oTable.Cell(rowIndex, colIndex).Range.InsertAfter(Row(Col.ColumnName))
Next
Next
oTable.AllowAutoFit = True
oTable.ApplyStyleFirstColumn = True
oTable.ApplyStyleHeadingRows = True
End Sub
'設置對齊
Public Sub SetAlignment(ByVal strType As String)
Select Case strType
Case "center"
oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
Case "left"
oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
Case "right"
oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
Case "justify"
oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify
End Select
End Sub
'設置字體
Public Sub SetStyle(ByVal strFont As String)
Select Case strFont
Case "bold"
oWordApplic.Selection.Font.Bold = 1
Case "italic"
oWordApplic.Selection.Font.Italic = 1
Case "underlined"
oWordApplic.Selection.Font.Subscript = 1
End Select
End Sub
'取消字體風格
Public Sub DissableStyle()
oWordApplic.Selection.Font.Bold = 0
oWordApplic.Selection.Font.Italic = 0
oWordApplic.Selection.Font.Subscript = 0
End Sub
'設置字體字型大小
Public Sub SetFontSize(ByVal nSize As Integer)
oWordApplic.Selection.Font.Size = nSize
End Sub
'跳過本頁
Public Sub InsertPageBreak()
Dim pBreak As Integer
pBreak = CInt(Word.WdBreakType.wdPageBreak)
oWordApplic.Selection.InsertBreak(pBreak)
End Sub
'轉到書簽
Public Sub GotoBookMark(ByVal strBookMark As String)
Dim missing = System.Reflection.Missing.Value
Dim BookMark = CInt(Word.WdGoToItem.wdGoToBookmark)
oWordApplic.Selection.GoTo(BookMark, missing, missing, strBookMark)
End Sub
'判斷書簽是否存在
Public Function BookMarkExist(ByVal strBookMark As String) As Boolean
Dim Exist As Boolean
Exist = oDocument.Bookmarks.Exists(strBookMark)
Return Exist
End Function
'轉到文檔結尾
Public Sub GotoTheEnd()
Dim missing = System.Reflection.Missing.Value
Dim unit = Word.WdUnits.wdStory
oWordApplic.Selection.EndKey(unit, missing)
End Sub
'轉到文檔開頭
Public Sub GotoTheBegining()
Dim missing = System.Reflection.Missing.Value
Dim unit = Word.WdUnits.wdStory
oWordApplic.Selection.HomeKey(unit, missing)
End Sub
'轉到表格
Public Sub GotoTheTable(ByVal ntable As Integer)
'Dim missing = System.Reflection.Missing.Value
'Dim what = Word.WdGoToItem.wdGoToTable
'Dim which = Word.WdGoToDirection.wdGoToFirst
'Dim count = ntable
'oWordApplic.Selection.GoTo(what, which, count, missing)
'oWordApplic.Selection.ClearFormatting()
'oWordApplic.Selection.Text = ""
oRange = oDocument.Tables(ntable).Cell(1, 1).Range
oRange.Select()
End Sub
'轉到表格的某個儲存格
Public Sub GotoTableCell(ByVal ntable As Integer, ByVal nRow As Integer, ByVal nColumn As Integer)
oRange = oDocument.Tables(ntable).Cell(nRow, nColumn).Range
oRange.Select()
End Sub
'表格中轉到右面的儲存格
Public Sub GotoRightCell()
Dim missing = System.Reflection.Missing.Value
Dim direction = Word.WdUnits.wdCell
oWordApplic.Selection.MoveRight(direction, missing, missing)
End Sub
'表格中轉到左面的儲存格
Public Sub GotoLeftCell()
Dim missing = System.Reflection.Missing.Value
Dim direction = Word.WdUnits.wdCell
oWordApplic.Selection.MoveLeft(direction, missing, missing)
End Sub
'表格中轉到下麵的儲存格
Public Sub GotoDownCell()
Dim missing = System.Reflection.Missing.Value
Dim direction = Word.WdUnits.wdCell
oWordApplic.Selection.MoveDown(direction, missing, missing)
End Sub
'表格中轉到上面的儲存格
Public Sub GotoUpCell()
Dim missing = System.Reflection.Missing.Value
Dim direction = Word.WdUnits.wdCell
oWordApplic.Selection.MoveUp(direction, missing, missing)
End Sub
'插入圖片
Public Sub InsertPic(ByVal FileName As String)
Dim missing = System.Reflection.Missing.Value
oWordApplic.Selection.InlineShapes.AddPicture(FileName, False, True, missing)
End Sub
End Class
Private oWordApplic As Word.Application
Private oDocument As Word.Document
Private oRange As Word.Range
Private oSelection As Word.Selection
Public Sub New()
'啟動com word介面
oWordApplic = New Word.Application
oWordApplic.Visible = True
End Sub
'設置選定文本
Public Sub SetRange(ByVal para As Integer)
oRange = oDocument.Paragraphs(para).Range
oRange.Select()
End Sub
Public Sub SetRange(ByVal para As Integer, ByVal sent As Integer)
oRange = oDocument.Paragraphs(para).Range.Sentences(sent)
oRange.Select()
End Sub
Public Sub SetRange(ByVal startpoint As Integer, ByVal endpoint As Integer, ByVal flag As Boolean)
If flag = True Then
oRange = oDocument.Range(startpoint, endpoint)
oRange.Select()
Else
End If
End Sub
'生成空的新文檔
Public Sub NewDocument()
Dim missing = System.Reflection.Missing.Value
Dim isVisible As Boolean = True
oDocument = oWordApplic.Documents.Add(missing, missing, missing, missing)
oDocument.Activate()
End Sub
'使用範本生成新文檔
Public Sub NewDocWithModel(ByVal FileName As String)
Dim missing = System.Reflection.Missing.Value
Dim isVisible As Boolean = True
Dim strName As String
strName = FileName
oDocument = oWordApplic.Documents.Add(strName, missing, missing, isVisible)
oDocument.Activate()
End Sub
'打開已有文檔
Public Sub OpenFile(ByVal FileName As String)
Dim strName As String
Dim isReadOnly As Boolean
Dim isVisible As Boolean
Dim missing = System.Reflection.Missing.Value
strName = FileName
isReadOnly = False
isVisible = True
oDocument = oWordApplic.Documents.Open(strName, missing, isReadOnly, missing, missing, missing, missing, missing, missing, missing, missing, isVisible, missing, missing, missing, missing)
oDocument.Activate()
End Sub
Public Sub OpenFile(ByVal FileName As String, ByVal isReadOnly As Boolean)
Dim strName As String
Dim isVisible As Boolean
Dim missing = System.Reflection.Missing.Value
strName = FileName
isVisible = True
oDocument = oWordApplic.Documents.Open(strName, missing, isReadOnly, missing, missing, missing, missing, missing, missing, missing, missing, isVisible, missing, missing, missing, missing)
oDocument.Activate()
End Sub
'退出Word
Public Sub Quit()
Dim missing = System.Reflection.Missing.Value
oWordApplic.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(oWordApplic)
oWordApplic = Nothing
End Sub
'關閉所有打開的文檔
Public Sub CloseAllDocuments()
oWordApplic.Documents.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
End Sub
'關閉當前的文檔
Public Sub CloseCurrentDocument()
oDocument.Close(Word.WdSaveOptions.wdDoNotSaveChanges)
End Sub
'保存當前文檔
Public Sub Save()
Try
oDocument.Save()
Catch
MsgBox(Err.Description)
End Try
End Sub
'另存為文檔
Public Sub SaveAs(ByVal FileName As String)
Dim strName As String
Dim missing = System.Reflection.Missing.Value
strName = FileName
oDocument.SaveAs(strName, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing)
End Sub
'保存為Html檔
Public Sub SaveAsHtml(ByVal FileName As String)
Dim missing = System.Reflection.Missing.Value
Dim strName As String
strName = FileName
Dim format = CInt(Word.WdSaveFormat.wdFormatHTML)
oDocument.SaveAs(strName, format, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing)
End Sub
'插入文本
Public Sub InsertText(ByVal text As String)
oWordApplic.Selection.TypeText(text)
End Sub
'插入一個空行
Public Sub InsertLineBreak()
oWordApplic.Selection.TypeParagraph()
End Sub
'插入指定行數的空行
Public Sub InsertLineBreak(ByVal lines As Integer)
Dim i As Integer
For i = 1 To lines
oWordApplic.Selection.TypeParagraph()
Next
End Sub
'插入表格
Public Sub InsertTable(ByRef table As DataTable)
Dim oTable As Word.Table
Dim rowIndex, colIndex, NumRows, NumColumns As Integer
rowIndex = 1
colIndex = 0
NumRows = table.Rows.Count + 1
NumColumns = table.Columns.Count
oTable = oDocument.Tables.Add(oWordApplic.Selection.Range(), NumRows, NumColumns)
'初始化列
Dim Row As DataRow
Dim Col As DataColumn
For Each Col In table.Columns
colIndex = colIndex + 1
oTable.Cell(1, colIndex).Range.InsertAfter(Col.ColumnName)
Next
'將行添入表格
For Each Row In table.Rows
rowIndex = rowIndex + 1
colIndex = 0
For Each Col In table.Columns
colIndex = colIndex + 1
oTable.Cell(rowIndex, colIndex).Range.InsertAfter(Row(Col.ColumnName))
Next
Next
oTable.AllowAutoFit = True
oTable.ApplyStyleFirstColumn = True
oTable.ApplyStyleHeadingRows = True
End Sub
'設置對齊
Public Sub SetAlignment(ByVal strType As String)
Select Case strType
Case "center"
oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter
Case "left"
oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
Case "right"
oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
Case "justify"
oWordApplic.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustify
End Select
End Sub
'設置字體
Public Sub SetStyle(ByVal strFont As String)
Select Case strFont
Case "bold"
oWordApplic.Selection.Font.Bold = 1
Case "italic"
oWordApplic.Selection.Font.Italic = 1
Case "underlined"
oWordApplic.Selection.Font.Subscript = 1
End Select
End Sub
'取消字體風格
Public Sub DissableStyle()
oWordApplic.Selection.Font.Bold = 0
oWordApplic.Selection.Font.Italic = 0
oWordApplic.Selection.Font.Subscript = 0
End Sub
'設置字體字型大小
Public Sub SetFontSize(ByVal nSize As Integer)
oWordApplic.Selection.Font.Size = nSize
End Sub
'跳過本頁
Public Sub InsertPageBreak()
Dim pBreak As Integer
pBreak = CInt(Word.WdBreakType.wdPageBreak)
oWordApplic.Selection.InsertBreak(pBreak)
End Sub
'轉到書簽
Public Sub GotoBookMark(ByVal strBookMark As String)
Dim missing = System.Reflection.Missing.Value
Dim BookMark = CInt(Word.WdGoToItem.wdGoToBookmark)
oWordApplic.Selection.GoTo(BookMark, missing, missing, strBookMark)
End Sub
'判斷書簽是否存在
Public Function BookMarkExist(ByVal strBookMark As String) As Boolean
Dim Exist As Boolean
Exist = oDocument.Bookmarks.Exists(strBookMark)
Return Exist
End Function
'轉到文檔結尾
Public Sub GotoTheEnd()
Dim missing = System.Reflection.Missing.Value
Dim unit = Word.WdUnits.wdStory
oWordApplic.Selection.EndKey(unit, missing)
End Sub
'轉到文檔開頭
Public Sub GotoTheBegining()
Dim missing = System.Reflection.Missing.Value
Dim unit = Word.WdUnits.wdStory
oWordApplic.Selection.HomeKey(unit, missing)
End Sub
'轉到表格
Public Sub GotoTheTable(ByVal ntable As Integer)
'Dim missing = System.Reflection.Missing.Value
'Dim what = Word.WdGoToItem.wdGoToTable
'Dim which = Word.WdGoToDirection.wdGoToFirst
'Dim count = ntable
'oWordApplic.Selection.GoTo(what, which, count, missing)
'oWordApplic.Selection.ClearFormatting()
'oWordApplic.Selection.Text = ""
oRange = oDocument.Tables(ntable).Cell(1, 1).Range
oRange.Select()
End Sub
'轉到表格的某個儲存格
Public Sub GotoTableCell(ByVal ntable As Integer, ByVal nRow As Integer, ByVal nColumn As Integer)
oRange = oDocument.Tables(ntable).Cell(nRow, nColumn).Range
oRange.Select()
End Sub
'表格中轉到右面的儲存格
Public Sub GotoRightCell()
Dim missing = System.Reflection.Missing.Value
Dim direction = Word.WdUnits.wdCell
oWordApplic.Selection.MoveRight(direction, missing, missing)
End Sub
'表格中轉到左面的儲存格
Public Sub GotoLeftCell()
Dim missing = System.Reflection.Missing.Value
Dim direction = Word.WdUnits.wdCell
oWordApplic.Selection.MoveLeft(direction, missing, missing)
End Sub
'表格中轉到下麵的儲存格
Public Sub GotoDownCell()
Dim missing = System.Reflection.Missing.Value
Dim direction = Word.WdUnits.wdCell
oWordApplic.Selection.MoveDown(direction, missing, missing)
End Sub
'表格中轉到上面的儲存格
Public Sub GotoUpCell()
Dim missing = System.Reflection.Missing.Value
Dim direction = Word.WdUnits.wdCell
oWordApplic.Selection.MoveUp(direction, missing, missing)
End Sub
'插入圖片
Public Sub InsertPic(ByVal FileName As String)
Dim missing = System.Reflection.Missing.Value
oWordApplic.Selection.InlineShapes.AddPicture(FileName, False, True, missing)
End Sub
End Class
2008年12月1日 星期一
在word 裡面插入統計圖表
'方法一:用word內建函數
Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop
Imports owc11 = Microsoft.Office.Interop.Owc11
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oRng As Word.Range
Dim oShape As Word.InlineShape
Dim oChart As Object
oShape = oDoc.Bookmarks.Item("\endofdoc").Range.InlineShapes.AddOLEObject( _
ClassType:="MSGraph.Chart", FileName _
:="", LinkToFile:=False, DisplayAsIcon:=False)
oChart = oShape.OLEFormat.Object
oChart.charttype = 5 'xlLine = 4
'都沒有=立體圖(長條圖) 1=一般 4=線圖 5=圓餅圖
oChart.Application.Update()
oChart.Application.Quit()
'If desired, you can proceed from here using the Microsoft Graph
'Object model on the oChart object to make additional changes to the
'chart.
oShape.Width = oWord.InchesToPoints(6.25)
oShape.Height = oWord.InchesToPoints(3.57)
'方法2:用vb.net(word) 內建控制項繪製,轉成圖片,插入word文件
'ACS=AxCharSpace
Dim oWord As New Word.Application
Dim oDoc As Word.Document
Dim oRng As Word.Range
Dim ii As Integer
Dim rowIndex, colIndex As Integer
Dim missing = System.Reflection.Missing.Value
Dim aX, aY
ReDim aX(5)
ReDim aY(5)
Dim DS As New DataSet
DS.Tables.Add()
DS.Tables(0).Columns.Add("姓名")
DS.Tables(0).Columns.Add("成績")
DS.Tables(0).Rows.Add()
DS.Tables(0).Rows(DS.Tables(0).Rows.Count - 1).Item("姓名") = "陳小邦"
DS.Tables(0).Rows(DS.Tables(0).Rows.Count - 1).Item("成績") = "93"
DS.Tables(0).Rows.Add()
DS.Tables(0).Rows(DS.Tables(0).Rows.Count - 1).Item("姓名") = "張小千"
DS.Tables(0).Rows(DS.Tables(0).Rows.Count - 1).Item("成績") = "66"
DS.Tables(0).Rows.Add()
DS.Tables(0).Rows(DS.Tables(0).Rows.Count - 1).Item("姓名") = "林小狗"
DS.Tables(0).Rows(DS.Tables(0).Rows.Count - 1).Item("成績") = "89"
DS.Tables(0).Rows.Add()
DS.Tables(0).Rows(DS.Tables(0).Rows.Count - 1).Item("姓名") = "王宜靜"
DS.Tables(0).Rows(DS.Tables(0).Rows.Count - 1).Item("成績") = "99"
DS.Tables(0).Rows.Add()
DS.Tables(0).Rows(DS.Tables(0).Rows.Count - 1).Item("姓名") = "張大飛"
DS.Tables(0).Rows(DS.Tables(0).Rows.Count - 1).Item("成績") = "78"
DS.Tables(0).Rows.Add()
DS.Tables(0).Rows(DS.Tables(0).Rows.Count - 1).Item("姓名") = "桶一針"
DS.Tables(0).Rows(DS.Tables(0).Rows.Count - 1).Item("成績") = "79"
For ii = 0 To DS.Tables(0).Rows.Count - 1
aX(ii) = DS.Tables(0).Rows(ii).Item("姓名")
aY(ii) = DS.Tables(0).Rows(ii).Item("成績")
Next
oRng = oDoc.Bookmarks.Item("\endofdoc").Range
oRng.InsertParagraphAfter()
Dim Chart1 As Owc11.ChChart
Chart1 = ChartSpace1.Charts.Add() '在ChartSpace1繪圖空間內建一個新圖表(繒圖區)
Dim Chart1_Series1 As Owc11.ChSeries
'宣告資料列...
Chart1_Series1 = Chart1.SeriesCollection.Add(0) '在Chart1圖表中加一個資料列
Chart1_Series1.Type = Owc11.ChartChartTypeEnum.chChartTypeBarClustered
'命名資料系列(名稱將在圖例中顯示出來)
Chart1_Series1.SetData(Owc11.ChartDimensionsEnum.chDimSeriesNames, Owc11.ChartSpecialDataSourcesEnum.chDataLiteral, "成績")
'將資料組中的資料填入圖表
Chart1_Series1.SetData(Owc11.ChartDimensionsEnum.chDimCategories, Owc11.ChartSpecialDataSourcesEnum.chDataLiteral, aX) '姓名軸
Chart1_Series1.SetData(Owc11.ChartDimensionsEnum.chDimValues, Owc11.ChartSpecialDataSourcesEnum.chDataLiteral, aY) '成績軸
Chart1_Series1.SetData(ChartDimensionsEnum.chDimHighValues, Owc11.ChartSpecialDataSourcesEnum.chDataLiteral, 100)
'匯出圖片
ACS.ExportPicture(Application.StartupPath & "\1.GIF", "GIF", ACS.Width, ACS.Height)
'尋找文件結尾
missing = System.Reflection.Missing.Value
Dim unit = Word.WdUnits.wdStory
oWord.Selection.EndKey(unit, missing)
oWord.Selection.InlineShapes.AddPicture(Application.StartupPath & "\1.GIF", False, True, missing)
'Add text after the chart.
oRng.InsertParagraphAfter()
oDoc.SaveAs(Application.StartupPath & "\1.doc")
oDoc.Close(True)
oWord.Quit(True)
oDoc = Nothing
oWord = Nothing
Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop
Imports owc11 = Microsoft.Office.Interop.Owc11
Dim oWord As Word.Application
Dim oDoc As Word.Document
Dim oRng As Word.Range
Dim oShape As Word.InlineShape
Dim oChart As Object
oShape = oDoc.Bookmarks.Item("\endofdoc").Range.InlineShapes.AddOLEObject( _
ClassType:="MSGraph.Chart", FileName _
:="", LinkToFile:=False, DisplayAsIcon:=False)
oChart = oShape.OLEFormat.Object
oChart.charttype = 5 'xlLine = 4
'都沒有=立體圖(長條圖) 1=一般 4=線圖 5=圓餅圖
oChart.Application.Update()
oChart.Application.Quit()
'If desired, you can proceed from here using the Microsoft Graph
'Object model on the oChart object to make additional changes to the
'chart.
oShape.Width = oWord.InchesToPoints(6.25)
oShape.Height = oWord.InchesToPoints(3.57)
'方法2:用vb.net(word) 內建控制項繪製,轉成圖片,插入word文件
'ACS=AxCharSpace
Dim oWord As New Word.Application
Dim oDoc As Word.Document
Dim oRng As Word.Range
Dim ii As Integer
Dim rowIndex, colIndex As Integer
Dim missing = System.Reflection.Missing.Value
Dim aX, aY
ReDim aX(5)
ReDim aY(5)
Dim DS As New DataSet
DS.Tables.Add()
DS.Tables(0).Columns.Add("姓名")
DS.Tables(0).Columns.Add("成績")
DS.Tables(0).Rows.Add()
DS.Tables(0).Rows(DS.Tables(0).Rows.Count - 1).Item("姓名") = "陳小邦"
DS.Tables(0).Rows(DS.Tables(0).Rows.Count - 1).Item("成績") = "93"
DS.Tables(0).Rows.Add()
DS.Tables(0).Rows(DS.Tables(0).Rows.Count - 1).Item("姓名") = "張小千"
DS.Tables(0).Rows(DS.Tables(0).Rows.Count - 1).Item("成績") = "66"
DS.Tables(0).Rows.Add()
DS.Tables(0).Rows(DS.Tables(0).Rows.Count - 1).Item("姓名") = "林小狗"
DS.Tables(0).Rows(DS.Tables(0).Rows.Count - 1).Item("成績") = "89"
DS.Tables(0).Rows.Add()
DS.Tables(0).Rows(DS.Tables(0).Rows.Count - 1).Item("姓名") = "王宜靜"
DS.Tables(0).Rows(DS.Tables(0).Rows.Count - 1).Item("成績") = "99"
DS.Tables(0).Rows.Add()
DS.Tables(0).Rows(DS.Tables(0).Rows.Count - 1).Item("姓名") = "張大飛"
DS.Tables(0).Rows(DS.Tables(0).Rows.Count - 1).Item("成績") = "78"
DS.Tables(0).Rows.Add()
DS.Tables(0).Rows(DS.Tables(0).Rows.Count - 1).Item("姓名") = "桶一針"
DS.Tables(0).Rows(DS.Tables(0).Rows.Count - 1).Item("成績") = "79"
For ii = 0 To DS.Tables(0).Rows.Count - 1
aX(ii) = DS.Tables(0).Rows(ii).Item("姓名")
aY(ii) = DS.Tables(0).Rows(ii).Item("成績")
Next
oRng = oDoc.Bookmarks.Item("\endofdoc").Range
oRng.InsertParagraphAfter()
Dim Chart1 As Owc11.ChChart
Chart1 = ChartSpace1.Charts.Add() '在ChartSpace1繪圖空間內建一個新圖表(繒圖區)
Dim Chart1_Series1 As Owc11.ChSeries
'宣告資料列...
Chart1_Series1 = Chart1.SeriesCollection.Add(0) '在Chart1圖表中加一個資料列
Chart1_Series1.Type = Owc11.ChartChartTypeEnum.chChartTypeBarClustered
'命名資料系列(名稱將在圖例中顯示出來)
Chart1_Series1.SetData(Owc11.ChartDimensionsEnum.chDimSeriesNames, Owc11.ChartSpecialDataSourcesEnum.chDataLiteral, "成績")
'將資料組中的資料填入圖表
Chart1_Series1.SetData(Owc11.ChartDimensionsEnum.chDimCategories, Owc11.ChartSpecialDataSourcesEnum.chDataLiteral, aX) '姓名軸
Chart1_Series1.SetData(Owc11.ChartDimensionsEnum.chDimValues, Owc11.ChartSpecialDataSourcesEnum.chDataLiteral, aY) '成績軸
Chart1_Series1.SetData(ChartDimensionsEnum.chDimHighValues, Owc11.ChartSpecialDataSourcesEnum.chDataLiteral, 100)
'匯出圖片
ACS.ExportPicture(Application.StartupPath & "\1.GIF", "GIF", ACS.Width, ACS.Height)
'尋找文件結尾
missing = System.Reflection.Missing.Value
Dim unit = Word.WdUnits.wdStory
oWord.Selection.EndKey(unit, missing)
oWord.Selection.InlineShapes.AddPicture(Application.StartupPath & "\1.GIF", False, True, missing)
'Add text after the chart.
oRng.InsertParagraphAfter()
oDoc.SaveAs(Application.StartupPath & "\1.doc")
oDoc.Close(True)
oWord.Quit(True)
oDoc = Nothing
oWord = Nothing
訂閱:
文章 (Atom)