用VB操作word方法_第1頁
用VB操作word方法_第2頁
用VB操作word方法_第3頁
用VB操作word方法_第4頁
用VB操作word方法_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、1. '先引用Microsoft Word 11.0 Object Library  2. Option Explicit  3.   4. Dim WordApp As Word.Application '創(chuàng)建Word應(yīng)用程序  5.   6. Private Sub Command1_Click()  7.    &#

2、160;Dim i As Long  8.     On Error GoTo Errhandler  9.     CommonDialog1.Filter = "Word(*.Doc)|*.Doc|AllFile(*.*)|*.*"  10.     CommonDialog1.FilterIndex =

3、 1  11.     CommonDialog1.ShowOpen  12.     Set WordApp = New Word.Application '實(shí)例化  13.      CommonDialog1.FileName '打開Word文件  14.    &#

4、160;WordApp.Visible = True '顯示 Office Word 界面  15.     '或者Application.Visible = True  16.     WordApp.DisplayAlerts = False '不提示保存對(duì)話框  17.     

5、  18.     '返回段落文字,返回的段落文字在文本框控件中  19.     Text1.Text = ""  20.     For i = 1 To   21.         Text1.Text =

6、 Text1.Text & (ActiveDocument.Paragraphs(i).Range.Text & vbCrLf & vbCrLf)  22.     Next  23.       24.     '控制分頁  25.      u

7、nit:=wdStory  '將光標(biāo)移到文檔末尾  26.      wdPageBreak '在文檔末尾插入一頁  27.       28.     '設(shè)置圖片格式的頁眉  29.     If  <> wdPaneNone Then

8、60; 30.        ActiveWindow.Panes(2).Close  31.     End If  32.     If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow.ActivePane.View.Type = w

9、dOutlineView Then  33.         = wdPrintView  34.     End If  35.     ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader  36.   

10、0;  FileName:="F:資料My Pictures2013年元旦.gif", LinkToFile:=False, SaveWithDocument:=True '加載一圖片文件作為頁眉  37.      = wdAlignParagraphLeft  38.      = wdSeekMainDocument  39

11、.       40.     '設(shè)置文本格式的頁眉  41.     If  <> wdPaneNone Then  42.        ActiveWindow.Panes(2).Close  43.     End

12、0;If  44.     If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow.ActivePane.View.Type = wdOutlineView Then  45.         = wdPrintView  46.  

13、60;  End If  47.     ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader  48.     Selection.TypeText Text:="辦公室常用工具"  49.      = wdSeekMainDocument&

14、#160; 50.       51.     '隱藏頁眉的橫線  52.     WordApp.ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Borders(wdBorderBottom).Visible = False  53.      &#

15、160;54.     '取得頁眉的內(nèi)容  55.     Debug.Print WordApp.ActiveDocument.Sections(1).Headers(wdHeaderFooterFirstPage).Range.Text  '獲取WORD第一節(jié)的頁眉的文字內(nèi)容  56.       57.      &

16、#160;58.     '設(shè)置頁腳  59.     If  <> wdPaneNone Then  60.        ActiveWindow.Panes(2).Close  61.     End If  62.    

17、 If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow.ActivePane.View.Type = wdOutlineView Then  63.         = wdPrintView  64.     End If  

18、65.     ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader  66.     If  = True Then  67.        ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPag

19、eFooter  68.     Else  69.        ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader  70.     End If  71.     Selection.TypeText Tex

20、t:="2013年" '設(shè)置頁腳  72.     Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldNumPages  73.      = wdSeekMainDocument  74.       75.    

21、0;ActiveDocument.SaveAs "c:MyWord.doc" '保存最后生成的word文檔  76.           77. Errhandler:  78.     Exit Sub  79. End Sub  80.   81. Private Sub

22、60;Form_Unload(Cancel As Integer)  82.     On Error Resume Next  83.     WordApp.Quit  84.     Set WordApp = Nothing  85. End Sub  效果圖如下: &#

23、160;       2、控制Word文檔中的文本框?qū)ο髒b view plaincopy1. '先引用Microsoft Word 11.0 Object Library  2. Option Explicit  3.   4. Dim WordApp As Word.Application '創(chuàng)建Word應(yīng)用程序  5.  &#

24、160;6. Private Sub Command1_Click()  7.     On Error GoTo Errhandler  8.     CommonDialog1.Filter = "MS Office Word(*.Doc)|*.Doc|AllFile(*.*)|*.*"  9.     

25、CommonDialog1.FilterIndex = 1  10.     CommonDialog1.ShowOpen  11.     Set WordApp = New Word.Application '實(shí)例化  12.      CommonDialog1.FileName '打開Word文件

