Log in

View Full Version : [SOLVED:] Populating Word Bookmarks without deleting it



pamcheng24
10-17-2017, 05:43 AM
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

Paul_Hossler
10-17-2017, 07:01 AM
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

snb
10-17-2017, 07:33 AM
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

pamcheng24
10-17-2017, 10:48 AM
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

pamcheng24
10-17-2017, 11:20 AM
Nevermind about the last inquiry. Managed to find a solution.

Thanks for the help!