View Full Version : Importing Text from Access into Word Document Bookmark I am unable to change the font
kamah00
07-17-2014, 11:31 AM
I am opening a word document and then I want to write text to a Bookmark and I would like to change the font size but that part is not working....
Can anyone help with this?
Here is my code:
Option Compare Database
Option Explicit
Public Sub Bookmark()
Dim myapp As Word.Application
Dim mydoc As Word.Document
Dim wordTemplate As String
'-------------------OPEN DOCUMENT--------------------------------------------------------------------------
Set myapp = New Word.Application
wordTemplate = "test.doc"
Set mydoc = myapp.Documents.Open(wordTemplate)
myapp.Visible = True
With mydoc
Name = "Jeffers"
.Bookmarks("BM").Range.Text = Name
.Bookmarks("BM").Range.Font.Size = 6
.Bookmarks("BM2").Range.Font.Name = "Times New Roman"
End With
End Sub
SoftwareMatt
07-22-2014, 08:18 AM
Can't you just set the text that you want in a word template?
Changing the text deletes the bookmark, so you can't set a new reference to it.
You need to create one reference and use that for all changes.
With mydoc.Bookmarks("BM").Range
.Text = Name
.Font.Size = 6
End With
mydoc.Bookmarks("BM2").Range.Font.Name = "Times New Roman"
kamah00
07-23-2014, 11:15 AM
okay that worked. Thanks so much.
kamah00
07-23-2014, 11:25 AM
Hi jonh,
Say I want to move a string into the bookmark can I alternate between BOLD and non Bold content? If so , how would do that with this string
DataOutput = "NAME:" + vbtab + Name
I want "NAME:" to be bold
and the variable Name to be non Bold
With mydoc.Bookmarks("BM").Range
.Text = "NAME:" + vbTab + name
.Font.Size = 6
mydoc.Range(.Start, .Start + Len("NAME:")).Bold = True
End With
kamah00
07-24-2014, 12:08 PM
hi Jonh,
Thanks for all the help so far....
DataOutput = DataOutput + vbTab + "NAME: " + vbTab + Name + vbCrLf + _
vbTab + "HOME ADDRESS: " + vbTab + HAddress + vbCrLf + _
vbTab + vbTab + Haddress2 + vbCrLf + _
vbTab + "HOME TELEPHONE: " + vbTab + HPhone + vbCrLf + _
vbTab + "SSN: " + vbTab + SSN + vbCrLf + _
vbTab + "DOB: " + vbTab + DOB + vbCrLf + _
vbTab + "OLN: " + vbTab + OLN + vbCrLf + _
vbTab + "PHYSICAL: " + vbTab + Physical + vbCrLf + _
vbTab + "WORK ADDRESS: " + vbTab + Waddress1 + vbCrLf + _
vbTab + vbTab + Waddress2 + vbCrLf + _
vbTab + "WORK PHONE: " + vbTab + WPhone + vbCrLf + _
vbTab + "POSITION: " + vbTab + Position + vbCrLf + _
vbTab + "CONFIDENTIALITY: " + vbTab + Confidentiality + vbCrLf + _
vbTab + "CRIMINAL RECORD: " + vbTab + Criminal + vbCrLf + vbCrLf
Is there any easy way to BOLD everything in quotes in this string as I move it into a bookmark???
If you're trying to create a table format, why not use a table?
I don't know much about word, by the way. There could be much better ways of doing this type of thing.
Sub test()
Dim mydoc As Document
Set mydoc = ActiveDocument
Dim t As Table
Set t = mydoc.Tables.Add(mydoc.Bookmarks("BM").Range, 1, 2)
addrow t, "NAME:", name
addrow t, "HOME ADDRESS:", HAddress & vbCrLf & Haddress2
addrow t, "HOME TELEPHONE:", HPhone
addrow t, "SSN:", SSN
addrow t, "DOB:", DOB
addrow t, "OLN:", OLN
addrow t, "PHYSICAL:", Physical
addrow t, "WORK ADDRESS:", Waddress1 & vbCrLf & Waddress2
addrow t, "WORK PHONE:", WPhone
addrow t, "POSITION:", Position
addrow t, "CONFIDENTIALITY:", Confidentiality
addrow t, "CRIMINAL RECORD:", Criminal
t.Rows(1).Delete
formattable t
set t = nothing: set mydoc= nothing
End Sub
Sub addrow(t As Table, ttl As String, value As String)
Dim r As Row
Set r = t.Rows.Add
r.Cells(1).Range.Text = ttl
r.Cells(1).Range.Bold = True
r.Cells(2).Range.Text = value
Set r = Nothing
End Sub
Sub formattable(t As Table)
With t.Range.Font
.name = "Arial"
.Size = 6
End With
t.Columns(1).Width = 100
t.Columns(1).Shading.BackgroundPatternColor = wdColorGray125
End Sub
kamah00
07-29-2014, 07:53 AM
Thanks so much. I got it to work.
Powered by vBulletin® Version 4.2.5 Copyright © 2025 vBulletin Solutions Inc. All rights reserved.