26、0; 13.     If Documents.Count >= 1 Then  14.        Text1.Text = "打開的Word文件是:" & ActiveDocument.Name & vbCrLf & vbCrLf  15.   

27、;  End If  16.     WordApp.Visible = True '顯示 Office Word 界面  17.     '或者Application.Visible = True  18.     WordApp.DisplayAlerts = Fals

28、e '不提示保存對(duì)話框  19.       20.      unit:=wdStory  '將光標(biāo)移到文檔末尾  21.      = 1  22.      = "黑體"  23.    

29、  = 18  24.      Text:="在Word文件中插入文本框?qū)ο?quot;  25.     WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter '居中顯示  26.     &

30、#160;     27.     '創(chuàng)建文本框?qū)ο?座標(biāo)(100,100),寬度200,高度200  28.     With ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 100, 100, 400, 300).Fill  29.     

31、0;    '.Transparency = 1 '設(shè)置透明色  30.          .ForeColor = vbRed '設(shè)置前景顏色  31.          .UserPicture ("F:資料My Pict

32、ures758254_960x1000_0.jpg") '設(shè)置文本框?qū)ο蟮谋尘皥D片  32.     End With  33.     ActiveDocument.Shapes(1). = "這是一個(gè)美女" '給文本框賦值  34.     'ActiveDocument.Shapes(1).Line.Tran

33、sparency = 1 '設(shè)置透明邊框線條  35.       36.     '再創(chuàng)建一個(gè)透明背景的文本框?qū)ο?#160; 37.     With ActiveDocument.Shapes.AddTextbox(msoTextOrientationHorizontal, 100, 400, 400, 300).Fill&#

34、160; 38.          .Transparency = 1 '設(shè)置透明色背景  39.          .ForeColor = vbRed '設(shè)置前景顏色  40.     End With  41. 

35、60;   ActiveDocument.Shapes(2). = "這是一個(gè)透明背景的文本框" '給文本框賦值  42.     'ActiveDocument.Shapes(2).Line.Transparency = 1 '設(shè)置透明邊框線條  43.       44.     

36、'下面是獲取文本框?qū)ο蟮膬?nèi)容  45.     Dim i As Long      46.     For i = 1 To   47.         Text1.Text = Text1.Text &

37、0;("第" & i & "個(gè)文本框的內(nèi)容:" & ActiveDocument.Shapes(i).TextFrame.TextRange.Text & vbCrLf)  48.     Next  49.       50.     ActiveDocument.

38、SaveAs "c:MyWord.doc" '保存最后生成的word文檔  51.       52. Errhandler:  53.     Exit Sub  54. End Sub  55.   56. Private Sub Form_Unload(Cancel As Intege

39、r)  57.     On Error Resume Next  58.     WordApp.Quit  59.     Set WordApp = Nothing  60. End Sub          效果圖如下: 

40、        3、在Word文檔中設(shè)置Excel風(fēng)格的頁碼vb view plaincopy1. '先引用Microsoft Word 11.0 Object Library  2. Option Explicit  3.   4. Dim WordApp As Word.Application '創(chuàng)建Word應(yīng)用程序  5. D

41、im WordDoc As Word.Document    '創(chuàng)建Word文檔對(duì)象  6.   7. Private Sub Command1_Click()  8.     Dim i As Long  9.     On Error GoTo Errhandler 

42、 10.     CommonDialog1.Filter = "Word(*.Doc)|*.Doc|AllFile(*.*)|*.*"  11.     CommonDialog1.FilterIndex = 1  12.     CommonDialog1.ShowOpen  13.     Set

43、60;WordApp = New Word.Application '實(shí)例化  14.     Set WordDoc = WordApp.Documents.Open(CommonDialog1.FileName) '選擇并打開Word文件  15.     WordApp.Visible = True '顯示 Office

44、0;Word 界面  16.     '或者Application.Visible = True  17.     WordApp.DisplayAlerts = False '不提示保存對(duì)話框  18.       19.     '設(shè)置Word文檔第一頁頁碼 &

45、#160;20.     Dim WordRange As Range  21.     Set WordRange = WordApp.ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).Range  22.            23. &#

46、160;   With WordRange  24.          .InsertAfter "第"  25.          .Font.Size = 14  26.         

47、; .Collapse Direction:=wdCollapseEnd  27.       28.          '插入頁碼域  29.          .Fields.Add Range:=WordRange, Type:=wdFieldEmpty, 

48、;Text:="PAGE  * Arabic ", PreserveFormatting:=True  30.          .Expand unit:=wdWord  31.          .InsertAfter "頁 "  3

49、2.       33.          .InsertAfter "共"  34.          .Collapse Direction:=wdCollapseEnd  35.        36.          '插入頁數(shù)域  37.          .Fields.Add Range:=

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論