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

Joined
Aug 29, 2016
Messages
3
Reaction score
0
Hello,

I am using an excel worksheet and using VBA to access a .dot template. Both files have the bookmarks, data is transfered correctly. I am using placeholder bookmarks, the one that looks like a beam I.

My only problem is how to get the correct format in the Word document.

Example: The bookmark in excel shows 0.71 however the bookmark in word is displaying 0.712669683257918.

How can I get the bookmark to display only 2 decimals in word or exactly display the same value and format as the bookmark in excel?

Thanks

Yomero
 

macropod

Microsoft MVP
Joined
Mar 2, 2012
Messages
580
Reaction score
50
That's probably because you're reading the cell's value property instead of it's text property.
 
Joined
Aug 29, 2016
Messages
3
Reaction score
0
Thanks for your reply,

Below is the code in the macro for reading and sending the value in the bookmark.

dottempplate.Bookmarks(xlName.Name).Range.Text = Range(xlName.Value)
 

macropod

Microsoft MVP
Joined
Mar 2, 2012
Messages
580
Reaction score
50
As I said, you should be reading the cell's text property instead of it's value property. Thus, instead of:
dottempplate.Bookmarks(xlName.Name).Range.Text = Range(xlName.Value)
you should be using:
dottempplate.Bookmarks(xlName.Name).Range.Text = Range(xlName.Text)
 
Joined
Aug 29, 2016
Messages
3
Reaction score
0
Thanks!..
The following changes, worked perfect (Thanks to gmaxey).
dottempplate.Bookmarks(xlName.Name).Range.Text = Format(Range(xlName.Value), "#.00")

Case closed....
 

macropod

Microsoft MVP
Joined
Mar 2, 2012
Messages
580
Reaction score
50
The solution provided by gmaxey will only work with numbers that are to be output with 2 decimal places. It will delete any currency symbols and thousands separators. It is also inappropriate for text. The solution I posted preserves currency symbols, all the decimal places your numbers display, plus thousands separators and is also appropriate for text.

It is also evident you've posted about this in another forum. For cross-posting etiquette, please read: http://www.excelguru.ca/content.php?184
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top