none
VB.net – how do you set a variable to get ActiveDocument name RRS feed

  • Question

  • I have an existing Word session running

    A word document is open – DocumentOne

    I would like to obtain and set a variable to DocumentOne

    A second document is opened – DocumentTwo

    I would like to obtain and set a variable to DocumentTwo active Document name

    Select and activate DocumentOne

    Edit DocumentOne

    Select and activate DocumentTwo

    Edit DocumentTwo

    Select and activate DocumentOne

    Do until end…

    I have working code in VBA. However vb.net fails, and it maybe due to my declarations.

    I’ve searched the net and some examples declare a new instance of Word.  However I’m using the current open Word session/Instance.

    This is my working VBA code.

    Dim DocOne As Document

    Dim DocTwo As Document

    Dim strDocOne As String

    dim 

    Set DocOne = ActiveDocument

    x = ActiveDocument.Sections.Count

    strDocOne = ActiveDocument.ActiveWindow.Caption

    MsgBox (ActiveDocument.ActiveWindow.Caption)

    MsgBox (strDocOne)

    ‘File Open second doc

    Set DocTwo = ActiveDocument

    Windows(strDocOne).Activate

    ActiveDocument.Sections(j).Range.Select

    Selection.Copy

    Vb.net code Fail here…

    Dim DocOne As Microsoft.Office.Tools.Word.Document

    Dim DocTwo As Microsoft.Office.Interop.Word.Document


    Perhaps I’m not correctly declaring. any suggestions would be greatly appreciated.

    Many thanks in advance

    --dd


    • Edited by dd_ Tuesday, January 27, 2015 11:36 AM
    Tuesday, January 27, 2015 11:35 AM

Answers

  • Hello,

    What line of code exactly throws an exception?

    You can use the Documents property of the Application class which returns a Documents collection that represents all the open documents. So, you can initialize each reference with the document instances.

    The Document class provides the Activate method which activates the specified document so that it becomes the active document.

    Tuesday, January 27, 2015 1:56 PM