我是州政府作業人員和新手編碼員。我正在嘗試學習編碼以減少一些乏味、容易出錯的作業。
當我從一個活動的 Word 檔案運行一個宏時,我會打開一個提示,我稱之為 Doc A:
strPrompt = "Please enter HD number and Draft." & vbCrLf & "Enter number only, followed by a space, then draft number with no spaces."
strHR = InputBox(strPrompt, "HR number and draft")
從這里開始,我希望 Word 激活檔案名中包含“HR”和 strHR 的 Word 檔案(Doc B)。
然后我想從 Doc B 復制某些文本并將其粘貼到 Doc A 中。
我在這里閱讀了其他關于如何復制和粘貼文本的帖子;只需要知道如何告訴 Word 選擇正確的 Word 檔案 (Doc B) 將該文本粘貼到。
uj5u.com熱心網友回復:
您將需要類似以下內容:
Sub Example()
Dim strPrompt As String, strHR As String
strPrompt = "Please enter HD number and Draft." & vbCrLf & "Enter number only, followed by a space, then draft number with no spaces."
strHR = "HR" & InputBox(strPrompt, "HR number and draft")
Dim sourceDoc As Document
Set sourceDoc = FindOpenDocument(strHR)
If sourceDoc Is Nothing Then
'document wasn't found
Else
'copy whatever you need from the document
End If
End Sub
Function FindOpenDocument(findText) As Document
Dim doc As Document
For Each doc In Documents
If InStr(doc.Name, findText) > 0 Then
Set FindOpenDocument = doc
Exit For
End If
Next doc
End Function
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/348381.html
下一篇:帶格式的文本連接
