If I try to paste from an empty clipboard, I get an error. I would like to check if the clipboard is empty of text before pasting so that I can avoid this. How can this be accomplished? I'm aware it can be done through error handling, but I would prefer a method that avoids an error.
Edit -- Per request, adding code that creates the error and the error message:
Code that causes the problem:
Sub PasteFromEmptyClipBoard()
Selection.Paste
End Sub
Error message that I get:
"Run-time error '4605' This method or property is not available because the Clipboard is empty or is not valid."
Very important: You must first set a reference to the "Microsoft Forms 2.0 Object Library" (as in the attached screenshot below) before implementing this code. You may find that it is not an option when you scroll through the reference libraries. To make it show up, just add a form to the project (you can always delete the form later).
Sub CheckClipboard()
Dim myDataObject As DataObject
Set myDataObject = New DataObject
myDataObject.GetFromClipboard
If myDataObject.GetFormat(1) = True Then
'''There is text on clipboard, so it's safe to paste
Else
'''there is no text on the clipboard, so you may get error.
End If
End Sub
Related
Using PowerPoint 2016, I am writing a macro that allows the user to input data in a live presentation using UserForms and TextBoxes. This is working nicely, except when i try to display the text in a SmartArt. The following macro illustrates the problem:
Sub writeToSmartArt()
Dim artShape As Shape
Set artShape = ActivePresentation.Slides(maalSlide).Shapes("Diagram")
MsgBox artShape.SmartArt.Nodes(1).Nodes(1).TextFrame2.TextRange.Text
artShape.SmartArt.Nodes(1).Nodes(1).TextFrame2.TextRange.Text = "testing"
End Sub
The sub sets the SmartArt shape to the artShape variable, and first prints out the contents of a specified node in a MsgBox. In the next step, I am setting the text property to a new value. Everything works fine as long as the presentation is not active. I am able to manually run the sub, and everything behaves as expected. However, when the slideshow is running i get the following error message when trying to write to the SmartArt node:
Run-time error '-2147467259 (80004005)':
Method 'Text' of object 'TextRange2' failed
Displaying the current content in the MsgBox still works. How can i overcome this problem?
Not sure why you get an error in slideshow mode. However, a workaround would be to use the node's Shape() method to access the shape directly, instead of the node itself, like so: artShape.SmartArt.Nodes(1).Nodes(1).Shapes(1).TextFrame2.TextRange.Text = "testing". Hope this helps!
I had the same problem, so instead use SmartArt.Nodes(), I am using SmartArt.AllNodes().
Exemple:
ppt_output.Slides(SLIDESMART).Shapes("TheList").SmartArt.AllNodes(1).Shapes.TextFrame2.TextRange.Text
I have an Access form with an attached recordset. Controls in the header adjust the recordset filters. Sometimes those filters return an empty recordset, which is OK.
However when the recordset is empty and the user clicks on any control in the header (let's say to change the filter again), Access pops up an error box with 3021 - No Current Record.
I've been unable to find the source of this error in the code - however I've added an event trigger on the form - onError for the form itself.
Private Sub Form_Error(DataErr As Integer, Response As Integer)
MsgBox "caught error: " & DataErr
End Sub
That works! But I had expected this sub to replace the Access error. Instead my code is executed and the original error message pops up after!
I know there are many questions about getting rid of this error but most are related to taking a VBA action - this is about removing the error when no other VBA is triggered. Is there a way to get the Form_Error sub to not show the popup?
The simple solution is to use the Response parameter in the Form Error event. Setting it to acDataErrContinue tells it to continue and ignore the error.
You can see more examples on the doc page.
I wanted to use the named built-in dialog box in Excel via VBA. I learned to use it like this here.
That is working for many named built-in boxes, but not for ApplyNames. All arguments are optional, so that is not the problem. I also tried to define a boolean variable and say variable = dialogbox so that the box (outcome is a boolean as far as I understood it) has something to write the result in. But that didn't work either.
My code looks like this:
Public Sub Box()
Application.Dialogs(xlDialogApplyNames).Show
End Sub
The error which occurs is:
Laufzeitfehler 1004: Objekt- oder Anwendungsdefinierter Fehler"
or in english (hope I translated it correctly):
"runtime error 1004: error of object or application"
Thank you in advance!
Simon
I just tested and I can reproduce the error if the active workbook does not contain any range names. As soon as you add one the dialog is displayed. So:
Public Sub Box()
If ActiveWorkbook.Names.Count >0 Then
Application.Dialogs(xlDialogApplyNames).Show
End If
End Sub
I'm busy with some word automation and have run into an issue whereby a context menu within a document has items in, that I wish to remove.
Once the document is open, through vba I can remove these items by running the following code;
[VBA]
Dim oContextMenu As CommandBar
Dim oContextMenuItem As CommandBarControl
'Make changes to the ActiceDocument only (this is needed to make any changes to this document).
CustomizationContext = ActiveDocument
For Each oContextMenu In ActiveDocument.CommandBars
If oContextMenu.Type = MsoBarType.msoBarTypePopup Then 'Loop through all the context menus of type (msoBarTypePopup)
For Each oContextMenuItem In oContextMenu.Controls
If (InStr(oContextMenuItem.Caption, "Smokeball")) Then
oContextMenuItem.Delete
End If
Next
End If
Next
If I execute this code and check the document, all contextMenu sub items that contain the text "smokeball" are removed.
When I try move this code to my VB.NET solution (I have no choice of language, so VB it is), I get errors on the CustomizationContext = ActiveDocument line (this line has to be there for it to affect the current document).
The error I get is CustomizationContext' is not a by reference property.
Does anyone know how to get just that ONE line equivalent for vb.net?
Thanks in advance.
EDIT: In case you need to see the vb.net sub:
Private Sub RemoveUnwantedContextMenuItems()
Dim oContextMenu As CommandBar
Dim oContextMenuItem As CommandBarControl
'Make changes to the ActiceDocument only (this is needed to make any changes to this document).
WordApplication.CustomizationContext = WordApplication.ActiveDocument 'This is the error.
For Each oContextMenu In WordApplication.CommandBars
If oContextMenu.Type = MsoBarType.msoBarTypePopup Then 'Loop through all the context menus of type (msoBarTypePopup)
For Each oContextMenuItem In oContextMenu.Controls
If (InStr(oContextMenuItem.Caption, "Smokeball")) Then
oContextMenuItem.Delete()
End If
Next
End If
Next
End Sub
PS - I have also already tried using the .AttachedTemplate as well as .Normal / .NormalTemplate
Jules pointed me in the right direction with his sample code.
After lots of playing around I noticed that somewhere in the solution, the [TYPE] of WordApplication was getting changed to a dynamic type of sorts, hence, it couldn't use CustomizationContext.
My solution was this:
I changed this line;
WordApplication.CustomizationContext = WordApplication.ActiveDocument
To this:
CType(WordApplication, Microsoft.Office.Interop.Word.Application).CustomizationContext = WordApplication.ActiveDocument
Forcing the types to be correct.
Simple solution but took some time.
Thanks to Jules for pointing me in the right direction.
(Points should go to you).
Does anyone know what this means
Set oProjTasks = ActiveSelection.Tasks
I have a macro that generates status reports from MS project and exports them directly into MS Word. It is a slick tool when it works.
When I run it now it throws "runtime error '424': object required" at this point.
How do I fix this?
The code that you are displaying is a set statement, that is setting the object ProjTasks equal to the task that is selected in the message box. The ActiveSelection property returns a selection object that represents the active selection.
It could be that you are experiencing an issue where there are no items selected, in which case it will throw a trappable error code 424. There is a code snippet that you can modify from the MSDN that will work to prevent this type of error from occuring.
Here is the link to the MSDN article... just remember to not use this code verbatim, but modify it to work with your macro.
http://msdn.microsoft.com/en-us/library/aa169315%28v=office.11%29.aspx
You could try just wrapping the error check around the set statement. I've written a small macro on a non-empty project file:
Sub Testing()
On Error GoTo ActiveSelectionErrHandler:
Set oProjTasks = ActiveSelection.Tasks
If oProjTasks Is Nothing Then
MsgBox "No tasks in current project"
End If
ActiveSelectionErrHandler:
Set oProjTasks = ThisProject.Tasks 'or something like that
Resume Next
End Sub
This handles the error but as Steve has already expressed more work is required to integrate the code.
You will have to follow the code to make changes to handle oProjTasks being empty where it is expected to have some values. Otherwise you will see more errors perhaps where the oProjTasks is found to be empty.
Another alternative solution could be to launch the macro after selecting a project as the code you have quoted will work fine if something is selected.