Word Document Properties


Listing 5-7 Modifying Words by Using a Do While...Loop Statement



Download 8,82 Mb.
Pdf ko'rish
bet113/207
Sana16.03.2022
Hajmi8,82 Mb.
#496217
1   ...   109   110   111   112   113   114   115   116   ...   207
Bog'liq
VBA For Dummies 5th Ed.pdf ( PDFDrive )

Listing 5-7
Modifying Words by Using a Do While...Loop Statement
Public Sub ChangeWords()
Dim CurrentWord As Long ‘ Current word selection.
Dim TotalWords As Long ‘ Total number of words
‘ Get the total number of words.
124
Part II: Learning the Ropes 
10_046500 ch05.qxp 12/5/06 5:35 PM Page 124


TotalWords = ActiveDocument.Words.Count
‘ Select the first word in the document.
ActiveDocument.Words(1).Select
CurrentWord = 1
‘ Keep selecting words until we run out.
Do While CurrentWord < TotalWords
‘ Make a change based on the word.
Select Case Trim(ActiveWindow.Selection.Text)
Case “Hello”
Selection.Font.Italic = True
Case “Goodbye”
Selection.Font.Bold = True
Case “Yes”
Selection.Font.Color = wdColorGreen
Case “No”
Selection.Font.Color = wdColorRed
End Select
‘ Move to the next word.
CurrentWord = CurrentWord + 1
ActiveDocument.Words(CurrentWord).Select
Loop
End Sub
The code begins by getting the word count for the active document — the
one currently selected for editing. The program uses this number to deter-
mine when to stop making changes. The 
ActiveDocument.Words
document
has a number of other interesting uses (such as those shown in the example
code), so you should keep it in mind when working with Word.
The next task is to select the first word in the document. You do this by
telling 
ActiveDocument.Words
which word to select and then using the
Select
method as shown. It’s also important to keep track of the current
word selection, so the code sets 
CurrentWord
to 
1
. This variable uses the
Long
data type because 
ActiveDocument.Words.Count
can return a 
Long
value. VBA won’t tell you whether you use the wrong data type — you must
verify the data type needs by viewing the return data type from method calls
and properties.
The 
Do While...Loop
structure compares the 
CurrentWord
value with
the 
TotalWords
value. If 
CurrentWord
is less than 
TotalWords
, it exe-
cutes the statements contained within the structure.
125

Download 8,82 Mb.

Do'stlaringiz bilan baham:
1   ...   109   110   111   112   113   114   115   116   ...   207




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish