Word文档模板内容替换并保存的客户端函数

做了一个WEB客户端函数,使用Word文档作为模板,替换指定的内容,并自动保存。

 

有兴趣的大侠尝试下修改成 javascipt吧

 

<script language="vbscript">
// ---------------------------------------------------------------------------------
// 替换Word文档内容,并另存为 
// pSelect - Input Select,text为原值,value为目标值 
// theTemplate
// saveTo, 另存为,函数中未使用。
// CertificateCode 证书编号,另存的文件名使用 
// ---------------------------------------------------------------------------------
Sub buildDoc( theTemplate, pSelect, saveTo, CertificateCode ) 	
	dim cSaveTo 	

	cSaveTo = "c:\" + CertificateCode + ".doc"   	
	// 另存为文件"
	
	Set objWordDoc = CreateObject("Word.Document") 
	ObjWordDoc.Application.Visible=True 	
	ObjWordDoc.Application.Documents.Open theTemplate, False 

	Set myRange = ObjWordDoc.Application.ActiveDocument.Content

	For intCount = 0 to pSelect.length - 1

		myRange.Find.ClearFormatting
    	myRange.Find.Replacement.ClearFormatting
    
    	FindText 		= pSelect.options(intCount).text
    	ReplaceWith		= pSelect.options(intCount).value

    	MatchCase		= false
    	MatchWholeWord	= false 
    	MatchWildcards	= false 
    	MatchSoundsLike	= false 	
    	MatchAllWordForms = false 
    	Forward	= true  
    	// wdFindAsk  2 	wdFindContinue  1 		wdFindStop  0 
    	Wrap 	= 1    
    	Format	= false 
    	// wdReplaceAll = 2   wdReplaceNone = 0  wdReplaceOne = 1 
    	nReplace = 2 
    	call myRange.Find.Execute( FindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike, MatchAllWordForms, Forward, Wrap, Format, ReplaceWith, nReplace )
		
	Next 
	
	// 另外为...。如果目标文件已经存在,ActiveDocument.SaveAs会直接覆盖,所以先判断文件是否存在
	// 查询文件是否已经存在。VBA中有个函数Dir,没试验成功。
	lFind = false 
	set objFileSearch = ObjWordDoc.Application.FileSearch
	objFileSearch.FileName = CertificateCode + ".doc"
	objFileSearch.LookIn = "c:\\"
	if objFileSearch.Execute > 0 then 
		// MsgBox objFileSearch.FoundFiles.Count
    	For I = 1 to objFileSearch.FoundFiles.Count
			// msgbox ucase(cSaveTo) + "|" + ucase(objFileSearch.FoundFiles( I ))
    		if  ucase(cSaveTo) = ucase(objFileSearch.FoundFiles( I ) ) then 
				lFind = true
				exit for 
			end if 
    	Next 
	end if 
	
	if lFind = false then 
		// 直接另存为 
		ObjWordDoc.Application.ActiveDocument.SaveAs cSaveTo 
	else 
		// vbYesNoCancel + vbExclamation )
		slt = MsgBox( "系统自动命名保存的证书草稿文件“" + cSaveTo + "”已经存在,需要覆盖吗?", 3 + 48 ) 
		Select Case slt
         Case 6 	// vbYes		 	
			ObjWordDoc.Application.ActiveDocument.SaveAs cSaveTo 
         Case 7 	// vbNo		 	
			set dlgSaveAs = ObjWordDoc.Application.Dialogs( 84 )
            dlgSaveAs.Name = cSaveTo
            dlgSaveAs.Show
         Case Else
      	End Select
	end if 
	
	// 激活指定的应用
	ObjWordDoc.Application.Activate
	
End Sub 

</script>

你可能感兴趣的:(C++,c,C#,VBScript,VBA)