Networks Business Online Việt Nam & International VH2

VBA Macro trong Microsoft Word

Đăng ngày 08 November, 2022 bởi admin
Còn đây là 06 thao tác thông dụng do mình biên soạn bằng Tiếng Việt :

1. Định dạng vùng chọn

Sub FormatSelection()

Bạn đang đọc: VBA Macro trong Microsoft Word

 With Selection.Font

 .Name = “Times
New Roman”

 .Size = 14

 .AllCaps = True

 End
With

 With Selection.ParagraphFormat

 .LeftIndent = InchesToPoints(0.5)

 .Space1

 End
With

End Sub

hoặc hoàn toàn có thể viết ngắn như sau :

Selection.Font.Color = 809458

Selection. Font. Name = ” Consolas “

2. Chèn từ và định dạng ký tự, đoạn văn

+ Trong ví dụ sau sẽ chèn từ “Title” ở đầu văn bản.

+ Đoạn văn đầu tiên sẽ được căn giữa và 01 khoảng cách 0.5
inch sẽ
được thêm vào sau đoạn văn.

+ Từ “Title” sẽ được định dạng font “Arial” cỡ chữ 24.

Sub

InsertFormatText ( )

Dim

rngFormat

As

Range

Set

rngFormat = ActiveDocument. Range ( Start : =

0

,

End

: =

0

With

rngFormat
. InsertAfter

Text

: =

" Title "

. InsertParagraphAfter

With

. Font
. Name =

" Tahoma "

. Size =

24

. Bold =

True

End

With

End

With

With

ActiveDocument. Paragraphs (

1

. Alignment = wdAlignParagraphCenter
. SpaceAfter = InchesToPoints (

0.5

End

With

End

Sub


3. Tìm 01 chuỗi trong vùng chọn

Findingand Replacing Text or Formatting | Microsoft Docs

With Selection.Find

 .Forward = True

 .Wrap = wdFindStop

 .Text = “Hello”

 .Execute

End With

4. Tìm 01 chuỗi trong hàng loạt văn bản

+ Tìm từ “Blue” trong toàn bộ văn bản và Tô Đậm từ đầu tiên tìm thấy.

With ActiveDocument.Content.Find

 .Text = “blue”

 .Forward = True

 .Execute

 If .Found = True Then .Parent.Bold = True

End With

+ Tìm 02 chuỗi “react” và “react-native” trong toàn bộ văn bản và định dạng toàn bộ
các từ tìm thấy.

Set vungChon = ActiveDocument.Content ‘chon toan bo van ban

‘to mau chu react

Do

With
vungChon.Find

 .Text = “‘react'”

 .Forward = True

 .Execute

 

 If .Found =
True Then

    .Parent.Font.Color = 45958

    .Parent.Font.Name = “Consolas”

 Else

    Exit Do

 End If

 

End With

Loop

‘to mau chu react-native

‘không reset lai vungChon se bi sai

Set
vungChon = ActiveDocument.Content

Do

With
vungChon.Find

 .Text = “‘react-native'”

 .Forward = True

 .Execute

 

 If .Found =
True Then

    .Parent.Font.Color = 45958

    .Parent.Font.Name = “Consolas”

 Else

    Exit Do

 End If

 

End With

Loop

5. Thay thế chuỗi bằng Replace

With Selection.Find

 .ClearFormatting

 .Text = “hi”

 .Replacement.ClearFormatting

 .Replacement.Text
= “hello”

 .Execute Replace:=wdReplaceAll, Forward:=True,
_

 Wrap:=wdFindContinue

End With

Ví dụ trên thay thế toàn bộ các chuỗi “hi” tìm thấy bằng chuỗi “hello” trong vùng chọn.

6. Làm việc với Table

Workingwith tables | Microsoft Docs

Tạo bảng, chèn chữ và định dạng

Ví dụ sau sẽ
tạo 01 bảng có 04 cột-03 dòng ở đầu văn bản hiện hành.

Chèn chữ “Cell
“ + CellNo ở mỗi ô và định
dạng bảng tự động.

Sub CreateNewTable()

 Dim docActive As Document

 Dim tblNew As Table

 Dim celTable As Cell

 Dim intCount As
Integer

 Set docActive = ActiveDocument

 Set tblNew = docActive.Tables.Add( _

 Range:=docActive.Range(Start:=0, End:=0), NumRows:=3,
_

 NumColumns:=4)

 intCount = 1

 For
Each celTable In
tblNew.Range.Cells

 
celTable.Range.InsertAfter “Cell
” & intCount

 
intCount = intCount + 1

 Next celTable

 tblNew.AutoFormat
Format:=wdTableFormatColorful2, _

 ApplyBorders:=True,
ApplyFont:=True, ApplyColor:=True

End Sub

Chèn chữ vào ô trong bảng

·        

dụ sau sẽ chèn chữ “Cell 1,1” vào ô đầu tiên trong bảng đầu tiên của văn bản
hiện hành.

·        
Phương
thức Cell() trả về 01 đối tượng Cell đơn lập.

·        
Thuộc
tính .Range trả về 01 đối tượng Range.

·        
Phương
thức Delete để xóa chữ đang có trong ô.

·        
Phương
thức InsertAfter để chèn chuỗi “Cell 1,1”

Sub InsertTextInCell()

 If ActiveDocument.Tables.Count >= 1 Then

 
With ActiveDocument.Tables(1).Cell(Row:=1,
Column:=1).Range

   .Delete

   .InsertAfter Text:=”Cell 1,1″

 
End With

 End
If

End Sub

Nếu vẫn chưa rõ các bạn xem thêm video clip sau:Nếu vẫn chưa rõ những bạn xem thêm video clip sau :

Source: https://vh2.com.vn
Category : Tin Học