Delete text after bookmark in Word from Excel VBA

Tomaas

New Member
Joined
Nov 12, 2011
Messages
3
Hi,

I'm trying to generate a lot of word documents from a list in excel. The macro copies a predefined template and insert parameters from the excel list into bookmarks in the template and saves the template with a different name. So far so good, but when I loop through this routine the problem is that the parameters just add up after the bookmarks. In other words, how can i delete the text under the bookmark in the word doc before inserting a new parameter and saving? Or is there another solution?

PS: I'm new to vba...

--------------------------------------------------------------------------
mal = MyPath & "\Frontpages\"
MkDir (mal)

'Copies the template to new folder
FileCopy "M:\#work\#LCI\SMIR&Forside\MALFOR~1.DOC", mal & "mal.doc"

'Open the template
Set wdApp = New Word.Application
Set myDoc = wdApp.Documents.Open(mal & "mal.doc")
'Loop
For iRow = 8 To iMaxRow

With Worksheets("Side 1").Cells(iRow, 1)

'See if the celle is empty
If .Value = "" Then
Exit For
Else

Docname = Worksheets("Side 1").Cells(2, 2).Value
DocNr = Worksheets("Side 1").Cells(iRow, 1).Value
DocPO = Worksheets("Side 1").Cells(2, 5).Value
DocTag = Worksheets("Side 1").Cells(iRow, 3).Value
DocTitle = Worksheets("Side 1").Cells(iRow, 4).Value

With myDoc.Bookmarks
.Item("DocName").Range.InsertAfter Docname
.Item("DocNr").Range.InsertAfter DocNr
.Item("DocPO").Range.InsertAfter DocPO
.Item("DocTag").Range.InsertAfter DocTag
.Item("DocTitle").Range.InsertAfter DocTitle

myDoc.SaveAs (mal & DocNr)


End With
End If
End With

Next iRow
myDoc.Close
Set wdApp = Nothing
Set myDoc = Nothing
Kill (mal & "mal.doc")
--------------------------------------------------------------------------

I'm grateful for tips/help.
Cheers:)
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Welcome to the forum,

Have you looked at recording a macro in word and using the goto command to find one of your bookmarks then look to delete the line of text or what every it is, then stop recording look at the code and adapt it into your sub routine to run from your Workbook. That should work for you
 
Upvote 0
Thanks for the answer.
As I'm new to vba, I'm having some problems with adapting recorded codes in word to fit in my code.

I was hoping there was an easy solution like:
.Item("DocName").Range.Delete Text after bookmark then insert new text.

Cheers
 
Upvote 0
What is the "text after bookmark"?
Is it the remainder of the document?
Is it one word?
Is it an entire paragraph, or two paragraphs?
Can't rightly tell you how to delete it if we don't know what it is. :)
 
Upvote 0
Hi Anne,

The "text after bookmark" is a string which can contain both numbers and letters and can be longer than one word but not longer than one line. Maybe I can use some kind of "Ctrl + move cursor down and delete" but i think that my main problem is that I'm new to vba.
 
Upvote 0
What did you record in the Word application?

How did you get the first set of code if you don't know VBA ! Did someone write it for you, or something else.

If you recorded the macro in Word like I suggested it would give you the code and then you place it in your original code with a WITH statement which you already have so a fullstop and the code would work no porblems......
 
Upvote 0

Forum statistics

Threads
1,216,153
Messages
6,129,172
Members
449,490
Latest member
TheSliink

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