Log in

View Full Version : Insert Bookmark data from one Word File into another Word file



MME89
07-24-2024, 02:39 AM
Hi,

Just to start, i'm new with VBA and i'm having a hard time understanding the code. I got a good share done with code I found online. But now I want to do something and I can't find what i'm looking for.
Second my native language is Dutch so if my english doesn't make sense. I do apologise up front, i'm doing the best i can ;)
If you can give me a direction where to look, I would already be glad. I searched the database but I couldn't find the information i needed. I hope someone will be able to help me out.

What i did was build a UserForm to enter data like the name of a project, the project owner, etc. This information can be inserted. I can recall the form, have the form read the bookmarks again, edit the information and insert the data back into the document. Which all works like a charm. The next step is to read these bookmarks into another Word file. In my company we make different document where we need the same information over and over again.
What i like to do is be able to open a new document (which are all standardised documents) let a UserForm pop up. Let me choose another document and have it read the bookmarks in that document. So the information that is already in the other document is inserted in this new Userform. So we don't have to fill out the same fields over and over again. I don't want it to be send directly to the bookmarks. But have the data inserted in to the fields in the userform so they can be edited.

I know there is a way but i have no clue how to get there.

This is what I have but it only opens the other document but doesn't read the bookmarks.
Also now it is a set document and i want is to give the user the choice of which document to read. I know I need to name the bookmarks the same in every file which is fine. I'm importing the data in to the documents anyway.
Reason is that our files start with the number of the project so with each project the file name will be different. So a set document in the VBA code like the example is not an option.



Private Sub CommandButton12_Click()
Dim strDocName As String
strDocName = "Y:\03. Marketing\03. Documenten\Sjablonen\20230714 Sjablonen nieuw\01.Offerte met Macro's DEF 00-0 mme.dotm"
Dim SrcDoc As Word.Document
Dim DestDoc As Word.Document
Set DestDoc = ActiveDocument
Set SrcDoc = Documents.Open(strDocName)
DestDoc.FormFields("Me.Ftitelof.Text").Result = SrcDoc.FormFields("TITofferte").Result
DestDoc.FormFields("Me.FprojectName.Text").Result = SrcDoc.FormFields("projectName").Result
DestDoc.FormFields("Me.Freferentie.Text").Result = SrcDoc.FormFields("REFra").Result
DestDoc.FormFields("Me.Fog.Text").Result = SrcDoc.FormFields("Opdrachtgever").Result
End Sub

Aussiebear
07-24-2024, 04:41 AM
Welcome to VBAX MME89. I have tidied up your post, and for someone whose native language is not English you did pretty well. Have a read through your post as it may help you with certain words. I'm not a word guru, but hopefully someone will be along shortly to assist you.

Dave
07-24-2024, 06:27 PM
Hi MME89 and Welcome to this forum. Not a Word guru either, but here's some code that seems relevant. This code will let you select a Word file, create a Word application, and then load the bookmark information from the document into an array. Following that, where you want to do some manipulation with the bookmarks, this code just msgboxes you all the bookmarks in the document. Following that, it sounds like you would want to change the savechanges part of the code to true, then close the doc and quit the Word application. I'm not exactly clear on your final outcome, but this seems like it should get you started. HTH. Dave

Sub test()
Dim objWord As Object, BkMArr() As Variant, i As Integer
Dim TargetFiles As FileDialog, Bcnt As Integer, WdStart As Boolean


'select Word file
Set TargetFiles = Application.FileDialog(msoFileDialogFilePicker)
With TargetFiles
.Title = ("Select File and Click OK")
'Makes sure the user can select only one file
.AllowMultiSelect = False
'Filter to just the following types of files to narrow down selection options
.Filters.Add "Word Files", "*.doc; *.docx; *.docm; *.dotx;*.dot", 1
If .Show = 0 Then
MsgBox ("No file was selected.")
Exit Sub
End If
End With


'get OR start Word app
On Error Resume Next
Set objWord = GetObject(, "Word.Application")
If Err.Number <> 0 Then
On Error GoTo 0
Set objWord = CreateObject("Word.Application")
WdStart = True
End If
objWord.Visible = False


'open the selected file
objWord.Documents.Open TargetFiles.SelectedItems(1)


'check for bookmarks
If objWord.activedocument.Bookmarks.Count > 1 Then
'load bookmarks into an array
ReDim BkMkArr(objWord.activedocument.Bookmarks.Count)
For i = 1 To objWord.activedocument.Bookmarks.Count
BkMkArr(i - 1) = objWord.activedocument.Range.Bookmarks(i)
Next i


'search bookmark array
With objWord.activedocument
For Bcnt = LBound(BkMkArr) To UBound(BkMkArr) - 1
'*** do stuff here to check/compare/enter bkmark info
MsgBox .Bookmarks(BkMkArr(Bcnt))
Next Bcnt
End With


Else
MsgBox "No bookmarks"
End If


'close doc and quit Word (if created)
objWord.activedocument.Close savechanges:=False
If WdStart Then
objWord.Quit
Set objWord = Nothing
End If
End Sub

Upon reflection, this is the Word forum. I assumed that you were doing this from XL and after a re-read maybe your looking for all Word VBA so I'm not sure that this will be all that helpful?

MME89
07-25-2024, 01:05 AM
Thank you verry much Aussiebear (http://www.vbaexpress.com/forum/member.php?3907-Aussiebear) for your help. Not finding the right terms for VBA things is an extra challange in finding help/coding online.
Another extra challange is my Word being in Dutch and my code is in both Dutch and English wich seems fine for VBA haha. :giggle

MME89
07-25-2024, 01:09 AM
Thanks Dave, I wil give it a go. If I can figure out your steps here. I might know how to change it into the VBA code. Otherwise I will be back with more questions :yes

gmaxey
07-25-2024, 04:20 AM
Tag the controls in the userform to match the names of the bookmarks in the source documents:


Private Sub CommandButton1_Click()
Dim varBMArray() As Variant, lngIndex As Integer
Dim oDlg As FileDialog, Bcnt As Integer, WdStart As Boolean
Dim oDocSource As Document
Dim oCtrl
Set oDlg = Application.FileDialog(msoFileDialogFilePicker)
With oDlg
.Title = ("Select File and Click OK")
.AllowMultiSelect = False
.Filters.Add "Word Files", "*.doc; *.docx; *.docm; *.dotx;*.dot", 1
If .Show = 0 Then
MsgBox ("No file was selected.")
Exit Sub
End If
End With
Set oDocSource = Documents.Open(oDlg.SelectedItems(1), , , , , , , , , , , False)
If oDocSource.Bookmarks.Count > 0 Then
ReDim varBMArray(oDocSource.Bookmarks.Count - 1, 1)
For lngIndex = 0 To oDocSource.Bookmarks.Count - 1
varBMArray(lngIndex, 0) = oDocSource.Bookmarks(lngIndex + 1).Name
varBMArray(lngIndex, 1) = oDocSource.Bookmarks(lngIndex + 1).Range.Text
Next lngIndex
Else
MsgBox "No bookmarks"
End If
oDocSource.Close wdDoNotSaveChanges
For lngIndex = 0 To UBound(varBMArray)
For Each oCtrl In Me.Controls
If oCtrl.Tag = varBMArray(lngIndex, 0) Then
oCtrl.Value = varBMArray(lngIndex, 1)
Exit For
End If
Next oCtrl
Next lngIndex

lbl_Exit:
Exit Sub
End Sub