- VBA - Tìm hiểu nhanh qua các ví dụ đơn giảnCần xem nếu bạn chưa biết bắt đầu từ đâu
- VBA (MS Excel) - Tìm hiểu nhanh qua các ví dụ đơn giản
- Thao tác liên quan đến tệp Word
Mã: Chọn tất cả
' ================================================= ' THAO TÁC LIÊN QUAN ĐẾN TỆP WORD ' ================================================= Dim doc As Document Set doc = Documents.Open(FileName:= "DuongDanDenTep.docx" , _ ConfirmConversions:= False , _ ReadOnly:= False , AddToRecentFiles:= False , _ PasswordDocument:= "" , PasswordTemplate:= "" , _ Revert:= False , WritePasswordDocument:= "" , _ WritePasswordTemplate:= "" , _ Format:=wdOpenFormatAuto, XMLTransform:= "") doc.Close SaveChanges:=False Dim fd As Office.FileDialog Set fd = Application.FileDialog(msoFileDialogFilePicker) With fd .AllowMultiSelect = False .Title = "Tiêu đề" .Filters.Clear .Filters.Add "Word Files (*.docx)", "*.docx" .Filters.Add "Word 2003 Files (*.doc)", "*.doc" If .Show = True Then TenTep = Dir(.SelectedItems(1)) End If End With On Error GoTo NhanhThoat_ Documents.Open (TenTep) NhanhThoat_: ActiveWindow.View = wdPrintView If Dialogs(wdDialogFileOpen).Show = - 1 Then ' Lỗi mở tệp End If
- Tham chiếu đến các đối tượng trong Word
Mã: Chọn tất cả
' ================================================= ' THAM CHIẾU ĐẾN CÁC ĐỐI TƯỢNG TRONG WORD ' ================================================= ActiveDocument Selection Application.Documents ActiveDocument.Content Documents.Item(1) Documents(1) Documents("TenTep").Paragraphs Documents("TenTep").Paragraphs(1) Documents("TenTep").Tables ActiveDocument.Tables(1).Rows ActiveDocument.Tables(1).Rows(1).Cells ActiveDocument.Tables(1).Columns(1) ActiveDocument.Tables(1).Columns(1).Cells ActiveDocument.Sections ActiveDocument.Words ActiveDocument.Shapes ActiveDocument.Range ActiveDocument.Range(Start:=0, End:=10)
- Thao tác với các đối tượng trong Word
Mã: Chọn tất cả
' ================================================= ' THAO TÁC VỚI CÁC ĐỐI TƯỢNG TRONG WORD ' ================================================= Application.WindowState = wdWindowStateMaximize ActiveDocument.PageSetup.Orientation = wdOrientLandscape ActiveDocument.PrintOut Background:=False, Copies:= 2 ActiveDocument.Password = "Mật khẩu" ActiveDocument.Words(2).Delete Selection.Copy Selection.Cut Selection.Paste ' Dán và giữ định dạng Selection.PasteAndFormat (wdFormatOriginalFormatting) ' Dán dưới dạng văn bản thuần túy Selection.PasteSpecial DataType:=wdPasteText ' Dán hình ảnh Selection.PasteSpecial DataType:=wdPasteBitmap
- Thao tác với các đối tượng trong Word (Tìm kiếm và thay thế)
Mã: Chọn tất cả
' ================================================= ' THAO TÁC VỚI CÁC ĐỐI TƯỢNG TRONG WORD (TÌM KIẾM VÀ THAY THẾ) ' ================================================= With Selection.Find .ClearFormatting .Replacement.ClearFormatting .Text = "" .Replacement.Text = "" .Font.Italic = True .Replacement.Font.Bold = True .Forward = True .Wrap = wdFindContinue .Format = True .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Execute Replace:=wdReplaceAll End With
- Thao tác với các đối tượng trong Word (Định dạng kí tự và canh lề)
Mã: Chọn tất cả
' ================================================= ' THAO TÁC VỚI CÁC ĐỐI TƯỢNG TRONG WORD (ĐỊNH DẠNG KÍ TỰ VÀ CANH LỀ) ' ================================================= ActiveDocument.Words(10).Bold = True ActiveDocument.Words(10).Underline = wdUnderlineSingle Selection.Font.Name = "Times New Roman" Selection.Font.Size = 12 Selection.Font.Bold = True Selection.Font.Italic = True Selection.Font.Underline = wdUnderlineSingle Selection.Font.Color = wdColorBlue Selection.Font.AllCaps = True Selection.Font.StrikeThrough = True Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter Selection.ParagraphFormat.LeftIndent = CentimetersToPoints(1) Selection.ParagraphFormat.FirstLineIndent = CentimetersToPoints(0.5) Selection.ParagraphFormat.SpaceBefore = 6 Selection.ParagraphFormat.SpaceAfter = 6 Selection.ParagraphFormat.LineSpacing = 18 Selection.ClearFormatting Selection.ClearAllFormatting
- Thao tác với các đối tượng trong Word (Bảng)
Mã: Chọn tất cả
' ================================================= ' THAO TÁC VỚI CÁC ĐỐI TƯỢNG TRONG WORD (BẢNG) ' ================================================= ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=3, NumColumns:=4 Dim TenBien As Table Set TenBien = ActiveDocument.Tables(1) TenBien.Rows.Add TenBien.Columns.Add TenBien.Rows(1).Select TenBien.Rows(1).Delete TenBien.Delete TenBien.Cell(1, 1).Range.Text = "Dữ liệu" MsgBox TenBien.Cell(1, 1).Range.Text TenBien.Cell(1, 1).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter TenBien.Cell(1, 1).VerticalAlignment = wdCellAlignVerticalCenter TenBien.Borders.Enable = True TenBien.Rows.HeightRule = wdRowHeightExactly TenBien.Rows.Height = CentimetersToPoints(1) With TenBien.Borders(wdBorderBottom) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth050pt .Color = wdColorBlack End With TenBien.AutoFitBehavior wdAutoFitContent TenBien.AutoFitBehavior wdAutoFitWindow TenBien.AutoFitBehavior wdAutoFitFixed TenBien.Style = "Table Grid"