PDA

View Full Version : Delete Selection in word from Excel



fadib
07-04-2011, 09:14 AM
Hi guys,
I am trying to delete a selected bookmarks in word from excel.
For example one my bookmarks is a table. the code is able to select the table when I call the bookmark, but for some reason, "Selection.delete" is not doing what it is supposed to, I thought I had it right. I tried even Selection.Typebackspace
Any suggestions?

Private Sub CommandButton1_Click()
Dim wrdApp As Variant
Dim wrdDoc As Variant
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open("Document Path" & "Form" & ".doc")
With wrdDoc
.Bookmarks("Table1").Select
Selection.Delete
End With
wrdDoc.SaveAs ("Document Path" & "Form" & ".doc")

wrdDoc.Close ' close the document
wrdApp.Quit ' close the Word application
Set wrdDoc = Nothing
Set wrdApp = Nothing
End Sub

Frosty
07-04-2011, 11:03 AM
Does Selection.Range.Delete also fail?

fadib
07-04-2011, 12:46 PM
Yes it does fail.

Frosty
07-04-2011, 01:15 PM
It's tough to give quick solutions when I'm not a fan of ever using the selection object and there could be a lot more to this process than what you're currently showing.

You can try:
Selection.Cut

Selection.Tables(1).Delete

Both of those should work in place of the Selection.Delete you want to do (which, when the selection includes *exactly* a table serves as more of a "clear" than a "delete"). If your bookmark was slightly bigger (i.e., you included a paragraph above or below), you could stick with the Selection.Delete method.

.Cut is probably safer (and more accurate), although you may not want to mess with the clipboard object, and depending on the size of the table you cut, you may get a warning when exiting Word about "large amount of information placed in the clipboard"

.Tables(1).Delete problem is more obvious. If your "table1" bookmark contains more than a table (or doesn't exist), you might be deleting stuff you didn't want to delete (depending on how you have your error trapping).

Don't want to solve a nail with a sledgehammer, but need more info to give a more complete answer. I'd suggest looking up "Working with Ranges" in vba help, and try to get away from using the selection object (which is, 99.9% of the time, a hold-over from a recorded macro).

Frosty
07-04-2011, 01:37 PM
Also, check out the .Information object...
Something like:

With Selection.Range
If .Characters.First.Information(wdWithInTable) = True and _
.Characters.First.Previous.Information(wdWithInTable) = False then
If .Characters.Last.Information(wdWithInTable) = True and _
.Characters.Last.Next.Information(wdWithInTable) = False then
If .Tables.Count = 1 Then
.Tables(1).Delete
End If
End If
End If
End With
It's ugly, but using the selection object often is. The above would basically test to make sure the selection is selecting exactly (and only) a table before deleting. It will cause an error if your table is at the very beginning of your document (a table can't be at the very end of your document, there is always the trailing paragraph mark). I made it deliberately complicated so you could see a couple of the various conditions you might want to test for (and perhaps you'll think of others which would be helpful/safe for you).

Just opening a document and attempting to delete everything contained in a generic bookmark named "Table1" seems relatively hazardous to me, especially with no error trapping regarding the existence (or lack) of the bookmark, nothing to see if the "Table1" bookmark contains what you think it would contain (bookmarks are easily modified/moved/expanded by the simplest user interactions like Copy, Paste and just typing).

At least you're doing a Save As. But I assume, since you've coded this process, you have to do this on a lot of documents (a safer methodology would be to program the open/finding/selecting of the table to allow yourself a human being performing a "sanity check" before deletion, and then programming the SaveAs/Close as a separate process).

But assuming these are very standard documents with no issues (and all my blah blah aside), the .Cut method stated above will probably be the easiest quick & dirty solution.

macropod
07-04-2011, 11:32 PM
Hi fadib,

You have to be clear about whether you want to delete the bookmark, or the range it refers to! To delete the bookmarked range, try:
Private Sub CommandButton1_Click()
Dim wrdApp As Variant, wrdDoc As Variant
Set wrdApp = CreateObject("Word.Application")
wrdApp.Visible = True
Set wrdDoc = wrdApp.Documents.Open("Document Path" & "Form" & ".doc")
With wrdDoc
.Bookmarks("Table1").Range.Delete
.SaveAs ("Document Path" & "Form" & ".doc")
.Close ' close the document
End With
wrdApp.Quit ' close the Word application
Set wrdDoc = Nothing: Set wrdApp = Nothing
End Sub
To delete just the bookmark, change:
.Bookmarks("Table1").Range.Delete
to:
.Bookmarks("Table1").Delete

fadib
07-04-2011, 11:45 PM
Frosty,
Your line of thoughts takes into account important scenarios, such as a user modifying the bookmark.
In my case, I am the only one using the documents.
I was too specific in describing my problem.
Regardless of what is the content of a bookmark, would it be a table, a picture or text.... I want to delete the content of the selected bookmark.
I tried selection.cut, .delete, w/ and w/o range ..... still not working.
But I noticed that when I write my macro in word it works perfectly, but when I go through excel it is not. Can't find the missing link.
Sorry for the hassle, your thoughts are of a good reference to the way the code should be when I have user interaction.

fadib
07-04-2011, 11:53 PM
macropod,
Thank you it worked.
.Bookmarks("Table1").Range.Delete
This was a bit confusing, I guess I was trapped by the macro recording when it does a selection (Like Frosty said) and modifying it to fit my purpose (Where we had to bypass the selection and just select the range of the object).
But out of curiosity why it wasn't working?

macropod
07-05-2011, 12:42 AM
But out of curiosity why it wasn't working?
Because your code was trying to select the bookmark (not the bookmarked range).

Frosty
07-05-2011, 08:04 AM
Completely skipped over the fact that you were calling from a different application. Whoops.

I'm pretty sure the reason it failed is because there is a Selection object in Excel as well as Word. So when you call from Excel and don't specify that you wanted the Word selection (for your code, wrdApp.Selection), your code chose to reference the Excel.Selection (since you didn't instruct it otherwise).

wrdApp.Selection.Cut would probably have worked (as well as all the others).

Regardless, it's a good lesson to get out of the selection object entirely, and really deal with what you actually want to deal with-- not what the left-over recorded macro stuff gave you.

As an additional note, don't use Variants when you want Objects. Most of the time VBA will save you from yourself, but if you're going to declare something, get as close as you can. A variant is a bit of a catch-all (more so than Object), but if you know you're using an object (i.e., GetObject/CreateObject), it's a better habit to:
Dim wrdApp As Object

fadib
07-06-2011, 02:46 AM
Frosty,
Thanks a lot for the additional info/notes, it is helping look more int the nuance between things.
I will change the declaration from Variant to Object, it makes sense.
Cheers guys!