Rewrite Word VBA Macro in VB.NET

I am trying to rewrite this in a VB.NET form which is manipulating a Word document, but running into a number of problems.

Original VBA in Word
Dim rng As Range
Dim intPages As Integer

With ActiveDocument
    For intPages = Selection.Information(wdNumberOfPagesInDocument) To 0 Step -1
        Set rng = .GoTo(What:=wdGoToPage, Name:=intPages)
        Set rng = rng.GoTo(What:=wdGoToBookmark, Name:="\page")
        If Len(rng) = 2 Then
        End If
End With

Open in new window

My attempt to rewrite in VB.NET
    Private Sub Button2_Click_1(sender As Object, e As EventArgs) Handles Button2.Click

        Dim oWord As Microsoft.Office.Interop.Word.Application
        Dim oDoc As Microsoft.Office.Interop.Word.Document
        Dim oTable As Microsoft.Office.Interop.Word.Table
        Dim oPara1 As Microsoft.Office.Interop.Word.Paragraph, oPara2 As Microsoft.Office.Interop.Word.Paragraph
        Dim oPara3 As Microsoft.Office.Interop.Word.Paragraph, oPara4 As Microsoft.Office.Interop.Word.Paragraph
        Dim oRng As Microsoft.Office.Interop.Word.Range
        Dim oShape As Microsoft.Office.Interop.Word.InlineShape
        Dim oChart As Object
        Dim Pos As Double

        Dim oPages As Microsoft.Office.Interop.Word.Pages
        Dim intPages As Integer

        'Start Word and open the document template.
        oWord = CreateObject("Word.Application")
        oWord.Visible = True
        oDoc = oWord.Documents.Add("C:\Users\Owner\Documents\HAI\Clients\JamesJaccard\jimtest.docx")

        For intPages = Microsoft.Office.Interop.Word.Selection.Information(wdNumberOfPagesInDocument) To 0 Step -1
            oRng = .GoTo(What:=wdGoToPage, Name:=intPages)
            oRng = oRng.GoTo(What:=wdGoToBookmark, Name:="\page")
            If Len(oRng) = 2 Then
            End If

        oWord = Nothing

    End Sub

Open in new window

Here is an image of my VB.NET code so you can see where I am having problems.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fabrice LambertConsultingCommented:

It looks like you're facing a typicall late binding problem:
Constants / enums are unknown.

So either:
- Fully qualify them.
- Declare them in the function
- Declare them in a separate file (preferably use a namespace) that you'll import whenever you need it.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
That is true for those enums. You already perform early binding in the DIMs, so you should be able to fully quallify the enums too. You'll have to view the description for the methods you use them in to see the full class name, and translate that into Interop syntax.

You'll also have to prefix method calls just starting with a dot (e..g   .GoTo) with the proper object oDoc or oWord at all times.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Neil FlemingConsultant and developerCommented:
As Qlemo says. In your original, you have a "with ActiveDocument" statement that allows you to use

Open in new window

But your re-write does not use an equivalent "with oDoc".

Meanwhile, if you are only using those 3 enums then their values are:

wdgotopage = 1
wdgotobookmark = -1
wdnumberofpagesindocument = 4

You can simply plug those numbers into the code, eg:

oRng = oRng.GoTo(What:=-1, Name:="\page")

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.