How to access the previous clipboard in VBA Word? - vba

I am writing macro where I need to paste clipboard into MS Word. When I have run my code it happens error 4605 and debugger stops on line #11.
On Error GoTo ErrHandler
Documents.Add.Content.Paste
If False Then
ErrHandler:
' Add a delay
Dim tmpStart
tmpStart = Timer
Do
DoEvents
Loop While (tmpStart + 1) > Timer
Documents.Add.Content.Paste
End If
When I check the clipboard panel in MS Word I see this:
The last item caused problem. But what I need to access is the item marked in red rectangle. So in the case that the error happens I need to get the previous clipboard in the Word's Memory. Can you tell me how to achieve it?
Edit:
So far I have found this question, which is concerned about the problem.

Related

Adding a text clock to PowerPoint using VBA

I'm trying to add a clock to a PowerPoint slide, and it has to show the current time, which means it has to update once every second. This is my current macro:
Sub OnSlideShowPageChange()
Dim i As Integer
i = ActivePresentation.SlideShowWindow.View.CurrentShowPosition
MsgBox "Macro still working!"
'With ActivePresentation.Slides(i).Shapes("Date Placeholder 8")
' Do
' Sleep 1000
' .TextFrame.TextRange.InsertDateTime ppDateTimehmmssAMPM
' DoEvents
' Loop
'End With
End Sub
This shows a MsgBox with the text "Macro still working!" as expected. However, if I remove the apostrophes, thus (supposedly) allowing the update code to run, nothing happens, and I don't see a MsgBox. Can anyone help?
I've tried using the AutoDateTime add-in, and it works, except there are animations that need to run on my slide and AutoDateTime keeps interrupting those animations. I've also tried TM Timer and it has the same problem.

How to prevent VBA macro from stopping when opening other workbooks with runtime errors?

My macro goes through a range and opens all hyperlinks in it, and looks like this:
For Each xhyperlink In WorkRng.Hyperlinks
xhyperlink.Follow
Next
Now, some of the hyperlinks are excel files. In some rare cases, one of the Excel files being opened contains some runtime error inside it - and this error jumps when file is opened, and stops my macro from running. How can I prevent such things from happening? I tried using both
"On error resume next" and "on error goto X" but that won't stop the message from appearing.
Any ideas how to solve this? Thanks a million!
At the beginning of your macro add:
Application.DisplayAlerts = False
Then at the end add
Application.DisplayAlerts = True

VBA Compile Error 'Wend Without While'

I have a VBA macro in excel that has run fine for a while. I've been making changes with these loops already working and suddenly the code is not compiling:
Compile error: Wend without While
I have tried changing to Do While...Loop loops, but I got the equivalent
Compile error: Loop without Do
Here is the structure of my code:
'This loop is fine
While '(code for evaluation)
'code to run in loop
Wend
While '(more eval code)
'code
While '(eval code)
'code
Wend '<--This is where the compile error occurs
'code
While '(eval code)
'code
Wend
'code
Wend
Does anyone have any idea what could be the issue? (Bonus: Does the indentation of code actually matter?)
This message Wend without While usually indicates an un-terminated IF
Somewhere there may be an IF without the matching End If
(This kind of error message is a bit annoying since it hides the true error)
The real problem is that you have a method that does too many things.
You can locate the mismatched code block, and fix it - but you'd be dismissing a readability issue with an "hey it works, leave it alone" excuse.
While
'code to run in loop
Wend
' **** move to another method
While
'code
' **** move to another method
While
'code
Wend
' **** move to another method
'code
While
'code
Wend
'code
Wend
Extracting methods in VBA code can be a bit of a pain, because parameters and return values must be determined, and then the extraction point needs to be turned into a method/function call. Fortunately, there's a tool for that (disclaimer: I wrote it).

Simple VBA selection code error

I have this simple VBA code with which I want to change the background of the selected cells. Somehow the command Selection. that I learned before doesn't work. Could you help me with this code? I know the answer is probably stupid, but I can't seem to figure it out.
Sub set_background_color()
'Add background color to selected cells
Selection.Interior.Color = RGB(255, 0, 0)
End Sub
Thanks
EDIT: Sorry for the vague question, it's my first question on stack overflow so I didn't think of the importance of the type of error. It gives me the error "Compile error: Expected function or variable".
It is attached to a button, but even if I run it as a macro without button it gives me the same error.
EDIT 2: I'm running Excel 2011 on a Mac, until now it never gave me any compatibility issues in VBA. However this does not seem to work.
The problem is not within your code, it is within your cells.
Check the locked status of the cells and the protection status of the worksheet.
If your worksheet is not protected then try this
Sub set_background_color()
Dim r As Range
On Error Resume Next
Set r = Selection
On Error GoTo 0
If Not r Is Nothing Then
r.Interior.ColorIndex = 3
Else
MsgBox "Invalid Selection"
End If
End Sub

why Excel session has to be killed from Windows Task Manager on VBA error

I have a excel book with VBA. When an error is triggered, I try to close the entire Excel app. But I find the Excel session is still running in Windows Task Manager. So I have to kill the session before I can properly reboot the app and run the VBA program.
How can I handle errors so that even when there is an error I still can run the VBA program without killing and rebooting Excel itself?
At the top of your sub/function write
'ErrHandler is a label we put at the bottom of our code section.
On Error Goto ErrHandler
and at the bottom of the function/sub
Exit Function
ErrHandler:
'Do something here if there is an error
'For Example
Msgbox(Err.Description)
End Function
or
Exit Sub
ErrHandler:
'Do something here if there is an error
'For Example
Msgbox(Err.Description)
End Sub
Note that you have to put the Exit statement or you will enter the code block at the end of the execution
The err object is used to get information about the error in the error section of the sub/function. You can use this to do different things based on the type of error.
eg.
Select Case err.Number
Case 13
Msgbox("Type Mismatch, macro will continue")
'Go back to the point of the error and resume code execution..
Resume Next
Case Else
Msgbox("A fatal error has occurred. Macro will end " & err.Description)
End Select
Nice reference for catching specific error codes.
http://www.mcoffice.com/faq/edi/errors.html
if you have given the user control over the application with the following lines
xlApp.Visible = True
xlApp.UserControl = True
it will not go away, else Ending the sub or function should release the errant process