VBA code in Excel opens Word template and fills bookmarks, but can't get percent formatting right!

Gordo24

New Member
Joined
Apr 19, 2011
Messages
43
Excel is my source document which contains my data. Word is my template with my bookmarks. I have created a macro in Excel which opens my template and populates my Word bookmarks. The problem I am having is my data in Excel is a percentage (out to 2 decimals) and when the bookmarks in Word are filled, they appear as .6556 or .55. I need them to appear as 65.56% or 55.00%. Here is my code:

Sub createWordReport()
On Error GoTo errorHandler
Dim wdApp As Word.Application
Dim myDoc As Word.Document
Dim mywdRange As Word.Range
Dim CatD As Excel.Range
Dim CatB As Excel.Range

Set wdApp = New Word.Application
With wdApp
.Visible = True
.WindowState = wdWindowStateMaximize
End With
Set myDoc = wdApp.Documents.Add(Template:="C:\Documents\Test.doc")
Set CatD = Sheets("Sheet1").Range("A7")
Set CatB = Sheets("Sheet1").Range("A6")

With myDoc.Bookmarks
.Item("CatD").Range.InsertAfter CatD
.Item("CatB").Range.InsertAfter CatB
End With

errorHandler:
Set wdApp = Nothing
Set myDoc = Nothing
Set mywdRange = Nothing
End Sub

I was able to create a workaround that works in the .6556 scenario above by doing the following code below and simply adding a % sign after where the bookmark is populated.

Item("CatD").Range.InsertAfter CatD * 100

The problem is this will not work in the 55.00% example as it populates my bookmark as 55%

Basically I need Excel to populate the bookmarks in Word as a percentage out to two decimals. Any help would be gretaly appreciated!
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
If the cells in Excel are formatted as percentages you can try using their Text property.
Code:
With myDoc.Bookmarks
.Item("CatD").Range.InsertAfter CatD.Text
.Item("CatB").Range.InsertAfter CatB.Text
End With
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,727
Members
449,049
Latest member
MiguekHeka

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top