Skip to content

Integration: Word

Tích hợp sâu DVDAddin với Microsoft Word — beyond Merge Word, đến custom template, batch convert, table extraction.

Use cases

1. Merge Word hàng loạt

→ Xem Merge Word.

Cơ bản: Excel data → Word template → output N file.

2. Convert Word → PDF batch

→ Xem Print Utilities → Converters.

Folder .docx → folder .pdf qua Word COM.

3. Extract bảng từ Word → Excel

Workflow:

  1. Word document có bảng (vd: BBNT do TVGS gửi lại).
  2. DVDAddin: "Import Word Tables" (plan 2.8.x).
  3. Output: bảng Excel với cấu trúc giữ nguyên.

(Hiện tại workaround: Word → Save As HTML → Excel mở HTML file → recognize table.)

4. Sinh hợp đồng từ template

Combine Excel data + Word template với clause động:

HỢP ĐỒNG SỐ {{SoHD}}

Bên A: {{TenA}}
Bên B: {{TenB}}

Giá trị: {{GiaTri|VND}} ({{GiaTri|VND}} bằng chữ)

Thời gian: {{NgayBatDau|dd/MM/yyyy}} đến {{NgayKetThuc|dd/MM/yyyy}}

[CONDITIONAL: nếu GiaTri > 1 tỷ → chèn clause Bảo lãnh thực hiện hợp đồng]

Conditional clause via formula trong Excel:

=IF(GiaTri > 1000000000, "Điều X. Bảo lãnh thực hiện hợp đồng: ...", "")

→ Output Word có/không clause tùy giá trị.

Word COM patterns

DVDAddin gọi Word qua COM API. Patterns:

Open Word instance

csharp
dynamic wordApp = Microsoft.VisualBasic.Interaction.CreateObject("Word.Application");
wordApp.Visible = false;

Visible = false → Word chạy ngầm, không UI flicker.

Open document

csharp
dynamic doc = wordApp.Documents.Open(@"D:\Templates\BBNT.docx", ReadOnly: true);

ReadOnly để không lock file gốc.

Find and replace placeholder

csharp
dynamic find = doc.Content.Find;
find.Text = "{{HangMuc}}";
find.Replacement.Text = "Móng cọc";
find.Execute(Replace: 2);  // 2 = wdReplaceAll

Save as PDF

csharp
doc.ExportAsFixedFormat(
    OutputFileName: @"D:\Output\BBNT-001.pdf",
    ExportFormat: 17,  // 17 = wdExportFormatPDF
    OpenAfterExport: false,
    OptimizeFor: 0,    // 0 = wdExportOptimizeForPrint
    BitmapMissingFonts: true,
    DocStructureTags: true,
    CreateBookmarks: 1  // 1 = wdExportCreateHeadingBookmarks
);

DVDAddin Merge Word implement đúng pattern này.

Cleanup

csharp
doc.Close(SaveChanges: false);
wordApp.Quit();
Marshal.ReleaseComObject(doc);
Marshal.ReleaseComObject(wordApp);

KHÔNG release COM → process Word zombie chiếm memory.

Word template best practices

Dùng style nhất quán

Define styles trong Word template:

  • Heading 1 — tiêu đề chính.
  • Heading 2/3 — subsections.
  • Normal — body text.
  • Caption — chú thích bảng/ảnh.

→ Output PDF có outline đẹp + table of contents tự generate.

Placeholder không break style

Sai: placeholder nằm trong text bold → khi replace, mất bold.

Bold: {{HangMuc}}    ← khi Word find/replace, đôi khi mất format

Đúng: placeholder DỄ BREAK trong character formatting đặc biệt. Test bằng cách:

  1. Apply bold cho cả {{HangMuc}} (gồm {{}}).
  2. Find/replace bằng tay trong Word → verify bold giữ nguyên.

Nếu mất → đổi placeholder syntax sang non-special chars hoặc dùng Content Control.

Content Controls (advanced)

Word 2010+ có Content Controls thay text placeholder:

  • Insert → Quick Parts → Document Property → Custom.
  • Tag = HangMuc.

