Creating a MAPI Session in VBA - excel

I need to send some eMails from an excel-sheet. I currently use Excel2010, but it should also work with 2013. I searched the net for a solution and found a function, which unfortunatly doesn't work for me.
Sub SendMail(Recepient As String, Address As String, Subject As String, Contents As String)
Dim mapi_session As MSMAPI.MAPISession
Dim mapi_messages As MSMAPI.MAPIMessages
'Set mapi_session = New MSMAPI.MAPISession
Set mapi_session = CreateObject("MAPI.Session")
...
At line Set mapi_session = New MSMAPI.MAPISession it throws an error, saying "Unable to create object by ActiveX component (I translated from german)". I did some more investigation and came across a thread that suggested to use CreateObject instead, but it still throws the same error.
I included the msmapi32.ocx beforehand.
I'm pretty new to VBA - any idea what the problem might be?
Thanks in advance,
Frank

CDO 1.21 (that is what "MAPI.Session" is) is no longer being installed by Outlook. You can download it from https://www.microsoft.com/en-us/download/details.aspx?id=3671, but keep in mind that it is no longer being developed or supported by Microsoft.
What do you do with CDO 1.21? You really need to change your code to use only the Outlook Object Model. You can also use Redemption (I am its author) - it's RDO family of objects is (just like CDO 1.21) an in-proc MAPI wrapper and its RDOSession object is similar to MAPI.Session in CDO 1.21 (but has quite a few extra goodies).

Try Set mapi_session = CreateObject("MSMAPI.MAPISession").

Related

Add custom property to 97/2003 MSOffice Files (xls/doc/ppt) using DSOFile 2.1

I'm trying to add custom property on the ppt file. But Received value("güsiöçÇÖISÜG") is not equal sent value("ğüşiöçÇÖİŞÜĞ").
I faced a strange situation like that:
Open the sample.ppt by LibreOffice.
Save (CTRL+S) and Close, any change is not necessarily.
Run the code again.
And two values are equal
Sample ppt files is here: https://drive.google.com/drive/folders/1qTlREuLAAM-I0JIn-htPwLEUwRjQ9IiJ?usp=sharing
Is there anyone to help me, thanks.
You need to check encodings of your Excel files. Most probably a software (DSO) generates a UTF-8 based document instead of UTF-16. For example, you may try using the following code:
Dim strEncodingName As String = String.Empty
Dim myStreamRdr As System.IO.StreamReader = New System.IO.StreamReader(myFileName, True)
Dim myString As String = myStreamRdr.ReadToEnd()
strEncodingName = mmFileIA.CurrentEncoding.EncodingName

How to query files (csv, bmp) using NI-488.2 from a GPIB port in VBA (Excel)

Using VBA, I tried to use the NI-488.2 calls directly, instead of using VISA COM. However, when I try to use NI-488.2 commands in VBA, I get the following error message :
''Sub or Function not defined''.
That is, when calling Call SendIFC(BOARD_ID) or Call EnableRemote(BOARD_ID, intAddrList()), Call SendGPIB(osa, ":mmem:stor:grap color,bmp,""test"",int"), = RecieveBinaryGPIB(osa, byteData()), etc...
My first guess would be that I am simply missing the correct library. (Or is there any deeper issue here?). For now, I added the VISA COM 3.0 Type Library and the VISA COM 488.2 Formatted I/O 1.0 to the Excel references.
Here is a sample code from YOKOGAWA (slightly edited) that can be found online and applies to Visual Basics, but that I could not run in VBA. I removed most of the code for clarity purpose. It simply saves a screenshot in internal memory.
Private Sub SaveImage()
Const BOARD_ID = 0 'GP-IB Interface card Address
Const osa = 1 'OSA GP-IB Address
Dim intAddrList(31) As Integer
'----- GP-IB Interface setting
' send IFC
Call SendIFC(BOARD_ID)
' assert th REN GPIB line
intAddrList(0) = NOADDR
Call EnableRemote(BOARD_ID, intAddrList())
' GPIB time out setting
Call ibtmo(BOARD_ID, T30s) 'Time out = 30sec
'----- send command to OSA
Call SendGPIB(osa, "CFORM1") ' Command mode set(AQ637X mode)
Call SendGPIB(osa, ":mmem:stor:grap color,bmp,""test"",int")
'----- Disconnect
Call EnableLocal(BOARD_ID, intAddrList())
End Sub
Now we have the code... you have now edited out the useful bits... I'm going to backtrack a bit and say that even if you get direct calls to work you’re still going to have a problem extracting the files.
I’m going to guess you’re getting "Undefined header" errors if you query the OSA with "SYST:ERR:ALL?" and this is quite possibly because the query command for the data is invalid.
I’ve notice you’re using including the data type your command:
":mmem:data? ""test.bmp"",int"
"test.bmp",int,0 is what you might see if you query the directory, however, it is not needed when extracting the file.
try using:
":mmem:data? ""test.bmp"""
(The number of quotations are to include them in the string)
If not, what is the GPIB error?

