Я пытаюсь создать документ Word, вставить несколько изображений и сохранить его.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Word = Microsoft.Office.Interop.Word;
using System.Windows.Forms;
using System.IO;
namespace Snapper
{
class WordDocumentGenerator
{
public void CreateWordDocument(string fileName)
{
string originalPath = Directory.GetCurrentDirectory();
string path = originalPath;
path += @"\snapshots";
Directory.SetCurrentDirectory(path);
Word._Application wordApp = new Word.Application();
Word._Document wordDoc;
object oMissing = System.Reflection.Missing.Value;
wordDoc = wordApp.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
wordDoc.InlineShapes.AddPicture(@"\snap1.jpg");
wordDoc.InlineShapes.AddPicture(@"\snap2.jpg");
path = originalPath;
path += @"\documents";
DirectoryInfo docDir = new DirectoryInfo(path);
if (!docDir.Exists)
{
docDir.Create();
}
Directory.SetCurrentDirectory(path);
Object savePath = path + @"\" + fileName + ".doc";
wordDoc.SaveAs(ref savePath,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing
);
wordDoc.Close(SaveChanges: true);
}
}
}
Я получаю сообщение об ошибке необработанного исключения, в котором говорится, что это недопустимое имя файла для метода addpicture(). Что я делаю не так?
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.Runtime.InteropServices.COMException (0x800A1420): This is not a valid file name.
Try one or more of the following:
* Check the path to make sure it was typed correctly.
* Select a file from the list of files and folders.
at Microsoft.Office.Interop.Word.InlineShapes.AddPicture(String FileName, Object& LinkToFile, Object& SaveWithDocument, Object& Range)
at Snapper.WordDocumentGenerator.CreateWordDocument(String fileName) in D:\RESOURCES\Projects\Visual Studio Projects\Snapper\Snapper\GenerateWordDocument.cs:line 27
at Snapper.SnapperForm.CreateDocumentButton_Click(Object sender, EventArgs e) in D:\RESOURCES\Projects\Visual Studio Projects\Snapper\Snapper\SnapperForm.cs:line 130
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
************** Loaded Assemblies **************
mscorlib
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4971 (win7RTMGDR.050727-4900)
CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
Snapper
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///D:/RESOURCES/Projects/Visual%20Studio%20Projects/Snapper/Snapper/bin/Debug/Snapper.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4977 (win7RTMGDR.050727-4900)
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4971 (win7RTMGDR.050727-4900)
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4980 (win7RTMGDR.050727-4900)
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
HotKeys
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///D:/RESOURCES/Projects/Visual%20Studio%20Projects/Snapper/Snapper/bin/Debug/HotKeys.DLL
----------------------------------------
System.Configuration
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
Microsoft.Office.Interop.Word
Assembly Version: 14.0.0.0
Win32 Version: 14.0.4762.1000
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/Microsoft.Office.Interop.Word/14.0.0.0__71e9bce111e9429c/Microsoft.Office.Interop.Word.dll
----------------------------------------
************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.
For example:
When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.
Подробнее здесь: https://stackoverflow.com/questions/146 ... gh-c-sharp
Невозможно вставлять изображения в документ Word, созданный с помощью C# ⇐ C#
Место общения программистов C#
1714800006
Anonymous
Я пытаюсь создать документ Word, вставить несколько изображений и сохранить его.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Word = Microsoft.Office.Interop.Word;
using System.Windows.Forms;
using System.IO;
namespace Snapper
{
class WordDocumentGenerator
{
public void CreateWordDocument(string fileName)
{
string originalPath = Directory.GetCurrentDirectory();
string path = originalPath;
path += @"\snapshots";
Directory.SetCurrentDirectory(path);
Word._Application wordApp = new Word.Application();
Word._Document wordDoc;
object oMissing = System.Reflection.Missing.Value;
wordDoc = wordApp.Documents.Add(ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
wordDoc.InlineShapes.AddPicture(@"\snap1.jpg");
wordDoc.InlineShapes.AddPicture(@"\snap2.jpg");
path = originalPath;
path += @"\documents";
DirectoryInfo docDir = new DirectoryInfo(path);
if (!docDir.Exists)
{
docDir.Create();
}
Directory.SetCurrentDirectory(path);
Object savePath = path + @"\" + fileName + ".doc";
wordDoc.SaveAs(ref savePath,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing,
ref oMissing
);
wordDoc.Close(SaveChanges: true);
}
}
}
Я получаю сообщение об ошибке необработанного исключения, в котором говорится, что это недопустимое имя файла для метода addpicture(). Что я делаю не так?
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.Runtime.InteropServices.COMException (0x800A1420): This is not a valid file name.
Try one or more of the following:
* Check the path to make sure it was typed correctly.
* Select a file from the list of files and folders.
at Microsoft.Office.Interop.Word.InlineShapes.AddPicture(String FileName, Object& LinkToFile, Object& SaveWithDocument, Object& Range)
at Snapper.WordDocumentGenerator.CreateWordDocument(String fileName) in D:\RESOURCES\Projects\Visual Studio Projects\Snapper\Snapper\GenerateWordDocument.cs:line 27
at Snapper.SnapperForm.CreateDocumentButton_Click(Object sender, EventArgs e) in D:\RESOURCES\Projects\Visual Studio Projects\Snapper\Snapper\SnapperForm.cs:line 130
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
************** Loaded Assemblies **************
mscorlib
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4971 (win7RTMGDR.050727-4900)
CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
Snapper
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///D:/RESOURCES/Projects/Visual%20Studio%20Projects/Snapper/Snapper/bin/Debug/Snapper.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4977 (win7RTMGDR.050727-4900)
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4971 (win7RTMGDR.050727-4900)
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4980 (win7RTMGDR.050727-4900)
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
HotKeys
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///D:/RESOURCES/Projects/Visual%20Studio%20Projects/Snapper/Snapper/bin/Debug/HotKeys.DLL
----------------------------------------
System.Configuration
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
Microsoft.Office.Interop.Word
Assembly Version: 14.0.0.0
Win32 Version: 14.0.4762.1000
CodeBase: file:///C:/Windows/assembly/GAC_MSIL/Microsoft.Office.Interop.Word/14.0.0.0__71e9bce111e9429c/Microsoft.Office.Interop.Word.dll
----------------------------------------
************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.
For example:
When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.
Подробнее здесь: [url]https://stackoverflow.com/questions/14672709/cannot-insert-images-to-word-document-created-through-c-sharp[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия