Reputation: 1
I would like the result of a combo box / list box selection in word document A to be inserted into a word document B in a bookmark location, in a specific file directory, using VBA.
I have so far but does not work:
Private Sub ComboBox1_DropButtonClick()
ComboBox1.List = Array("Township Establishment", "Division of Township", "Rezone", "Consent Use", "Written Consent A", "Written Consent B", "Temporary Use", "Subdivision", "Consolidation")
Dim strInput As String
strInput = ComboBox1
Dim WriteToBookmarkRange As String
Dim rng1 As Word.Range
Dim rng2 As Word.Range
Dim strTheText As String
Dim DestFileNum As Long
Dim sDestFile As String
sDestFile = "A:\ToDo-Hermann\Hermann Target.docm" 'Location of external file
DestFileNum = FreeFile()
Open sDestFile For Output As DestFileNum 'This opens new file with name DestFileNum
Set ActiveDocument.rng1 = ActiveDocument.Range
Set ActiveDocument.rng1 = Documents("Hermann Target").Bookmarks("bm1").Range.Text
If ComboBox1 = "Township Establsihment" Then
Set rng2 = ActiveDocument.Range(rng1.End, ActiveDocument.Range.End)
If rng2.Find.Execute(FindText:="Consolidation") Then
strTheText = ActiveDocument.Range(rng1.End, rng2.Start).Text
MsgBox strTheText 'writes string to a message box
Print #DestFileNum, strTheText 'Print # will write to external file with the text strTheText
End If
End If
Close #DestFileNum 'Close the destination file
Upvotes: 0
Views: 306
Reputation:
(Answer modified following a reminder in the comments)
Still not sure exactly what you need to get from the Combo Box, but the _DropButtonClick event will return whatever was selected when you click the drop button. Maybe better to use the _Click event and return the .Value of the Checkbox.
So you need something like this:
Dim rng As Word.Range
Dim target As Word.Document
Dim targetbm As Word.Bookmark
Dim targetBmName as String
Set target = Documents.Open(Filename:=sDestFile, Visible:=False)
targetbMName = "bm1"
If target.Bookmarks.Exists(targetBmName) Then
Set rng = target.Bookmarks(targetBmName).Range
rng.Text = Combobox1.Value ' may need something else here
' re-insert the bookmark
rng.Bookmarks.Add targetBmName
Set rng = Nothing
target.Close wdSaveChanges
Else
' maybe you also want to log a problem if the bookmark isn't there.
target.Close wdDoNotSaveChanges
End If
Set target = Nothing
Upvotes: 0