The How-To Geek Forums Have Migrated to Discourse

How-To Geek Forums / Microsoft Office

How to do formatting using macros in Word 2007

(2 posts)
  • Started 4 years ago by anju16saini
  • Latest reply from moreeg
  • Topic Viewed 680 times

Posts: 1


I need to generate a document wherein corresponding values will be filled as per the database.
o I am using bookmarks for the same ( shown below) . Also I am using macros for the same ( Shown below ) .
The problem I am facing are:

1. Where there is no value for the particular field , my bookmark name is appearing ; whereas it
should be blank.
2. As per the example , I have 2 values for a particular field . and my value is appearing in a single line with @@

My Doc ::

Name : [Name]
Phone(Optional) : [Phone]
Country :[Country]

These are the three fields. I have used the bookmarks. Since my Phone column is empty sometime , it
should show me as a blank but , it is showing the bookmark.

Similary, Country field has two values say : India and America. The value should appear as

Country : India

Whereas my values are coming like India@@America

Please help me

My Macro ::

Public Sub DefaultMacro()
Dim lngCpt As Long
Dim strBookmarkName As String
Dim strBookmarkValue As String

FormatTable "Name", "@@", 1
FormatTable "Phone", "@@", 1
FormatTable "Country", "@@", 1

'Clear unused bookmarks

End Sub

Private Sub RemoveUnusedBookmark()
Dim lngCpt As Long
Dim strBookmarkName As String
Dim strBookmarkValue As String

For lngCpt = 1 To ActiveDocument.Bookmarks.Count
strBookmarkName = ActiveDocument.Bookmarks(lngCpt).Name
Selection.GoTo What:=wdGoToBookmark, Name:=strBookmarkName
strBookmarkValue = Selection.Text
If strBookmarkName = strBookmarkValue Then SetBookMark strBookmarkName, ""
Next lngCpt

End Sub

Public Sub SetBookMark(ByVal pstrBookmark As String, ByVal pstrValue As String, Optional ByRef pwrdActiveDocument As Document, Optional ByVal pblnKeepBookmark As Boolean = True)
Dim wrdRange As Range

On Error GoTo ActivateError
If pwrdActiveDocument Is Nothing Then
Set pwrdActiveDocument = ActiveDocument
End If
If Not pwrdActiveDocument.Bookmarks.Exists(pstrBookmark) Then Exit Sub
Set wrdRange = pwrdActiveDocument.Bookmarks(pstrBookmark).Range
wrdRange.Text = pstrValue
If pblnKeepBookmark Then wrdRange.Bookmarks.Add pstrBookmark
On Error GoTo 0
End Sub

Private Sub FormatTable(ByVal pstrBookmark As String, ByVal pstrSeparator As String, ByVal pintNbrCol As Integer)
Dim strTemp As String
Dim strTemp2 As String
Dim intCpt As Integer

Selection.GoTo What:=wdGoToBookmark, Name:=pstrBookmark
strTemp = Selection.Text

If InStr(strTemp, pstrSeparator) = 0 Then Exit Sub
While InStr(strTemp, pstrSeparator) > 0
strTemp2 = Extraire_(strTemp, pstrSeparator)
Selection.Text = strTemp2
For intCpt = 1 To pintNbrCol
Selection.MoveRight Unit:=wdCell
Next intCpt
If Len(strTemp) > 0 Then Selection.Text = strTemp
End Sub

Private Function Extraire_(ByRef value As String, Optional ByVal Separator As String = ",") As String
If InStr(value, Separator) = 0 Then
Extraire_ = value
value = ""
Extraire_ = Mid$(value, 1, InStr(value, Separator) - 1)
value = Mid$(value, InStr(value, Separator) + Len(Separator))
End If
End Function

Posted 4 years ago
Posts: 842


I could see nothing obvious that was wrong with the formatting. I suspect it has something to do with the way data is being read from the "database". Not having access to your database nor having a sample of your document I cannot help with diagnosing the problem. Here is a short tutorial on debugging macros. If you follow it you will find exactly where the "formatting" goes wrong and then you will be better able to make any necessary corrections.

Posted 4 years ago

Topic Closed

This topic has been closed to new replies.