How to set the default printer using VBA in Outlook - vba

In Excel I'm able to set the default printer using for example:
Application.ActivePrinter = "My Printer on Ne03:"
and I can confirm it got set by MsgBox Application.ActivePrinter
The same code doesn't work in Outlook's VBA. I get
Run-time error 438: Object doesn't support this property or method
Update:
I have now referenced the Excel application to get access to its Application object as shown below:
Sub Example()
Dim excelapp as Object
excelapp = CreateObject("Excel.Application")
MsgBox excelapp.ActivePrinter
excelapp.ActivePrinter = "My Printer on Ne03:"
End Sub
I got a
Run-time error 1004 Unable to set the ActivePrinter property of the Application class
for excelapp.ActivePrinter = "My Printer on Ne03:".
I ensured the macro settings in Excel are all enabled and printer name is correct. However MsgBox excelapp.ActivePrinter works fine.

Related

Ignore an "external" error when opening a Word document

I'm attempting to copy the contents of a word document, but the document has embedded vba which causes a compile error anytime I open it.
The error reads
"The code in this project must be updated for use on 64-bit systems."
I expect this error to occur, so how do I tell my vba code in Access to ignore the errors without changing the word doc?
My vba code so far - the DisplayAlerts = wdAlertsNone does not work, neither does setting it to False:
Private Sub cmdQuickLtr_Click()
Dim wApp As Word.Application
Set wApp = CreateObject("Word.Application")
wApp.DisplayAlerts = wdAlertsNone
Dim doc As Object
Set doc = wApp.Documents.Open(Me.tbLetterPath.Value)
doc.Content.Copy
doc.Close
'do something with the copied content
wApp.DisplayAlerts = wdAlertsAll
Set doc = Nothing
Set wApp = Nothing
End Sub
Thank you all in advance.

Run-time error '424' Object Required when doing userform.show on imported userform

