Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Other Office > Word VBA
|
Word VBA Discuss using VBA to program Word.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Word VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 31st, 2011, 10:22 AM
Registered User
 
Join Date: Aug 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to webmeister
Default Replacing a range of text and keep formatting

I have a document that I am trying to print multiple times and print out a serial number on each copy via a Macro

in the document I have the SerialNum1 in the location where the serial number should be and formatted as it should look

It was suggested that I use to replace SerialNum1 with the current counter in the variable sn.

Code:
.Range.FormattedText = Replace(.Range.FormattedText, "SerialNum1", Format(sn, "000"))
When I run the macro, I am getting a Compile Error: type mismatch that highlights the Replace() part of .Range.FormattedText = Replace(.Range.FormattedText, "SerialNum1", Format(sn, "000"))

However, if I use
Code:
.Range.Text = Replace(.Range.Text, "SerialNum1", Format(sn, "000"))
it works however my entire document looses all of its formating and graphics.

Is there a way to make this work without the loss of my formating?
 
Old February 5th, 2011, 04:01 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi

You need to type over that. Try using Characters collection of the range and place the text

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Replacing Text in NTEXT field tcarnahan SQL Server 2000 0 January 22nd, 2010 05:51 PM
Replacing text in text box inside table Larry Landis Word VBA 0 December 27th, 2007 12:03 PM
Replacing abbreviation with proper text Brendan Bartley Access 2 June 2nd, 2006 08:03 AM
Replacing Text with a String in a Form newgeek Beginning VB 6 2 August 14th, 2003 12:37 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.