Insert an Image on .docx with pre-inserted image fails

Jul 17, 2009 at 11:59 PM


Maybe this is a BUG:

If I want to insert an image via "DocX.AddImage" on a .docx file with pre-inserted images on it, a exception is throw.

The exception is marked on the line

ImgItem = docItemSheet.AddImage(ExamDocs.Folder + "img" + CStr(j + 1) + ".jpg")

and is described as "A part of URI must start with slash".

If I delete the pre-inserted image, the code runs smoothly.



Jul 20, 2009 at 11:48 AM
Hello Gaspar,
sorry it took me so long to reply, I have been away exploring Ireland all weekend. I am unable to recreate the problem that you are having with Image insertion on my side. The error you have reported "A part of URI must start with slash" is odd. Can you create and send me a simple example in which this error always occurs?
Something like
1) Create a new document,
2) Add and image,
3) Add a second (different) image 
4) (Error occurs here)
kind regards,
Jul 20, 2009 at 2:52 PM

Hello Cathal:

(BTW nice exploration tour!)

Sorry for the inaccurate explanation, I was too much hurry.

I follow these steps, and I get the above exception:

1) Create a .docx file in Word 2007. The file contains custom styles, CustomProperties inserted on body and footers text. Also the file contains a embedded (not linked) image inside a TextBox (not a VBA text box, just an ordinary Word's box of text. In my Spanish version of Word2007, is "Cuadro de texto").

2) Load the .docx file via DocX library

3) Replace some text, add some CustomProperty (add, just for replace the values)

4) When I try to add another image, an exception is throw:

The error is marked at the line

ImgItem = docItemSheet.AddImage("Image.jpg")

I'm using a Spanish version of .Net framework, so the exception is in "Español":

"Un URI de parte debe comenzar con una barra diagonal."


"A part of URI must start with slash"

The StackTrace of the exceptions shows:

en System.IO.Packaging.PackUriHelper.ValidatePartUri(Uri partUri)
   en System.IO.Packaging.Package.GetPartHelper(Uri partUri)
   en System.IO.Packaging.Package.GetPart(Uri partUri)
   en Novacode.DocX.<AddImage>b__14(PackageRelationship ir)
   en System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   en Novacode.DocX.AddImage(Object o)
   en Novacode.DocX.AddImage(String filename)
   en feedbacK.frmExamNew.GenerarDocHojaItemOpc() en D:\Gaspar\Visual Studio 2008\Projects\feedbacKv7-trunk\feedbacKv7\frmExamNew.vb:línea 1976
   en feedbacK.frmExamNew.GenerarDocX() en D:\Gaspar\Visual Studio 2008\Projects\feedbacKv7-trunk\feedbacKv7\frmExamNew.vb:línea 1880
   en feedbacK.frmExamNew.btnNextStep3_MouseClick(Object sender, MouseEventArgs e) en D:\Gaspar\Visual Studio 2008\Projects\feedbacKv7-trunk\feedbacKv7\frmExamNew.vb:línea 1838
   en System.Windows.Forms.Control.OnMouseClick(MouseEventArgs e)
   en System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   en System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   en System.Windows.Forms.Control.WndProc(Message& m)
   en System.Windows.Forms.ButtonBase.WndProc(Message& m)
   en System.Windows.Forms.Button.WndProc(Message& m)
   en System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   en System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   en System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   en System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   en System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
   en System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   en System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   en System.Windows.Forms.Application.Run(ApplicationContext context)
   en Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
   en Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
   en Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
   en feedbacK.My.MyApplication.Main(String[] Args) en 17d14f5c-a337-4978-8281-53493378c1071.vb:línea 81
   en System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
   en System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   en Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   en System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   en System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   en System.Threading.ThreadHelper.ThreadStart()

When I try to isolate the annoying bug, I found the "pre-inserted image" been the cause. If I remove it from the file, the exception don't occurs.

I hope was clear enough this time...

Thanks as always!



Jul 28, 2009 at 10:44 PM

Hi Gaspar,

sorry it has taken me so long to reply, I have been very busy at work. Thank you for taking the time to write out such detailed steps. Unfortunately I still cannot seem to recreate this bug on my side. If the document is neither personal or confidential, can you please email it to me, so that I can do further testing?

I entend to get to the bottom of this bug :-)

kind regards,