How to Create object of MS Access Runtime in Excel VBA

I have Microsoft Access Runtime not full version of Microsoft Access, When i create object in Excel VBA
Set objAccess = CreateObject("Access.Application")
That time i am getting
Error 429 "ActiveX component can't create object."
Suggest how to create object?
I'm not sure whether this information is still relevant to OP, but it might help out others (like me) who were looking for a solution:
In cases where the simple route
Dim AccApp as Object
Set AccApp = CreateObject("Access.Application")
doesn't work (e.g. because only the Runtime Version of Access is available), the following route seems to work:
Const PathToDBFile as String = "W:\here\Your\DB\lies.accdb"
Const PathToAccess as String = "C:\Program files\YourOfficeVersion\MSACCESS.EXE"
Dim ShellCmd as String
' Piece together the parts (yes, the quotes are necessary in case there are spaces in the paths)
ShellCmd = """" & PathToAccess & """ """ & PathToDBFile & """"
' Execute the command in the shell
VBA.Shell ShellCmd
' Now GetObject can return the newly created instance of Access
Dim AccApp as Object
Set objAcc = GetObject(PathToDBFile)
(Source)
This code is only the bare bones to show the essential steps. One likely wants to make sure there isn't already an instance of Access running. Also I've not yet worked out how I can reliably get the path to the MSAccess.exe on different systems. But the above worked for me when I tried on a system with only the Runtime Version installed. (I was able to get the correct return from AccApp.Run "MyFunction".)

Unable to find the right excel file library reference error

Every time I open this Excel file I get this error which is in screenshot 1 and screenshot 2.
When I click OK it takes me to code windows and asks me to select library.
I tried everything to make it work but no luck.
Screenshot one and screenshot two:
Make sure that these references are in this Priority order.
HTML and Internet Controls need to be above OLE Automation.
If you are still having this issue after this, run a Repair on the Office install and it refreshes the .DLL files that may have been corrupted.
Update: 7/6/2017
The pervious answer above will re-associate the references and allow the script to compile.
However, there is a better way.
This issue occurs when the VBA script is shared to other computers that are not identical platforms(OS and MS Office versions) and use early binding. Early binding reduces latency and is the correct way when only intended for one computer.
The answer is to use late binding format in the script and not select any references. Change any data type objects other than object back to object and use the following format:
Sub Late_Binding()
Dim IE_App_obj As Object
Dim MyShell_obj As Object
Dim IE_Window_obj As Object
Dim Windows_cnt As Long
Dim x_cnt As Variant
Dim HTML_Element_obj As Object
Set IE_App_obj = CreateObject("InternetExplorer.Application")
'Use IE_App_obj to Navigate to webpage and control it.'
Set MyShell_obj = CreateObject("Shell.Application")
'Use MyShell_obj to find an existing webpage and control it.'
Let Windows_cnt = MyShell.Windows.Count
For x_cnt = Windows_cnt - 1 To 0 Step -1
On Error Resume Next
If Instr(MyShell_obj.Windows(x_cnt).Document.Title,"WebPage_Title") > 0 Then
Set IE_Window_obj = MyShell_obj.Windows(x_cnt)
Exit For
End If
Next
Set HTML_Element_obj = IE_Window_obj.Document.getElementByID("ID_text")
End Sub
Latency will increase but so will stability.

How do I set up a COM Server interface with Excel?

