【Solved】关于VB调用Word

   先开个题~~待所有问题得到解决再慢慢写,嘿嘿....

 

 2010-08-02

  基本上得到解决了,总结一下,其实是很简单的一个程序,就是拷贝一个word文档进行查找替换,然后再插入word文档的动作,但是在这个过程中遇到了几个问题,语言是VB6,开发环境Windows XP + office 2003标准版.

 

1) 在程序里面调用的word与其他word进程产生冲突,详情参照:

    http://topic.csdn.net/u/20100721/16/da79bde4-7737-4100-b885-8d5b160d2c32.html  

    解答:MS引用COM组件里的Microsoft Word 11.0 Object Library就不会存在这种情况.而我之前引用的是.NET中的Microsoft.Office.Interop.Word,有空再去研究一下这两种引用的不同之处.

 

2) 在hk同事的电脑上一运行这个程序就出现"此程序发生不知名错误需要关闭." 

    解答:这个问题以前也遇到过,不过是vb.net的程序,重装了SDK 就解决了,给了一些建议给HK同事,MS他太忙没有去管,所以这个问题是个迷.呵呵

 

3) 在某个服务器上运行也弹出需要关闭的错误,详情参考:

    http://topic.csdn.net/u/20100730/17/aca585ac-09f1-4ae6-b2f6-ba6974c494bd.html,

    失望的是在CSDN上发贴根本就没用,下次不浪费精力了.

 

    解答:这个问题到最后我也没办法了,因为服务器没装VB6没法调试,最后跟HK商量说能不能问问PC Team关于环境的问题或者申请安装VB6在服务器上供我调试,最后HK说用VB.NET2008重写,因为服务器上一直装有VB.NET2008,之前是为了节约成本,觉得VB6成本低所以一直坚持用VB6.

    OK,那就用VB.NET重写呗,但是在查找的时候依然遇到同样问题,不过呢可以调试就知道问题的细节了.

    System.AccessViolationException was unhandled Message="Attempted to read or write protected memory. This is often an indication that other memory is corrupt." Source="Microsoft.Office.Interop.Word" StackTrace: at Microsoft.Office.Interop.Word.Find.Execute(Object& FindText, Object& MatchCase, Object& MatchWholeWord, Object& MatchWildcards, Object& MatchSoundsLike, Object& MatchAllWordForms, Object& Forward, Object& Wrap, Object& Format, Object& ReplaceWith, Object& Replace, Object& MatchKashida, Object& MatchDiacritics, Object& MatchAlefHamza, Object& MatchControl) at TestWord.Module1.Main() in C:/Jessie/TestWord/Module1.vb:line 20 at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:

 

   于是乎在MSDN上找了一下相关的错误,发现应该是服务器上office的问题,于是乎修复了office,问题得到解决.

 

   修复办法:打开office下任何一个程序(word or excel...),点击help菜单-->Detect and Repair...--Start,注意要将
Restore my shortcuts while repairing勾上.

 

 

贴下调用word和查找替换的代码,发现网上很多人找

 

Dim doc As Word.Document Dim myRange As Word.Range Dim wdApp As Word.Application Dim strDestination as String dim strSource as String sub Main() wdApp = New Word.Application If File.Exists(strDestination) Then doc = wdApp.Documents.Open(strDestination, ReadOnly:=False) wdApp.Visible = False 'Move to end line myRange = doc.Range myRange.Select() myRange.Collapse((Word.WdCollapseDirection.wdCollapseEnd)) 'insert template myRange.InsertBreak() myRange.InsertFile(FileName:=strSource, Range:="", ConfirmConversions:=False, Link:=False, Attachment:=False) Else File.Copy(strSource, strDestination, True) doc = wdApp.Documents.Open(strDestination, ReadOnly:=False) wdApp.Visible = False End If 'call find and replace FindAndReplace("FindContect", "ReplaceContect") doc.Save() doc.Close() doc = Nothing wdApp.Application.Quit() wdApp = Nothing End sub Public Sub FindAndReplace(ByRef strFind As String, ByRef sReplace As String) Try With doc.Application.Selection.Find .ClearFormatting() .Forward = True .Wrap = Word.WdFindWrap.wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False .Text = strFind With .Replacement .ClearFormatting() .Text = sReplace End With .Execute(Replace:=Word.WdReplace.wdReplaceAll) End With Catch ex As Exception msgbox(ex.Message) End Try End Sub

 

 

你可能感兴趣的:(VB,VB.NET)