Bu makalede, Microsoft nasıl bir utomate için Word belgeye çeşitli bölümlerini üstbilgi ve altbilgi metni almak ve ayarlamak için göstermektedir.
Örnek otomasyon istemci ilk kez bir belge içindeki bölümleri oluşturmak gösterilmiştir. Sonra örnek bu bölüm için üstbilgi ve altbilgi metni eklemek gösterilmiştir. Örnek kod oluşturan belgeyi üç bölümden oluşur:
- Ilk bölümde iki sayfa içerir. Bu bölümde, ilk sayfa üstbilgisi ve altbilgisi ikinci sayfasından farklıdır.
- Ikinci bölümü, üç sayfa içerir. Bu bölümde, ilk sayfa üstbilgisi ve altbilgisi diğer iki sayfalardan farklıdır.
- Üçüncü bölüm iki sayfa içerir. Bu bölümde, tüm sayfaları aynı üstbilgiyi ve altbilgiyi paylaşır.
Not: ilk sayfasında
bölüm nesnesi için <a2>
DifferentFirstPageHeaderFooter</a2> özelliği tarafından belirlenen olup olmadığı bir bölümü farklı üstbilgi veya altbilgi içerir.
Örnek otomasyon istemci Ayrıca üstbilgi ve altbilgi bilgileri bir belgeden alma gösterilmiştir. Bir belgeyi açmadan sonra istemci, belgenin her sayfasında dolaşır ve üstbilgiler ve altbilgiler hata ayıklama penceresinde, o sayfanın verir. Not yalnızca
ilk sayfa üstbilgi/altbilgi ve
birincil için örnek kod üstbilgi/altbilgi hesapları;
çift sayfa üstbilgi/altbilgi için hesap.
ÖRNEK
- Yeni bir Standart EXE Project, Visual Basic'te oluşturun. Varsayılan olarak, Form1 oluşturulur.
- Proje menüsünde Başvurular ' ı tıklatın. Aşağıdakilerden birini tıklatın ve sonra Tamam ' ı tıklatın:
- Word 2007 için Microsoft Word 12.0 Nesne Kitaplığı ' ı tıklatın.
- Word 2003 için Microsoft Word 11.0 Nesne Kitaplığı ' ı tıklatın.
- Word 2002 için Microsoft Word 10.0 Nesne Kitaplığı ' ı tıklatın.
- Word 2000'de, Microsoft Word 9.0 Nesne Kitaplığı ' nı tıklatın.
- Iki komut düğmelerini Form1'e ekleyin. Belge OluşturCommand1 başlığını değiştirmek ve Üstbilgi/altbilgi almak için Command2 başlığını değiştirin.
- Add the following code to Form1:
Option Explicit
Private Sub Command1_Click()
SetHeadersFooters
End Sub
Private Sub Command2_Click()
GetHeadersFooters App.Path & "\mydoc.doc"
End Sub
Sub SetHeadersFooters()
Dim oApp As Word.Application
Dim oSec As Word.Section
Dim oDoc As Word.Document
'Create a new document in Word
Set oApp = New Word.Application
Set oDoc = oApp.Documents.Add
With oDoc
'=== SECTION 1 ==================================================
'Add two pages to the first section where the first page in the
'section has different headers and footers than the second page
Set oSec = .Sections(1)
oSec.PageSetup.DifferentFirstPageHeaderFooter = True
oSec.Range.InsertAfter "Text on Page 1 (Section 1)"
.Range(oSec.Range.End - 1).InsertBreak wdPageBreak
oSec.Range.InsertAfter "Text on Page 2 (Section 1)"
'Add the headers/footers for the first section (that contains two
'pages)
oSec.Headers(wdHeaderFooterFirstPage).Range.Text = _
"Page1 -- Section 1 First Page Header"
oSec.Headers(wdHeaderFooterPrimary).Range.Text = _
"Page2 -- Section 1 Primary Header"
oSec.Footers(wdHeaderFooterFirstPage).Range.Text = _
"Page1 -- Section 1 First Page Footer"
oSec.Footers(wdHeaderFooterPrimary).Range.Text = _
"Page2 -- Section 1 Primary Footer"
'=== SECTION 2 ==================================================
'Add a new section containing three pages where the first page in
'the section has different headers and footers than the other two
'pages
.Range(oSec.Range.End - 1).InsertBreak wdSectionBreakNextPage
Set oSec = .Sections(2)
oSec.PageSetup.DifferentFirstPageHeaderFooter = True
oSec.Range.InsertAfter "Text on Page 3 (Section 2)"
.Range(oSec.Range.End - 1).InsertBreak wdPageBreak
oSec.Range.InsertAfter "Text on Page 4 (Section 2)"
.Range(oSec.Range.End - 1).InsertBreak wdPageBreak
oSec.Range.InsertAfter "Text on Page 5 (Section 2)"
'Add the headers/footers for the second section (that contains
'three pages) -- notice that the second and third pages in this
'section will contain the primary header/footer
oSec.Headers(wdHeaderFooterFirstPage).LinkToPrevious = False
oSec.Headers(wdHeaderFooterFirstPage).Range.Text = _
"Page3 -- Section 2 First Page Header"
oSec.Headers(wdHeaderFooterPrimary).LinkToPrevious = False
oSec.Headers(wdHeaderFooterPrimary).Range.Text = _
"Page4and5 -- Section 2 Primary Header"
oSec.Footers(wdHeaderFooterFirstPage).LinkToPrevious = False
oSec.Footers(wdHeaderFooterFirstPage).Range.Text = _
"Page3 -- Section 2 First Page Footer"
oSec.Footers(wdHeaderFooterPrimary).LinkToPrevious = False
oSec.Footers(wdHeaderFooterPrimary).Range.Text = _
"Page4and5 -- Section 2 Primary Footer"
'=== SECTION 3 ==================================================
'Add a new section containing two pages that all have the same
'header/footer
.Range(oSec.Range.End - 1).InsertBreak wdSectionBreakNextPage
Set oSec = .Sections(3)
oSec.PageSetup.DifferentFirstPageHeaderFooter = False
oSec.Range.InsertAfter "Text on Page 6 (Section 3)"
.Range(oSec.Range.End - 1).InsertBreak wdPageBreak
oSec.Range.InsertAfter "Text on Page 7 (Section 3)"
'Add the headers/footers for the third section (that contains
' two pages)
oSec.Headers(wdHeaderFooterPrimary).LinkToPrevious = False
oSec.Headers(wdHeaderFooterPrimary).Range.Text = _
"Page6and7 -- Section 3 Primary Header Only"
oSec.Footers(wdHeaderFooterPrimary).LinkToPrevious = False
oSec.Footers(wdHeaderFooterPrimary).Range.Text = _
"Page6and7 -- Section 3 Primary Footer Only"
'Save the document
.SaveAs App.Path & "\mydoc.doc"
End With
'Make Word visible to examine the document
oApp.Visible = True
End Sub
Sub GetHeadersFooters(sFile As String)
Dim oApp As Word.Application
Dim oDoc As Word.Document
Dim oSec As Word.Section
Dim oPageStart As Word.Range
Dim iPage As Integer, iTotalPages As Integer, iSection As Integer
Dim sHeader As String, sFooter As String
'Open the document
Set oApp = New Word.Application
Set oDoc = oApp.Documents.Open(sFile)
iTotalPages = oDoc.ComputeStatistics(wdStatisticPages)
'Retrieve the headers and footers on each page
With oDoc
iSection = 0
For iPage = 1 To iTotalPages
'Go to the page represented by the page number iPage and
'retrieve its section
Set oPageStart = oDoc.GoTo(What:=wdGoToPage, _
Which:=wdGoToAbsolute, Count:=iPage)
Set oSec = oPageStart.Sections(1)
'If this is a different section than the one in the previous
'iteration and it has a first page header/.footer, then
'retrieve the first page header/footer for this section.
'Otherwise, retrieve the primary header/footer for this section
If (iSection < oSec.Index) And _
(oSec.PageSetup.DifferentFirstPageHeaderFooter) Then
sHeader = oSec.Headers(wdHeaderFooterFirstPage).Range.Text
sFooter = oSec.Footers(wdHeaderFooterFirstPage).Range.Text
Else
sHeader = oSec.Headers(wdHeaderFooterPrimary).Range.Text
sFooter = oSec.Footers(wdHeaderFooterPrimary).Range.Text
End If
iSection = oSec.Index
'Display the results in the debug window
Debug.Print "Page " & iPage & ", Section " & iSection & _
":" & vbCrLf
Debug.Print " Header: " & sHeader
Debug.Print " Footer: " & sFooter
Next
End With
'Make Word visible to compare the document with the results in the
'debug window
oApp.Visible = True
End Sub
- Programı çalıştırmak için F5 tuşuna basın.
- Form üzerinde Belge Oluştur'u tıklatın ve Visual Basic istemci, Word'Ü başlatır ve yeni bir belge oluşturur. Bu işlem tamamlandığında, belgeyi görülebilir. Belgeyi inceleyin ve farklı üstbilgiler ve altbilgiler, belge boyunca unutmayın.
- Yeni belgeyi kapatın ve sonra da Word'nden çıkın.
- Form üzerinde Üstbilgi/altbilgi Al'ı tıklatın. Visual Basic istemci, Word'Ü başlatır ve belgedeki her sayfanın üstbilgi/altbilgi metnini alır. Sonuçları, hata ayıklama penceresinde görüntülenir; bu belge sonuçları karşılaştırın.
Microsoft Word Otomasyonu Visual Basic'den gösteren ek bir örnek kodu hakkında ek bilgi için Microsoft Knowledge Base'deki makaleleri görüntülemek üzere aşağıdaki makale numaralarını tıklatın:
220607
(http://support.microsoft.com/kb/220607/EN-US/
)
Nasıl yapılır: Visual Basic'den adres mektup birleştirme gerçekleştirmek için Word otomatikleştirme
261999
(http://support.microsoft.com/kb/261999/EN-US/
)
Bir ADO Recordset, otomasyon ile bir Word tablosu için Aktarım nasıl kullanılır