Versions
Excel 2003Windows XP SimaPro 7.3.0 Developer Version Using a Work computer but was made administrator on this machine Libraries referenced in Excel/VBA: Visual Basic for Applications; Microsoft Excel 11.0 Object Library; OLE Automation; Microsoft Office 11.0 Object Library; Microsoft Forms 2.0 Object Library; COM+ 1.0 Admin Type Library; COM MakeCab 1.0 Type Library; COM+ Services Type Library; SimaPro Library Me: Beginner
What I'm trying to do
I am using a program called SimaPro that stores databases of "Life Cycle Analysis" information. The program has built in COM interface functionality. The program states that it does, indeed support Excel/VBA (but it doesn't specify versions).
I am trying to connect this program and/or COM server to excel so that I can interact with the information through excel.
What I've done
I've done the procedure they list:
-Open SimaPro
-Register COM Server
-Then I pasted the below code into VBA and tried to run it. This code is the sample code provided by the software company, I edited only the SP.Server, SP.Alias, SP.Login, and SP.OpenProject fields (below is as edited).
What Happened
Run-time Error: '-2147418113 (8000ffff)':
Automation Error
Catastrophic Failure
Question(s)
-Is the server name right? I've been reading a little on COM servers and I don't know if the way I put it in is in the right "form"
-Could it have something to do with certain registered/unregistered DLLs? I've worked with the company's IT people, and software programmers. None of them were very familiar with COM but one person suggested the DLLs might be the issue.
Thanks for your help!!
Here is the code that I'm inputting:
Sub CreateProcess()
Dim SP As SimaProServer
Dim PC As Process
Dim PC2 As Process
Dim PL As ProcessLine
Dim Param As ParamLine
Dim Subs As Substance
Set SP = New SimaProServer
SP.Server = "Local Server"
SP.Alias = "C:\Documents and Settings\All Users\Documents\SimaPro\Database\"
SP.Database = "Professional"
SP.OpenDatabase
SP.Login "", ""
SP.OpenProject "PROJECT", ""
' Not project's actual name, not allowed to state name of project
SP.CreateSubstance "Air", Subs
Subs.CASNumber = "4-5-13"
Subs.Name = "Some substance"
Subs.DefaultUnit = "kg"
Subs.Update
SP.CreateProcess ptMaterial, PC
Set PL = PC.AddLine(ppProduct, -1)
PL.ObjectName = "Steel 2"
PL.UnitName = "kg"
PL.Amount = "2"
PL.Comment.Add ("My new created process")
PL.CategoryPath = "Chemicals\inorganic"
PC.Update
' create second material process Case
SP.CreateProcess ptMaterial, PC2
Set PL = PC2.AddLine(ppProducts, 0)
PL.ObjectName = "Case 2"
PL.UnitName = "kg"
PL.Amount = "10"
Set Param = PC2.AddParamLine(ptInputParameter, -1)
Param.Name = "A"
Param.Value = "2,3"
' add input from Steel
Set PL = PC2.AddLine(ppMaterialsFuels, -1)
' input from steel
PL.SetProduct "Introduction to SimaPro 7", ptMaterial, "Steel 2"
PL.Amount = "8"
PL.UnitName = "kg"
Set PL = PC2.AddLine(ppAirborneEmissions, -1)
' input from steel
PL.SetSubstance "Some substance", ""
PL.Amount = "A+1"
PL.UnitName = "kg"
PC2.Update
SP.Logout
SP.CloseDatabase
Set SP = Nothing
End Sub
Given that this was more than one year ago. I'm assuming you got this working. If you haven't yet, I might know what the root cause might be.
I used to get the same error and from your changes for server, alias and login, I was able to make it run. One thing that is different is that you have changed the name of the project to "Project" from "Introduction to SimaPro 7". I honestly have zero (not being humble here) VBA knowledge. So, I'm speculating that there is no project names "Project" to open. I'm not sure if VBA would create a project automatically, if it can't find it. You can either try creating a project named "Project" or just rename it back. I am interested to see if that worked.
Automation Error usually means that there was a problem within the COM library you try to use. As it is a run-time error, it could be something very stupid, as missing parameter or wrong path or access rights. In my opinion, it also means that the library is not very well designed.
As you are not the author of the library you do not have many options. You can try to contact the vendor to get more documentation. You can also pray that the designer thought about logging - check the event log; if you are lucky you may find something interesting there.
Answering your first question, if you referenced the library and the code compiles - that means that you did everything right there.

Resources