I am attempting to import a userform, and show it on initial startup. The userform imports just fine, however, when attempting to show it, I keep getting a Run-time error '424' Object Required.
Here is my code:
Sub Workbook_Open()
Dim wkbTarget As Excel.Workbook
Dim szTargetWorkbook As String
Dim cmpComponents As VBIDE.VBComponents
Application.ScreenUpdating = False
szTargetWorkbook = ActiveWorkbook.Name
Set wkbTarget = Application.Workbooks(szTargetWorkbook)
Set cmpComponents = wkbTarget.VBProject.VBComponents
' IMPORT FORM
cmpComponents.Import "\\myserver.domain\Application\Forms\LOGIN.frm"
LOGIN.Show
End Sub
When I click End on the error, I can then show the userform just fine.
The run-time (thanks #Comintern!) won't like you referring to an object that doesn't exist yet. You could use:
Userforms.add("LOGIN").Show
to avoid that direct reference.

How to Open Embedded Object in Excel with Caption in Vba?

Try to open embedded object in excel file with caption,able to open file with name: can any one help how to open with caption or dynamically
Worksheets(SheetName).Activate
Set o = Worksheets(SheetName).OLEObjects("object 3")
o.Verb xlVerbOpen
MsgBox "Attachmene open"
Note: object will add continuously in excel file, how to find object dynamically to open with caption?
The embedded object in Excel is a Shape. Add two embeded workbooks in your ActiveSheet and try this code:
Public Sub TestMe()
Dim obj As Object
For Each obj In ActiveSheet.Shapes
Debug.Print obj.Application.Caption
Next obj
End Sub
Then try to change the code, with a simple condition, opening the obj, if the caption is the expected one:
If obj.Application.Caption = "someCaption" Then OpenTheWorkbook(obj)
At the end write some check to make sure that it skips some possible errors.
I open an embedded WORD document through Excel using the following verb command.
Set o = .OLEObjects("Object 1")
o.Verb xlVerbOpen
"Object 1" is the default name of the embedded object so that would be changed as needed.

VBA PowerPoint unable to save presentation

I'm trying to pull together a marco that add data and charts from Excel into a powerpoint template and save it in a directory. The macro is being run from Excel.
After running it, the following message appears:
Run-time error '-2147467259 (80004005)':
Presentation (unknown member): PowerPoint was unable to open or save this document. Please ensure that you have access priviliges to read or write the document that is not encrypted.
Below is the VBA code:
Sub CreateMPP()
Dim DestinationTemplate As String
Dim PowerPointApp As PowerPoint.Application
Dim myTemplate As PowerPoint.Presentation
DestinationTemplate = "C:\Users\Me\Documents\MPP_Template.potx"
Set PowerPointApp = CreateObject("PowerPoint.Application")
PowerPointApp.Visible = True
Set myTemplate = PowerPointApp.Presentations.Open(DestinationTemplate, ReadOnly:=msoFalse)
'Other macro that pulls the data and charts, which works perfectly fine
Call CreatePowerPointTest
myTemplate.SaveAs "TestTest", ppSaveAsPresentation
myTemplate.Close
PowerPointApp.Quit
End Sub

Detect if an object has been disconnected from its clients

I am having an issue with automating an Excel file. The VBA script within Excel first opens a Word application and Word document:
Dim wordApp As Object
Set wordApp = CreateObject("Word.Application")
vPath = Application.ActiveWorkbook.Path
Set wordDoc = wordApp.Documents.Open(vPath & "\test.doc")
And then I call a subroutine within the Word document passing some data from the Excel file:
Call wordApp.Run("StartWithData", variable1, variable2)
If Excel detects that an error occurs in that subroutine, I close the Word document and Word application from Excel in a label I call Err1:
On Error Goto Err1
'all the code from above
Exit Sub
Err1:
wordDoc.Close wdCloseWithoutSaving
wordApp.Quit SaveChanges:=wdDoNotSaveChanges
Set wordDoc = Nothing
Set wordApp = Nothing
This works perfectly fine under normal circumstances; however, if the Word document or application are closed before the Err1 label executes (such as the user manually closing the document), I get the following error:
Run-time error '-2147417848 (80010108)':
Automation error The object invoked has disconnected from its clients.
which makes perfect sense because the wordApp and/or wordDoc variables still reference the Application and Document objects and those objects do not exist anymore (yet are also not considered to be Nothing).
So here is my inquiry: Is there a way to check if an object has been disconnected from its client before the run-time error occurs so as to avoid having to rely on on error resume next?
Such as:
If Not isDisconnected(wordDoc) Then
wordDoc.Close wdCloseWithoutSaving
End If
If Not isDisconnected(wordApp) Then
wordApp.Quit SaveChanges:=wdDoNotSaveChanges
End If
Update 1:
After looking at omegastripes' answer, I realized that the error given above only occurs when the document (wordDoc) was the object that got disconnected. If the Word application (wordApp) is what got disconnected, I get the following error:
Run-time error '462':
The remote server machine does not exist or is unavailable
Consider the below example:
Sub Test()
Dim wordApp As Object
Dim wordWnd As Object
Dim wordDoc As Object
Set wordApp = CreateObject("Word.Application")
Set wordWnd = wordApp.Windows ' choose any object property as indicator
wordApp.Visible = True ' debug
Set wordDoc = wordApp.Documents.Open(Application.ActiveWorkbook.Path & "\test.doc")
MsgBox IsObjectDisconnected(wordWnd) ' False with opened document
wordDoc.Close
MsgBox IsObjectDisconnected(wordWnd) ' False with closed document
wordApp.Quit ' disconnection
MsgBox IsObjectDisconnected(wordWnd) ' True with quited application
End Sub
Function IsObjectDisconnected(objSample As Object) As Boolean
On Error Resume Next
Do
IsObjectDisconnected = TypeName(objSample) = "Object"
If Err = 0 Then Exit Function
DoEvents
Err.Clear
Loop
End Function
Seems any type detection of the variable, which references to the intrinsic Word objects, like .Documents, .Windows, .RecentFiles, etc., made immediately after document close or application quit commands have been invoked, may throw the error 14: Out of string space, while Word application processing the command. The same detection on the Applicationobject , may also hang Excel application.
In the example TypeName() call is wrapped into OERN loop, that should skip irrelevant results to get explicit disconnection feedback, relying on the type name, but not on the error number. To avoid hanging, .Windows property is being checked instead of Application.