Consulting

Results 1 to 5 of 5

Thread: Populating Word Bookmarks without deleting it

  1. #1

    Populating Word Bookmarks without deleting it

    Hi guys,

    I am trying to populate a word bookmark through vba in excel, however my current code deletes the bookmark.
    I want to populate the bookmark and save the file. Once I do, I want to populate it with a different value and save it as a different file. When using my current code, after populating for the first time, it gives me an error saying that the bookmark does not exist. I guess it is deleting the bookmark once it is populated.

    Would appreciate if you guys could also help me out with the code for saving the file right after it populates the bookmark.

    Sub CartaModelo()
    Dim wApp As Word.Application
    Dim bmrange As Range
     
    n = Cells(1, 2).End(xlDown).Row
     
    Set wApp = CreateObject("Word.Application")
    
    With wApp
    .Visible = True
    .Documents.Open ("C:\Users\3177475\Documents\PIS e Cofins\Carta Modelo\Carta Modelo v4.docx")
     
    For i = 2 To n
    .Selection.Goto What:=wdGoToBookmark, Name:="Valor"
    .Selection.TypeText Text:=Cells(i, 2).Value
    Next i
     
    End With
    End Sub
    Last edited by Paul_Hossler; 10-17-2017 at 06:53 AM. Reason: Added CODE Tags

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    I had created a sub long time ago to do that -- just to make it easy on my self

    I created 2 empty bookmarks and ran drv1 to put something in
    Then ran drv2 to make sure they were updated


    Option Explicit
    
    Sub drv1()
        Call FillABookmark("MARK1", "aaaaaaaaaaaaaaaaaaaaaaaaa")
        Call FillABookmark("MARK2", "bbbbbbbbbbbbbbbbbbbbbbbbb")
    End Sub
    
    Sub drv2()
        Call FillABookmark("MARK1", "ccccccccccccccccccccccccc")
        Call FillABookmark("MARK2", "ddddddddddddddddddddddddd")
    End Sub
     
    Sub FillABookmark(strBM_Name As String, strBM_Text As String)
        Dim oRange As Word.Range
        Set oRange = ActiveDocument.Bookmarks(strBM_Name).Range
        ActiveDocument.Bookmarks(strBM_Name).Range.Text = strBM_Text
        With oRange
            .Collapse Direction:=wdCollapseEnd
            .MoveEnd Unit:=wdCharacter, Count:=Len(strBM_Text)
        End With
        ActiveDocument.Bookmarks.Add strBM_Name, Range:=oRange
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    Use 'Range' bookmarks:

    Sub M_snb()
       With ActiveDocument
           .Bookmarks.Add "snb", .Paragraphs(1).Range
           .Range(.Bookmarks("snb").Start, .Bookmarks("snb").End - 1) = "example"
        End With
    End Sub

  4. #4
    Hi,

    Thanks for the help. I took your advice and with some research I finally managed to get to the code that I needed. There is one small thing that I still need to alter.

    The value that populates the bookmark is set as currency in excel. Whenever it is transferred to Word, it has spaces between the R$ and the value. For example, R$ 7.
    How do I format the bookmark so that it will appear as brazilian currency without the spaces and in bold?

    Sub CartaModelo()
    Dim wApp As Word.Application
    Dim wDoc As Word.Document
    Dim bmrange As Word.Range
    
    
    n = Cells(1, 2).End(xlDown).Row
    
    
    Set wApp = CreateObject("Word.Application")
    With wApp
    .Visible = True
    .Documents.Open ("C:\Users\3177475\Documents\PIS e Cofins\Carta Modelo\Carta Modelo v4.docx")
    
    
    Set wDoc = wApp.Documents.Open("C:\Users\3177475\Documents\PIS e Cofins\Carta Modelo\Carta Modelo v4.docx")
    Set bmrange = ActiveDocument.Bookmarks("Valor").Range
    
    
    For i = 2 To n
    bmrange.Text = Cells(i, 2).Text
    
    
    With wDoc
    ChangeFileOpenDirectory "C:\Users\3177475\Documents\PIS e Cofins\Cartas"
    .SaveAs2 Filename:=Cells(i, 1).Value, _
        FileFormat:=wdFormatXMLDocument, AddtoRecentFiles:=False
    End With
    
    
    
    
    ActiveDocument.Bookmarks.Add "Valor", bmrange
    Next i
    
    
    End With
    End Sub
    Thanks,
    Pamela

  5. #5
    Nevermind about the last inquiry. Managed to find a solution.

    Thanks for the help!

Posting Permissions

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