there is no Office.CommandBarButton, its full name is: Microsoft.Office.Core.CommandBarButton. as you have the namespace imported just use: "CommandBarButton"
The type or namespace name 'Office' could not be found
Andrews Dumith
41
Reputation points
Hi guys,
I am developing my first Outlook Add-In in C# and I am having the following problem.
Although the references are correctly added, the code is giving me the following error:
The type or namespace name 'Office' could not be found (are you missing a using directive or an assembly reference?)
I share with you the content of my ThisAddIn.cs file so you can see where the error occurs.
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Outlook;
using System;
using System.IO;
using System.Runtime.InteropServices;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace NewClientMatterIntake
{
public partial class ThisAddIn
{
private Ribbon ribbon;
private void ThisAddIn_Startup(object sender, EventArgs e)
{
ribbon = new Ribbon();
}
protected override Office.IRibbonExtensibility CreateRibbonExtensibilityObject()
{
return new Ribbon();
}
private void ThisAddIn_Shutdown(object sender, EventArgs e)
{
// Aquí puedes colocar código de limpieza si es necesario.
}
private string ReadTemplateFromFile(string templateName)
{
string filePath = Path.Combine(Environment.CurrentDirectory, "Templates", templateName);
if (File.Exists(filePath))
{
try
{
return File.ReadAllText(filePath);
}
catch (System.Exception ex) // Usamos System.Exception para evitar la ambigüedad
{
System.Windows.Forms.MessageBox.Show($"Error reading template '{templateName}': {ex.Message}");
}
}
else
{
System.Windows.Forms.MessageBox.Show($"Template file '{templateName}' not found.");
}
return string.Empty;
}
private void InternalButton_Click(Office.CommandBarButton Ctrl, ref bool CancelDefault)
{
string body = ReadTemplateFromFile("InternalTemplate.html");
if (!string.IsNullOrEmpty(body))
{
CreateEmail("New Client/Matter Intake (Internal)", body);
}
}
private void ExternalButton_Click(Office.CommandBarButton Ctrl, ref bool CancelDefault)
{
string body = ReadTemplateFromFile("ExternalTemplate.html");
if (!string.IsNullOrEmpty(body))
{
CreateEmail("New Client/Matter Intake (External)", body);
}
}
private void CreateEmail(string subject, string body)
{
var mail = Application.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
mail.Subject = subject;
mail.BodyFormat = Outlook.OlBodyFormat.olFormatHTML;
mail.HTMLBody = body + GetSignature();
mail.Display();
}
private string GetSignature()
{
var tempMail = Application.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
string signature = tempMail.HTMLBody;
Marshal.ReleaseComObject(tempMail);
return signature;
}
#region VSTO generated code
private void InternalStartup()
{
this.Startup += new EventHandler(ThisAddIn_Startup);
this.Shutdown += new EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}
I also share with you the references of my project.
Thank you in advance.