【发布时间】:2019-10-07 15:56:31
【问题描述】:
我一直在通过论坛和 YouTube 自学 VBA。我编写了一些使用If 语句的代码。如果该声明为真,我可以选择调用另一个 SUB 或仅在同一个 SUB 中编写代码。我的问题是,如果我调用 SUB 并且它使用 MS Word,如果代码使用循环,它是否相当于启动 MS Word 然后退出并一次又一次地重新启动它?属于第二个 SUB 的部分我评论了,下面没有包含。
Option Explicit
Sub CreateWordDocEarlyBinding()
'Declared Variables for Sub.
Dim sourceDoc As String
Dim lastRow As Long
Dim rowLoop As Long
'Adding code for MS Word here
Dim WdApp As Application
Set WdApp = New Word.Application
'Not sure I need this for any reason.
Dim columnLoop As Long
'Find last row.
lastRow = Sheet1.Cells(Sheet1.Rows.Count, 1).End(xlUp).Row
'Looping through column.
For rowLoop = 2 To lastRow
'Not sure I need at this point for code.
'columnLoop = Sheet1.Range(rowLoop, 1)
'Checking cells for A Conditon.
If Sheet1.Cells(rowLoop, 1).Value = "Test String 1" Then
If Sheet1.Cells(rowLoop, 1).Offset(0, 1).Value >= 0.1 Then
If Sheet1.Cells(rowLoop, 1).Offset(0, 1).Value < 1 Then
sourceDoc = "Test1.docx"
Call CreateNewSourceDoc(sourceDoc, WdApp)
Else
sourceDoc = "Test2.docx"
Call CreateNewSourceDoc(sourceDoc, WdApp)
End If
End If
'Checking cells for B condition.
ElseIf Sheet1.Cells(rowLoop, 1).Value = "Test String 2" Then
If Sheet1.Cells(rowLoop, 1).Offset(0, 1).Value >= 0.5 Then
If Sheet1.Cells(rowLoop, 1).Offset(0, 1).Value < 5 Then
sourceDoc = "Test3.docx"
Call CreateNewSourceDoc(sourceDoc, WdApp)
Else
sourceDoc = "Test4.docx"
Call CreateNewSourceDoc(sourceDoc, WdApp)
End If
End If
End If
'Checking next row.
Next
WdApp.Quit
Set WdApp = Nothing
End Sub
我在这里添加了第二个 SUB 的代码以进行澄清。这段代码每次循环时都会创建一个新的 word 实例吗?如果是这样,我最好去掉下面的SUB。
Sub CreateNewSourceDoc(sourceDoc As String, WdApp As Application)
'Declaring variables
Dim newFolderName As String
Dim newFilePath As String
'Source folder
filePath = ThisWorkbook.Path & "\"
'New Folder
newFolderName = Sheet1.Cells(rowLoop, 1) & " " & Sheet1.Cells(rowLoop, 2)
MkDir filePath & newFolderName
'New file path
newFilePath = filePath & newFolderName & "\"
'I commented the below code out to see if it is correct.
'Set WdApp = New Word.Application
With WdApp
.Visible = False
'.Activate
.Documents.Open filePath & sourceDoc
.ActiveDocument.SaveAs2 Filename:=newFilePath & Sheet1.Cells(rowLoop, 1) & " " & Sheet1.Cells(rowLoop, 2) & ".docx"
End With
sourceDoc = vbNullString
End Sub
【问题讨论】:
-
如果您将
WdApp传递给子,它可以使用它。否则它将不得不启动一个新实例,是的。 -
@GSerg ,不确定您的意思。我将代码添加到合并两个 SUB 的帖子中。当它们分开时,第一个 SUB 将“sourceDoc”传递给第二个 SUB,第二个 SUB 打开并保存单词 doc。为了避免新实例,我将在哪里传递“wdApp”。抱歉,我是新手。
-
New Word.Application每次执行 Word 时都会启动一个新实例,正如阅读代码所表明的那样。如果您想重用一个实例,那么除了您现在要传递的sourceDoc之外,您还必须将WdApp传递给第二个SUB;它将作为附加参数传递给第二个SUB.. 不过我不知道WdApp来自哪里;您在第一个SUB中使用它,但它不在该代码中的任何位置,除了单行WdApp.Quit。 -
@KenWhite ,我编辑了第一个 SUB 代码,并认为我将 WdApp 和 sourceDoc 传递给了第二个 sub。现在怎么样了?
-
@KenWhite ,我只看到一个单词实例,它在 sub.Thanks 的末尾消失了。