Consulting

Results 1 to 6 of 6

Thread: Insert Bookmark data from one Word File into another Word file

  1. #1
    VBAX Newbie
    Joined
    Jul 2024
    Posts
    5
    Location

    Question Insert Bookmark data from one Word File into another Word file

    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
    Last edited by Aussiebear; 07-24-2024 at 04:42 AM.

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,255
    Location
    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.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    861
    Location
    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?

  4. #4
    VBAX Newbie
    Joined
    Jul 2024
    Posts
    5
    Location
    Thank you verry much Aussiebearfor 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.

  5. #5
    VBAX Newbie
    Joined
    Jul 2024
    Posts
    5
    Location
    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

  6. #6
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,400
    Location
    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
    Greg

    Visit my website: http://gregmaxey.com

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •