當前位置:編程學習大全網 - 編程語言 - vb如何用宏寫word

vb如何用宏寫word

宏操作是在打開word文檔以後錄制的,也就是說運行宏也要在打開word文檔後進行,所以要把宏復制到vb環境中去運行的話,就得先在vb中操作打開word文檔,參考下面代碼:(演示前請先c;下建壹個文件名為test.doc的word空文檔,操作完成後,妳會發現C盤裏有個aa.doc文件,妳打開就會發現宏操作寫的壹則通知)

Private Sub Command1_Click()

Dim MyWord As Word.Application

Dim MyWordBook As Word.Document

Set MyWord = New Word.Application

Set MyWordBook = MyWord.Documents.Add("c:\test.doc") '打開test.doc用戶自定義的空白WORD文檔

MyWordBook.Activate

'3.放置宏代碼

With MyWordBook

'此處就可以插入宏代碼了

' Macro1 Macro

' 宏在 2009-9-20 由 adsl 錄制

'

Selection.Font.Size = 30

Selection.FormattedText.Bold = True

Selection.Font.Color = wdColorBlack

Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter

Selection.TypeText Text:=" 通知"

Selection.Font.Color = wdColorRed

Selection.TypeParagraph

Selection.Font.Size = 22

Selection.TypeText Text:=" 今天下午三壹)"

Selection.MoveLeft Unit:=wdCharacter, Count:=2

Selection.TypeText Text:="("

Selection.MoveRight Unit:=wdCharacter, Count:=2

Selection.TypeText Text:="班 全體同學四點鐘到操場進行體育課考試。"

Selection.TypeParagraph

Selection.TypeParagraph

Selection.TypeParagraph

Selection.TypeText Text:= _

" "

Selection.InsertDateTime DateTimeFormat:="yyyy-MM-dd", InsertAsField:= _

False, DateLanguage:=wdEnglishUS, CalendarType:=wdCalendarWestern, _

InsertAsFullWidth:=False

Selection.MoveUp Unit:=wdLine, Count:=1

Selection.Font.Size = 22

Selection.MoveRight Unit:=wdCharacter, Count:=1

Selection.TypeParagraph

Selection.Font.Size = 16

Selection.TypeText Text:=" "

Selection.Font.Color = wdColorRed

End With

'4.保存,關閉文檔,退出

MyWord.Visible = False '設置WORD可見

MyWordBook.SaveAs FileName:="c:\aa.doc"

MyWordBook.Close

MyWord.Quit

Set MyWordBook = Nothing

Set MyWord = Nothing

MsgBox "操作完畢", vbOKOnly, "提醒"

unload me

End Sub

  • 上一篇:初二語文暑假作業答案
  • 下一篇:什麽是復合螺紋切削循環?
  • copyright 2024編程學習大全網