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:
- Word document có bảng (vd: BBNT do TVGS gửi lại).
- DVDAddin: "Import Word Tables" (plan 2.8.x).
- 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
dynamic wordApp = Microsoft.VisualBasic.Interaction.CreateObject("Word.Application");
wordApp.Visible = false;Visible = false → Word chạy ngầm, không UI flicker.
Open document
dynamic doc = wordApp.Documents.Open(@"D:\Templates\BBNT.docx", ReadOnly: true);ReadOnly để không lock file gốc.
Find and replace placeholder
dynamic find = doc.Content.Find;
find.Text = "{{HangMuc}}";
find.Replacement.Text = "Móng cọc";
find.Execute(Replace: 2); // 2 = wdReplaceAllSave as PDF
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
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:
- Apply bold cho cả
{{HangMuc}}(gồm{{và}}). - 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
| Feature | Word Built-in | DVDAddin |
|---|---|---|
| Source data | Word Data Source / Excel / CSV | Excel range |
| Output format | Letter / Email / Directory | Word .docx / PDF / Merged 1 file |
| Batch performance | Chậm (UI workflow) | Nhanh (COM batch) |
| Custom filename per record | Cần macro | Built-in via pattern |
| Conditional content | If field hard to write | Excel formula trong placeholder |
| Hình ảnh dynamic | INCLUDEPICTURE workaround | Native (Content Control planned) |
| Embed PDF kết quả | Không | Tự độ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:
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:
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".
#5 — Hyperlink trong PDF không hoạt động
Word ExportAsFixedFormat default KHÔNG bao gồm hyperlink. Set:
doc.ExportAsFixedFormat(
...
BitmapMissingFonts: true,
DocStructureTags: true, ← important
CreateBookmarks: 1
);DVDAddin Merge Word bật mặc định.
Word template gallery
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
- Mở Word → New blank document.
- Format đẹp (logo, header, footer, style).
- Thêm placeholder
{{XYZ}}tại các vị trí cần điền. - Save as
.docx. - 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
- Merge Word — chi tiết command.
- Print Utilities → Converters — Word/Visio/Excel → PDF.
- Workflow: Nghiệm thu hàng loạt — combine Excel + Word + Email.
- Integration: Outlook — gửi mail kèm Word output.