Belgenin her bölümündeki sayfa sayısını saymak için Word otomasyonunu kullanma

Özet

Bu makalede, belgenin her bölümündeki sayfa sayısını belirlemek için Word ile otomasyonu nasıl kullanabileceğiniz açıklanmaktadır.

Daha Fazla Bilgi

Aşağıdaki örnek kod, C:\Mydoc.doc kaydedilen bir belge kullanır. Örnek kodu test etmek için, birden çok bölüm ve birden çok sayfa içeren yeni bir belge oluşturun ve C:\Mydoc.doc olarak kaydedin ya da var olan Word belgelerinizden birine başvurmak için koddaki belge yolunu değiştirin.

NOT: Örnek kod, bölüm sonunun yeni bir sayfa sonu zorlaması ve her sayfanın birden fazla bölüm içermediğini varsayar. Bu nedenle, örnek kodu test için C:\Mydoc.doc Word belgesini oluştururken bölüm sonları eklediğinizde, Bölüm Sonu türü olarak Sonraki Sayfa'yı seçmeniz gerekir.

Visual Basic Örneği

  1. Visual Basic'te yeni bir Standart EXE projesi oluşturun. Form1 varsayılan olarak oluşturulur.

  2. Form1'e bir komut düğmesi ekleyin ve düğmenin Click olayına aşağıdaki kodu ekleyin:

        Dim oApp As Object
        Dim oDoc As Object
        Dim oTbl As Object
    
    'Start Word and open the document.
        Set oApp = CreateObject("Word.Application")
        'For Word 2007, change the path to "c:\mydoc.docx"
        Set oDoc = oApp.Documents.Open("c:\mydoc.doc")
    
    'Repaginate the document.
        oDoc.Repaginate
    
    'Iterate each section in the document to retrieve the end page of the
        'document and compute the page count in that section. The results are 
        'displayed in the Immediate window.
        Dim oSec As Object
        Dim nStartPg As Integer, nEndPg As Integer, nSecPages As Integer
        Dim NumSections As Integer
        NumSections = oDoc.Sections.Count
        nStartPg = 1
        For Each oSec In oDoc.Sections
           nEndPg = oSec.Range.Information(3) - 1  'wdActiveEndPageNumber=3
           'Account for the last page.
           If oSec.Index = NumSections Then nEndPg = nEndPg + 1
           nSecPages = nEndPg - nStartPg + 1
           Debug.Print "Section " & oSec.Index & " --", _
                       "StartPage: " & nStartPg, _
                       "EndPage: " & nEndPg, _
                       "TotalPages: " & nSecPages
           nStartPg = nEndPg + 1
        Next
    
    'Close the document without saving changes and quit Word.
        oDoc.Close False
        oApp.Quit
    
    
  3. Uygulamayı çalıştırmak için F5 tuşuna basın ve formdaki düğmeye tıklayın. Kod, Her bölümün sayfa sayısını Anında penceresinde görüntüler.

MFC Örneği

  1. MSWord9.olb tür kitaplığında tanımlanan IDispatch arabirimlerini ve üye işlevlerini kullanan bir örnek proje oluşturmak için Microsoft Bilgi Bankası'ndaki aşağıdaki makaledeki 1 ile 12. adımları izleyin.

  2. Excel otomasyon kitaplığını kaldırmanız gerekir.

    AutoProjectDlg.cpp dosyasının en üstüne aşağıdaki satırlardan birini ekleyin:

    • Word 2002'de ve Word'ün sonraki sürümlerinde aşağıdaki satırı ekleyin:

      #include "MSWord.h"
      
    • Word 2000'de aşağıdaki satırı ekleyin:

      #include "MSWord9.h"
      
  3. AutoProjectDlg.cpp dosyasında CAutoProjectDlg::OnRun() öğesine aşağıdaki kodu ekleyin.

    COleVariant vOpt((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
    
    //Start Word.
    _Application oWord;
    oWord.CreateDispatch("Word.Application");
    oWord.SetScreenUpdating(FALSE);
    
    //Open the document.
    Documents oDocs = oWord.GetDocuments();
    // For Word 2007, use:
    // _Document oDoc = oDocs.Open(COleVariant("c:\\mydoc.docx"),
    //     vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt, vOpt, vOpt,vOpt,vOpt,vOpt,vOpt);
    _Document oDoc = oDocs.Open(COleVariant("c:\\mydoc.doc"),
             vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt,vOpt, vOpt, vOpt,vOpt,vOpt,vOpt,vOpt);
    
    //Repaginate the document.
    oDoc.Repaginate();
    
    //Iterate the collection of sections in the document to retrieve the page 
    //count for each section.
    Sections oSecs = oDoc.GetSections();
    long NumSections = oSecs.GetCount();
    long i;
    long StartPage=1; //Section start page.
    long EndPage=0;  //Section end page.
    long NumPages=0;  //Number of pages in the section.
    for(i=1;i<=NumSections;i++)
    {
    Section oSec = oSecs.Item(i);
    Range oSecRange = oSec.GetRange();
    VARIANT vInfo = oSecRange.GetInformation(3L);//wdActiveEndPageNumber=3
    //If oSec.Index = NumSections Then nEndPg = nEndPg + 1
    if(oSec.GetIndex()== NumSections) {EndPage++;}
    EndPage = vInfo.lVal-1;
    if(i==NumSections) {EndPage++;}  //Account for the last section.
    NumPages = EndPage - StartPage +1;
    char buf[5];
    sprintf(buf,"%d", NumPages);
    TRACE1("Section %d\n", oSec.GetIndex());
    TRACE3("   StartPage: %d  EndPage: %d   TotalPages: %d\n",
       StartPage, EndPage, NumPages);
    StartPage = EndPage + 1;
    }
    
    //Close the document without saving the changes, and then exit Word.
    oDoc.Close(COleVariant((short)false), vOpt, vOpt);
    oWord.Quit(COleVariant((short)false), vOpt, vOpt);
    
    
  4. Projeyi derleyin ve çalıştırın.

  5. İletişim kutusu görüntülendiğinde Çalıştır'a tıklayın. Sayı sonuçları Hata Ayıklama penceresinde görüntülenir. NumPages değişkenini Hata Ayıklama penceresine sürüklemeniz gerekir.

(c) Microsoft Corporation 2001, Tüm Hakları Saklıdır. Lori B. Turner, Microsoft Corporation'ın katkıları.

Başvurular

Daha fazla bilgi için aşağıdaki Microsoft Developer Network (MSDN) Web sitelerine bakın:

Visual Studio ile Microsoft Office Geliştirme https://msdn.microsoft.com/en-us/library/aa188489(office.10).aspx

Microsoft Office Geliştirici Merkezi https://msdn.microsoft.com/office

Ek bilgi için Microsoft Bilgi Bankası'ndaki makaleyi görüntüleyin:

220911 Visual C++ ve MFC Kullanarak Adres Mektup Birleştirme Gerçekleştirmek için Microsoft Word'u Otomatikleştirme