PDA

View Full Version : Help with Visual Studio



Bilby
07-16-2007, 04:49 PM
Greetings,

I am attempting to move from VBA to Visual Studio 2005 (VB) and I'm having fun:banghead: . I have written a COM addin that launchs a userform from a Word 2007 ribbon button. This works up to here.
When the user clicks a button on the userform a word document should be created. I've attempted the .Documents.Add

Object.Documents.Add(Template:="C:\Program Files\Named.dotm")

just about every way i can think of, but I just don't seem to be able to come to grips with how VS 2005 handles Word. If anyone has/knows of an example where a userform commandbutton creates a word document I'd be grateful.

fumei
07-17-2007, 07:49 AM
When you mention something is not working, it would be helpful if you clearly state was DOES happen.

Do you get an error message? if so, what error message?

VS 2005 handles Word like it handles any other application. It needs a reference. Does it have one?

A userform made from VBA in Word can create a document easily enough as it already has a reference.

Norie
07-17-2007, 09:22 AM
Bilby

You do realise that Visual Studio is only an IDE.

Are you using VB. NET? VB? VC++?

Bilby
07-17-2007, 07:57 PM
My Apologies,

The Reference Names taken from the COMs projects references tab lists:
Extensibility
MS Office 12 Object library
Ms Word 12 Object library
system
system.data
system.drawing
system.windows.forms
system.xml
as being referenced the COM, at least its ribbon creation functions work fine. Its written using Visual Basic 2005

The ribbon button opens a UserForm written in Visual Basic 2005. This UserFrom has several buttons each is supposed to create a new document based on a different template.

This is a sample of the code behind each button,
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
On Error Resume Next
MakeDocument("C:\Temp\Experimental 006.dotm")
On Error Resume Next
WAT.ActiveForm.Close()
End Sub

I am obviously over my head any help would be greatly appreciated

The MakeDocument sub accepts the full path name and should create a new doc

Private Sub MakeDocument(ByVal sS As Object)
On Error Resume Next
Dim wordApp As New word.Application()
Dim newTemplate As Object = False
Dim docType As Object = 0
Dim isVisible As Object = True
Dim aDoc As word.Document
aDoc = wordApp.Documents.Add(sS, newTemplate, docType, isVisible)
wordApp.Visible = True
aDoc.Activate()
End Sub
Unfortunately it only sometimes works, and only with Word 2003 templates!

In debug mode I get two messages
The first is:
LoadFromContext was detected
Message: The assembly named 'BilbyRibbon' was loaded from 'file:///C:/Program Files/DOD/BilbyRibbonSetup/BilbyRibbon.dll' using the LoadFrom context.
The use of this context can result in unexpected behavior for serialization, casting and dependency resolution.
In almost all cases, it is recommended that the LoadFrom context be avoided.
This can be done by installing assemblies in the Global Assembly Cache or in the ApplicationBase directory and using Assembly.Load when explicitly loading assemblies.


The second is more irritating as my DOTM template works fine when manually run from explorer.

System.Runtime.InteropServices.COMException occurred
ErrorCode=-2146823137
HelpLink="C:\Program Files\Microsoft Office\Office12\1033\WDMAIN11.CHM#24631"
Message="Word was unable to read this document. It may be corrupt.

Any help would be greatly appreciated.