I'm a state government worker and novice coder. I'm trying to learn to code to make some tedious, error-prone work a little less so.
I have a prompt that opens when running a macro from an active Word document that I'll call 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")
From here, I want Word to activate the Word document that contains "HR" & strHR in its file name (Doc B).
I then want to copy certain text from Doc B and paste it into Doc A.
I've read other posts here on how to copy text and paste it; just need to know how to tell Word to select the right Word doc (Doc B) to paste that text into.
CodePudding user response:
You will need something like the following:
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