DVDAddin (plan 2.8.x) sẽ support Content Controls — bảo toàn format perfect.

Hình ảnh trong template

Insert Picture placeholder qua Picture Content Control:

  • Developer tab → Picture Content Control.
  • Tag = Anh.

DVDAddin set Anh.Path = "D:\Photos\IMG_001.jpg" → Word render ảnh tại vị trí.

(Hiện tại workaround: dùng INCLUDEPICTURE field.)

Mail Merge Word built-in vs DVDAddin

FeatureWord Built-inDVDAddin
Source dataWord Data Source / Excel / CSVExcel range
Output formatLetter / Email / DirectoryWord .docx / PDF / Merged 1 file
Batch performanceChậm (UI workflow)Nhanh (COM batch)
Custom filename per recordCần macroBuilt-in via pattern
Conditional contentIf field hard to writeExcel formula trong placeholder
Hình ảnh dynamicINCLUDEPICTURE workaroundNative (Content Control planned)
Embed PDF kết quảKhôngTự động (PdfSharp merge)

DVDAddin tốt hơn cho batch generation. Word built-in tốt cho 1-time letter.

Convert .doc → .docx batch

File Word cũ .doc (97-2003 format) cần convert sang .docx:

DVDAddin chưa có UI → workaround Python script:

python
import win32com.client
import glob

word = win32com.client.Dispatch('Word.Application')
word.Visible = False

for path in glob.glob(r'D:\OldDocs\*.doc'):
    doc = word.Documents.Open(path)
    new_path = path + 'x'  # .doc → .docx
    doc.SaveAs(new_path, FileFormat=16)  # 16 = wdFormatXMLDocument
    doc.Close()
    print(f'Converted: {path}')

word.Quit()

Plan 2.9.x: built-in command "Convert .doc → .docx batch" trong DVDAddin.

Word automation troubleshooting

#1 — "Word.Application" prog ID not registered

Word chưa cài. Cài Microsoft Word từ Office 365 / standalone.

#2 — Word zombie process sau khi addin crash

Task Manager → kết thúc tất cả WINWORD.EXE.

Hoặc PowerShell:

powershell
Get-Process WINWORD -ErrorAction SilentlyContinue | Stop-Process -Force

#3 — "Document is locked for editing"

File Word đang được mở trong instance Word khác (visible). Đóng tất cả Word manual trước khi chạy DVDAddin.

#4 — Output PDF font lạ

Word render PDF dùng font installed trên máy. Nếu template dùng font không có:

  • Word substitute font khác → output xấu.
  • Fix: cài đầy đủ font template yêu cầu.
  • Hoặc embed font: Word → File → Options → Save → "Embed fonts in the file".

Word ExportAsFixedFormat default KHÔNG bao gồm hyperlink. Set:

csharp
doc.ExportAsFixedFormat(
    ...
    BitmapMissingFonts: true,
    DocStructureTags: true,    ← important
    CreateBookmarks: 1
);

DVDAddin Merge Word bật mặc định.

Templates DVDAddin shipped:

  • BBNT_Template.docx — biên bản nghiệm thu.
  • HopDong_Template.docx — hợp đồng (chưa shipped).
  • BaoCao_Tuan_Template.docx — báo cáo tuần (chưa shipped).
  • NhatKy_Template.docx — nhật ký công trình.

User download từ:

  • C:\DVDAddin\Template\ (cài cùng addin).
  • Hoặc DVD Cons → Mẫu Hồ sơ → menu mở từng template.

Tự thiết kế template

  1. Mở Word → New blank document.
  2. Format đẹp (logo, header, footer, style).
  3. Thêm placeholder {{XYZ}} tại các vị trí cần điền.
  4. Save as .docx.
  5. Test với DVDAddin Merge Word — verify placeholder thay đúng.

Share template với team

Lưu trên cloud (OneDrive / Google Drive / SharePoint):

  • Folder shared: \\Cloud\DA_Templates\.
  • Mỗi PM dùng template này để consistency cross-projects.

Liên quan

Released under DVDAddin License.