将Word.Document传递给vba中的sub

我试图将Word.Documents的引用传递给VBA中的一个子(这样子可以运行多次,总是使用相同的“目标”文档和不同的“源”文档)。

Public Sub StructuredFileParse(wrdDocSource As Word.Document, _ Optional wrdDocTarget As Word.Document = Nothing) Dim wrdApp As Object Set wrdApp = wrdDocSource.Application If wrdDocTarget Is Nothing Then Set wrdDocTarget = wrdApp.Documents.Add End If wrdDocTarget.Activate With wrdApp.Selection.PageSetup .LeftMargin = CentimetersToPoints(2#) 'Code fails here second time it runs .RightMargin = CentimetersToPoints(2#) End With 'Do stuff Set wrdApp = Nothing End Sub Public Sub TestSub() Const ERR_APP_NOTFOUND As Long = 429 Dim wrdApp As Word.Application Dim wrdDocSource As Word.Document Dim wrdDocTarget As Word.Document On Error Resume Next ' Attempt to reference running instance of Word. Set wrdApp = GetObject(, "Word.Application") ' If Word isn't running, create a new instance. If Err = ERR_APP_NOTFOUND Then Set wrdApp = New Word.Application End If On Error GoTo 0 wrdApp.Visible = True 'Create a new word target file Set wrdDocTarget = wrdApp.Documents.Add 'Set the first word source file Set wrdDocSource = wrdApp.Documents.Open(ThisWorkbook.Path & "\" & _ "AdvisorChargeQuoteSource.dot") Call StructuredFileParse(wrdDocSource:=wrdDocSource, wrdDocTarget:=wrdDocTarget) wrdDocSource.Close Set wrdDocSource = Nothing Set wrdDocTarget = Nothing Set wrdApp = Nothing End Sub 

我第一次打电话的时候,一切正常。 但是,第二次在上面的PageSetup部分结束时出现错误(没有错误文本,只是Ok / Help窗口并停止执行)。

任何人都可以突出错误(S)?

谢谢

史蒂夫

编辑:在http://support.microsoft.com/kb/189618find解决scheme

 With wrdApp.Selection.PageSetup .LeftMargin = CentimetersToPoints(2#) 'Code fails here second time it runs .RightMargin = CentimetersToPoints(2#) End With 

 With wrdApp.Selection.PageSetup .LeftMargin = wrdApp.CentimetersToPoints(2#) .RightMargin = wrdApp.CentimetersToPoints(2#) End With 

到目前为止,似乎每次都没有错误地运行。

解决scheme可在http://support.microsoft.com/kb/189618find

 With wrdApp.Selection.PageSetup .LeftMargin = CentimetersToPoints(2#) 'Code fails here second time it runs .RightMargin = CentimetersToPoints(2#) End With 

 With wrdApp.Selection.PageSetup .LeftMargin = wrdApp.CentimetersToPoints(2#) .RightMargin = wrdApp.CentimetersToPoints(2#) End With 

到目前为止,似乎每次都没有错误地运行。