VBA code works in debugger but breaks when playing

btb331

New Member
Joined
May 29, 2012
Messages
2
Hey guys,

This is a sub in word that I call from in Excel. It works perfectly when i step through it but when I run it throws an error when doing the pasteandformat command. Any help would be much appreciated! :)

Code : Sub titles()
For i = 2 To 10
ActiveDocument.Bookmarks(i).Select
Selection.MoveDown Unit:=wdLine, Count:=12
Selection.MoveDown Unit:=wdParagraph, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveUp Unit:=wdParagraph, Count:=1, Extend:=wdExtend
Selection.Copy
Selection.MoveUp Unit:=wdLine, Count:=13
Selection.PasteAndFormat (wdFormatPlainText)
Next i
ActiveDocument.Bookmarks(1).Select
Selection.TypeText (Date)
End Sub

Error
Run-time error '4198':
Command failed
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Try using the value of the constant. Sometimes Excel can't tell which Selection you mean. Excel's or Word's.

Code:
Selection.PasteAndFormat 22 '(wdFormatPlainText)

Might I ask what all this selection stuff is doing? We might be able to streamline it using Word's Range instead. Is the paragraph you're copying within a bookmark also?
 
Upvote 0
Thanks for the reply

Afraid that didn't work...

The selection stuff is to find a title in a row of a table. Had to do it this way otherwise selects the whole row rather than the words...

i changed it to selection.pastespecial datartpe:=wdpastetext

and got

run time error '4605'
This method or property is not available because the clipboard is empty or not valid'
 
Upvote 0

Forum statistics

Threads
1,217,358
Messages
6,136,091
Members
449,991
Latest member
IslandofBDA

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