Rewrite word document

K

Ken

Hi,

I usually get a external data file and use VBA to paste
those data in word document. My question is if I have a
new data file, is it possible I can replace the old data
using the new data from new data file? I hope my word
document can have all new data and delete old data in the
same word document. Thanks!

Ken
 
P

Peter Hewett

Hi Ken

Try this, then paste the new data:

ActiveDocument.Select
Selection.delete

HTH + Cheers - Peter
 
G

Guest

Thank you Peter. The original document already have some
format and tables in the document. I can't erase those. I
only want to erase the data I insert last time. And,
replace the new data. Thanks!

Ken
 
P

Peter Hewett

Hi Ken

In that case bookmark the text you are inserting and then always use the
following code. This will always replace whatever you last pasted into the
document using the code. Simply it uses a bookmark to mark the last text
you pasted into your document. It then reuses that bookmark for the next
paste operation.

Public Sub WhatTheHell()
Dim rngInsert As Word.Range

Set rngInsert = ActiveDocument.Bookmarks("BM_1").Range
rngInsert.Paste
ActiveDocument.Bookmarks.Add "BM_1", rngInsert
End Sub

Just add a bookmark to your template ("BM_1" in the above example) and then
select something you want pasted into you document and run the above macro.

HTH + Cheers - Peter
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

What I would do is insert { DOCVARIABLE "varname" } fields in the document
where want each piece of data to appear and then use vba to reset values of
the document variables with the data/newdata and then update the fields in
the document

ActiveDocument.Variables("varname").Value = [data]
--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.
Hope this helps
Doug Robbins - Word MVP
 
P

Peter Hewett

Hi Ken

Just to note, if you do it Doug's way be aware that any formatting or
tables etc. will be lost - if the source you are pasting from is another
word document.

Cheer - Peter

From: "Doug Robbins - Word MVP - DELETE UPPERCASE CHARACTERS FROM EMAIL
ADDRESS" <[email protected]> Subject: Re: Rewrite word
document Newsgroups: microsoft.public.word.vba.general

What I would do is insert { DOCVARIABLE "varname" } fields in the
document where want each piece of data to appear and then use vba to
reset values of the document variables with the data/newdata and then
update the fields in the document

ActiveDocument.Variables("varname").Value = [data]
--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions
forwarded directly to me will only be answered on a paid consulting
basis. Hope this helps
Doug Robbins - Word MVP
Thank you Peter. The original document already have some
format and tables in the document. I can't erase those. I
only want to erase the data I insert last time. And,
replace the new data. Thanks!

Ken
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

I was assuming that the data was in the cells of a table. A charformat
switch can be used to force the formatting of the data.

--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.
Hope this helps
Doug Robbins - Word MVP
Peter Hewett said:
Hi Ken

Just to note, if you do it Doug's way be aware that any formatting or
tables etc. will be lost - if the source you are pasting from is another
word document.

Cheer - Peter

From: "Doug Robbins - Word MVP - DELETE UPPERCASE CHARACTERS FROM EMAIL
ADDRESS" <[email protected]> Subject: Re: Rewrite word
document Newsgroups: microsoft.public.word.vba.general

What I would do is insert { DOCVARIABLE "varname" } fields in the
document where want each piece of data to appear and then use vba to
reset values of the document variables with the data/newdata and then
update the fields in the document

ActiveDocument.Variables("varname").Value = [data]
--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions
forwarded directly to me will only be answered on a paid consulting
basis. Hope this helps
Doug Robbins - Word MVP
Thank you Peter. The original document already have some
format and tables in the document. I can't erase those. I
only want to erase the data I insert last time. And,
replace the new data. Thanks!

Ken
-----Original Message-----
Hi Ken

Try this, then paste the new data:

ActiveDocument.Select
Selection.delete

HTH + Cheers - Peter

[email protected]:

Hi,

I usually get a external data file and use VBA to
paste
those data in word document. My question is if I have
a
new data file, is it possible I can replace the old
data
using the new data from new data file? I hope my word
document can have all new data and delete old data in
the
same word document. Thanks!

Ken


.
 

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