0
votes

I have a small VB.Net application which opens a new word document from a template (opens as Document1.docx for example). It runs some code to find a replace some text.

It then allows the user to edit the open document. There is a small winform open with a button called 'Complete'. When the user clicks this, I want it to save the document and do some further editing etc.

However, I cannot get the code to work to see the active document in the running instance of Word.

Whenever I refer to the activedocument, it throws and exception saying there are no documents open.

The code which is looking to save the active document is in a separate sub from where the Word.Application object is created. Word is running with the document open but it still fails.

I've tried the following to make sure it is getting the instance of word that is open; WordApp = Marshal.GetActiveObject("Word.Application")

But is still does not see any open documents.

Any suggestions?

EDIT #1 - Sorry for not including the code;

This is the code that starts Word and does some basic find/replace. It then starts form3 which stays in place whilst the user edits the word document. This code runs from a button click on form 2;

    Dim oWord As Word.Application
    Dim oDoc As Word.Document
    Dim path As String

    path = "C:\Templates/"
    Try
        oWord = CreateObject("Word.Application")
        oWord.Visible = True
    Catch ex As Exception
      ''Some error handling code not included for the purpose of StackOverflow  
    End Try

    Try
        oDoc = oWord.Documents.Add(path & "MG11.dot")
    Catch ex As Exception
         ''Some error handling code not included for the purpose of StackOverflow  
    End Try


    With oWord.Selection.Find
        ''Find & Replace Code - this works so is not included for Stackoverflow
    End With

    Form3.Show()
    Me.WindowState = FormWindowState.Minimized
    Form1.WindowState = FormWindowState.Minimized '(form 1 has no code which effects Word)

     End Sub

Form 3 has one button, which when pressed should save the word document. It will give it a filename based on info from elsewhere. Only the basic code is shown here - I just need to get vb.net to see the active document for now. This code is ran on Button_1 Click for Form 3.

Dim WordApp As Word.Application Dim oDoc As Word.Document = WordApp.ActiveDocument

    oDoc = WordApp.ActiveDocument

I've also tried;

    Dim WordApp As Word.Application
    WordApp = Marshal.GetActiveObject("Word.Application")
    Dim oDoc As Word.Document

    oDoc = WordApp.ActiveDocument

'once oDoc is set properly I will use SaveAs2 to handle the saving etc

It fails on WordApp.ActiveDocument, stating there are no open documents.

1
I suggest you show all code relevant to question, so that people can helpA Friend
Agreed with A Friend, we need to see the code that you already have so we can add to it.Danny James
code added with some explanation as requested.Adrian Brown

1 Answers

1
votes

Either make sure oWord is in scope in your Form3. It might already be in scope or you add a Public Word.Applications property to your Form3 and set that property before showing the form.

In the first case you access the ActiveDocument on the existing oWord object in scope, in the second you set the property on the form and access that before showing.

 oWord.ActiveDocument