Error using built-in dialog box "xldialogApplyNames" - vba

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

Related

Set SmartArt text when slideshow is active

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

How to check if clipboard is empty of text?

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

Excel Register UDF in Personal.xslb

I have an UDF named IP_Transpose, which is located under a standard module in Personal.xslb (so that every Excel workbook has access to it).
What I wanted to do is to register this function, so that it is accessible when user presses '=' key and it shows proper description just like any other Excel function (gives you hint when entering arguments).
Here is how I normally would register a function:
Public Sub RegisterFunction()
Dim vArg(1 To 2) As Variant
vArg(1) = "argument description 1"
vArg(1) = "argument description 2"
Application.MacroOptions Macro:="IP_Transpose", Description:="Some overall description", Category:="IP_UDF", ArgumentDescriptions:=vArg
End Sub
The problem is that not only that this does not work (uness I change MacroOptions Macro:="IP_Transpose" to MacroOptions Macro:="Personal.xslb!IP_Transpose"), but also when I start typing '=IP_Tra....' I cannot see it under function list.
How can to solve this issue? (I don't want to call my function as ='Personal.xslb'!IP_Transpose, but directly typing =IP_Transpose(...).
Thanks!

"Sub or Function not defined" when trying to run a VBA script in Outlook

As a first step in creating a VBA script to resize a currently selected image to 100% x 100%, I'm trying to reproduce the example in http://msdn.microsoft.com/en-us/library/ee814736(v=office.14).aspx. The macro is very simple:
Sub Test()
MsgBox ("Hello world")
End Sub
The VBA script was simply created in "Project1" which opens by default when one presses Alt+F11. However, I keep getting the error "Sub or Function not defined" when trying to run the VBA script (Figures 1 and 2).
How can I make the VBA script 'accessible' to Outlook?
Figure 1 Running the "Test" macro in Microsoft Outlook
Figure 2 "Sub or Function not defined" error, with module tree in the background
I solved the problem by following the instructions on msdn.microsoft.com more closely. There, it is stated that one must create the new macro by selecting Developer -> Macros, typing a new macro name, and clicking "Create". Creating the macro in this way, I was able to run it (see message box below).
I had a similar situation with this issue. In this case it would have looked like this
Sub Test()
MsqBox ("Hello world")
End Sub
The problem was, that I had a lot more code there and couldn't recognize, that there was a misspelling in "MsqBox" (q instead of g) and therefore I had an error, it was really misleading, but since you can get on this error like that, maybe someone else will notice that it was cause by a misspelling like this...
This error “Sub or Function not defined”, will come every time when there is some compile error in script, so please check syntax again of your script.
I guess that is why when you used msqbox instead of msgbox it throws the error.
This probably does not answer your question, but I had the same question and it answered mine.
I changed Private Function to Public Function and it worked.
I need to add that, if the Module name and the sub name is the same you have such issue.
Consider change the Module name to mod_Test instead of "Test" which is the same as the sub.
I think you need to update your libraries so that your VBA code works, your using ms outlook

Error on ActiveSelection.Tasks

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.