Word 2000 macro -> VB.Net conversion question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mason

    Word 2000 macro -> VB.Net conversion question

    I'm having some problems converting VBA for Word 2000 to code that
    VB.Net understands. I recorded a macro in Word to add numbering (a.
    b. c.) to my paragraphs. I managed to translate quite a bit of it,
    but I'm having trouble with two pieces:

    ListGalleries - I can't find any way to make VB.Net understand this.
    When I do a mouseover, the error message it gives is, "Interface
    'Word.ListGalle ries' cannot be indexed because it has no default
    property."

    PointToInches - Not sure VB.Net even has a way to translate it.

    Here is the Macro I recorded, followed by my transalted VB.Net code. I
    removed some of the code I thought would be unnecessary to the
    translation, but I could be wrong.

    Sub Macro1()
    '
    ' Macro1 Macro
    ' Macro recorded 9/14/2004 by H2O
    '
    With ListGalleries(w dNumberGallery) .ListTemplates( 6).ListLevels(1 )
    .NumberFormat = "%1."
    .TrailingCharac ter = wdTrailingTab
    .NumberStyle = wdListNumberSty leLowercaseLett er
    .NumberPosition = InchesToPoints( 0.25)
    .Alignment = wdListLevelAlig nLeft
    .TextPosition = InchesToPoints( 0.5)
    .TabPosition = InchesToPoints( 0.5)
    .ResetOnHigher = 0
    .StartAt = 1
    With .Font
    .Bold = wdUndefined
    .Italic = wdUndefined
    .StrikeThrough = wdUndefined
    .Subscript = wdUndefined
    .Superscript = wdUndefined
    .Shadow = wdUndefined
    .Outline = wdUndefined
    .Emboss = wdUndefined
    .Engrave = wdUndefined
    .AllCaps = wdUndefined
    .Hidden = wdUndefined
    .Underline = wdUndefined
    .Color = wdUndefined
    .Size = wdUndefined
    .Animation = wdUndefined
    .DoubleStrikeTh rough = wdUndefined
    .Name = ""
    End With
    .LinkedStyle = ""
    End With
    ListGalleries(w dNumberGallery) .ListTemplates( 6).Name = ""
    Selection.Range .ListFormat.App lyListTemplate
    ListTemplate:=L istGalleries( _
    wdNumberGallery ).ListTemplates (6),
    ContinuePreviou sList:=False, ApplyTo:= _
    wdListApplyToWh oleList,
    DefaultListBeha vior:=wdWord9Li stBehavior
    Selection.TypeT ext Text:="Blah blah blah"
    Selection.TypeP aragraph
    Selection.TypeT ext Text:="Gah gah gah"
    Selection.TypeP aragraph
    Selection.TypeT ext Text:="Wee wee wee"
    End Sub

    ------- Begin VB.Net Code -------

    Private Sub testing(ByVal owordnew As Word.Applicatio n)

    With owordnew
    With .ListGalleries( Word.WdListGall eryType.wdNumbe rGallery).ListT emplates(6).Lis tLevels(1)
    .NumberFormat = "%1."
    .TrailingCharac ter =
    Word.WdTrailing Character.wdTra ilingTab
    .NumberStyle =
    Word.WdListNumb erStyle.wdListN umberStyleLower caseLetter
    .NumberPosition = InchesToPoints( 0.25)
    .Alignment =
    Word.WdListLeve lAlignment.wdLi stLevelAlignLef t
    .TextPosition = InchesToPoints( 0.5)
    .TabPosition = InchesToPoints( 0.5)
    .ResetOnHigher = 0
    .StartAt = 1
    .LinkedStyle = ""
    End With
    .ListGalleries( Word.WdListGall eryType.wdNumbe rGallery).ListT emplates(6).Nam e
    = ""
    .Selection.Rang e.ListFormat.Ap plyListTemplate (ListTemplate:= ListGalleries(
    _
    Word.WdListGall eryType.wdNumbe rGallery).ListT emplates(6),
    ContinuePreviou sList:=False, ApplyTo:= _
    Word.WdListAppl yTo.wdListApply ToWholeList,
    DefaultListBeha vior:=Word.WdDe faultListBehavi or.wdWord9ListB ehavior)
    .Selection.Type Text(Text:="Bla h blah blah")
    .Selection.Type Paragraph()
    .Selection.Type Text(Text:="Gah gah gah")
    .Selection.Type Paragraph()
    .Selection.Type Text(Text:="Wee wee wee")
    End With
    End Sub


    Thanks for any assistance,
    MW
  • Siv

    #2
    Re: Word 2000 macro -> VB.Net conversion question

    I notice that you pass the Word application object byval, I would have
    thought by reference would expose more stuff?

    Does the Word object have to be passed at all, could you create it in the
    same function as it is used?

    Siv

    "Mason" <masonwood@gmai l.com> wrote in message
    news:5523a35c.0 409140535.4017d 47a@posting.goo gle.com...[color=blue]
    > I'm having some problems converting VBA for Word 2000 to code that
    > VB.Net understands. I recorded a macro in Word to add numbering (a.
    > b. c.) to my paragraphs. I managed to translate quite a bit of it,
    > but I'm having trouble with two pieces:
    >
    > ListGalleries - I can't find any way to make VB.Net understand this.
    > When I do a mouseover, the error message it gives is, "Interface
    > 'Word.ListGalle ries' cannot be indexed because it has no default
    > property."
    >
    > PointToInches - Not sure VB.Net even has a way to translate it.
    >
    > Here is the Macro I recorded, followed by my transalted VB.Net code. I
    > removed some of the code I thought would be unnecessary to the
    > translation, but I could be wrong.
    >
    > Sub Macro1()
    > '
    > ' Macro1 Macro
    > ' Macro recorded 9/14/2004 by H2O
    > '
    > With ListGalleries(w dNumberGallery) .ListTemplates( 6).ListLevels(1 )
    > .NumberFormat = "%1."
    > .TrailingCharac ter = wdTrailingTab
    > .NumberStyle = wdListNumberSty leLowercaseLett er
    > .NumberPosition = InchesToPoints( 0.25)
    > .Alignment = wdListLevelAlig nLeft
    > .TextPosition = InchesToPoints( 0.5)
    > .TabPosition = InchesToPoints( 0.5)
    > .ResetOnHigher = 0
    > .StartAt = 1
    > With .Font
    > .Bold = wdUndefined
    > .Italic = wdUndefined
    > .StrikeThrough = wdUndefined
    > .Subscript = wdUndefined
    > .Superscript = wdUndefined
    > .Shadow = wdUndefined
    > .Outline = wdUndefined
    > .Emboss = wdUndefined
    > .Engrave = wdUndefined
    > .AllCaps = wdUndefined
    > .Hidden = wdUndefined
    > .Underline = wdUndefined
    > .Color = wdUndefined
    > .Size = wdUndefined
    > .Animation = wdUndefined
    > .DoubleStrikeTh rough = wdUndefined
    > .Name = ""
    > End With
    > .LinkedStyle = ""
    > End With
    > ListGalleries(w dNumberGallery) .ListTemplates( 6).Name = ""
    > Selection.Range .ListFormat.App lyListTemplate
    > ListTemplate:=L istGalleries( _
    > wdNumberGallery ).ListTemplates (6),
    > ContinuePreviou sList:=False, ApplyTo:= _
    > wdListApplyToWh oleList,
    > DefaultListBeha vior:=wdWord9Li stBehavior
    > Selection.TypeT ext Text:="Blah blah blah"
    > Selection.TypeP aragraph
    > Selection.TypeT ext Text:="Gah gah gah"
    > Selection.TypeP aragraph
    > Selection.TypeT ext Text:="Wee wee wee"
    > End Sub
    >
    > ------- Begin VB.Net Code -------
    >
    > Private Sub testing(ByVal owordnew As Word.Applicatio n)
    >
    > With owordnew
    > With
    > .ListGalleries( Word.WdListGall eryType.wdNumbe rGallery).ListT emplates(6).Lis tLevels(1)
    > .NumberFormat = "%1."
    > .TrailingCharac ter =
    > Word.WdTrailing Character.wdTra ilingTab
    > .NumberStyle =
    > Word.WdListNumb erStyle.wdListN umberStyleLower caseLetter
    > .NumberPosition = InchesToPoints( 0.25)
    > .Alignment =
    > Word.WdListLeve lAlignment.wdLi stLevelAlignLef t
    > .TextPosition = InchesToPoints( 0.5)
    > .TabPosition = InchesToPoints( 0.5)
    > .ResetOnHigher = 0
    > .StartAt = 1
    > .LinkedStyle = ""
    > End With
    >
    > .ListGalleries( Word.WdListGall eryType.wdNumbe rGallery).ListT emplates(6).Nam e
    > = ""
    >
    > .Selection.Rang e.ListFormat.Ap plyListTemplate (ListTemplate:= ListGalleries(
    > _
    > Word.WdListGall eryType.wdNumbe rGallery).ListT emplates(6),
    > ContinuePreviou sList:=False, ApplyTo:= _
    > Word.WdListAppl yTo.wdListApply ToWholeList,
    > DefaultListBeha vior:=Word.WdDe faultListBehavi or.wdWord9ListB ehavior)
    > .Selection.Type Text(Text:="Bla h blah blah")
    > .Selection.Type Paragraph()
    > .Selection.Type Text(Text:="Gah gah gah")
    > .Selection.Type Paragraph()
    > .Selection.Type Text(Text:="Wee wee wee")
    > End With
    > End Sub
    >
    >
    > Thanks for any assistance,
    > MW[/color]


    Comment

    • Mason Wood

      #3
      Re: Word 2000 macro -&gt; VB.Net conversion question

      Yes, you're right, I should use byref. I had forgotten I'd changed it
      when I was testing out ways to get it to work. I've tried creating the
      document objects in the procedure, tried passing them, nothing seems to
      work. VB even autocompletes while I'm typing the ListGalleries line in,
      it just comes up with that error when I'm done entering it.

      Are there any good resources out there for MS Word to VB.Net coding? A
      book, or website, or something? I've been crawing along with what
      Microsoft has for online help, but it isn't really comprehensive enough
      for me.

      Thanks again,

      MW

      *** Sent via Developersdex http://www.developersdex.com ***
      Don't just participate in USENET...get rewarded for it!

      Comment

      • Siv

        #4
        Re: Word 2000 macro -&gt; VB.Net conversion question

        Mason,

        I had a go at what you were trying to achieve as far as the renumbering bit is concerned. I have posted the application at http://www.sivill.com/Downloads/WordMacrosVBNet/ you can upload the various files and copy them to your machine and see if it works. What I have done is create a reference to the Word 10 object as that is what I have on my machine, you may need to change the reference on your machine to Office 9 to get it to work. I then create a variable that references the Word.applicatio n:

        Inherits System.Windows. Forms.Form

        Private Wd As Word.Applicatio n 'Reference the instance of Word which is added as a Project Reference.

        Public Const wdParagraph As Word.WdUnits = 4 'Constant used by the Wd.Selection.Mo veUp method - get by pressing F2 to get at the Word Objects and constants.

        Public Const wdExtend As Word.WdMovement Type = 1 'As above

        The main routine that is called by pressing the button is as follows:

        Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click

        Dim MyNewDoc As Word.Document, n As Integer

        Wd = New Word.Applicatio n

        Wd.Visible = True

        MyNewDoc = Wd.Documents.Ad d("Normal.dot ", , , True)

        For n = 1 To 5

        Wd.Selection.Ty peText("This is paragraph " & n & "." & vbCrLf)

        Next n

        Wd.Selection.Mo veUp(Unit:=wdPa ragraph, Count:=5, Extend:=wdExten d)

        Wd.Run("Temp") 'Runs a Macro called Temp in Normal.dot

        If MessageBox.Show ("OK All Done - Yes to exit and close Word Window, No to leave Word open!", "Word Macro Test App By Siv", MessageBoxButto ns.YesNo, MessageBoxIcon. Question, MessageBoxDefau ltButton.Button 1) = DialogResult.Ye s Then

        MyNewDoc.Close( False) 'Don't save document (you probably would want to in your code).

        Wd.Quit() 'Shutdown word

        Wd = Nothing 'free up memory.

        End If

        End



        'The following is what should be in your "Temp" macro this was in my Normal.dot

        'With ListGalleries(w dNumberGallery) .ListTemplates( 5).ListLevels(1 )

        ' .NumberFormat = "%1)"

        ' .TrailingCharac ter = wdTrailingTab

        ' .NumberStyle = wdListNumberSty leLowercaseLett er

        ' .NumberPosition = CentimetersToPo ints(0.63)

        ' .Alignment = wdListLevelAlig nLeft

        ' .TextPosition = CentimetersToPo ints(1.27)

        ' .TabPosition = CentimetersToPo ints(1.27)

        ' .ResetOnHigher = 0

        ' .StartAt = 1

        ' With .Font

        ' .Bold = wdUndefined

        ' .Italic = wdUndefined

        ' .StrikeThrough = wdUndefined

        ' .Subscript = wdUndefined

        ' .Superscript = wdUndefined

        ' .Shadow = wdUndefined

        ' .Outline = wdUndefined

        ' .Emboss = wdUndefined

        ' .Engrave = wdUndefined

        ' .AllCaps = wdUndefined

        ' .Hidden = wdUndefined

        ' .Underline = wdUndefined

        ' .Color = wdUndefined

        ' .Size = wdUndefined

        ' .Animation = wdUndefined

        ' .DoubleStrikeTh rough = wdUndefined

        ' .Name = ""

        ' End With

        ' .LinkedStyle = ""

        'End With

        'ListGalleries( wdNumberGallery ).ListTemplates (5).Name = ""

        'Selection.Rang e.ListFormat.Ap plyListTemplate (ListTemplate:= ListGalleries( _

        ' wdNumberGallery ).ListTemplates (5), ContinuePreviou sList:=False, ApplyTo:= _

        ' wdListApplyToWh oleList, DefaultListBeha vior:=wdWord10L istBehavior)

        'Selection.EndK ey(Unit:=wdStor y)



        End Sub

        The rem at the end of the sub is what I used as my "Temp" macro in Word in Normal.dot. If you are using a different template than "Normal.dot " you need to change the:



        "MyNewDoc = Wd.Documents.Ad d("Normal.dot ", , , True)"



        bit so that your version references "xxyy.dot" or wahetever your's is called.

        Needless to sat this code has no error trapping or anything!!

        I hope this helps,

        Siv






        "Mason Wood" <mason@codemonk ey.cc> wrote in message news:%23j3h7nzm EHA.1652@TK2MSF TNGP09.phx.gbl. ..[color=blue]
        > Yes, you're right, I should use byref. I had forgotten I'd changed it
        > when I was testing out ways to get it to work. I've tried creating the
        > document objects in the procedure, tried passing them, nothing seems to
        > work. VB even autocompletes while I'm typing the ListGalleries line in,
        > it just comes up with that error when I'm done entering it.
        >
        > Are there any good resources out there for MS Word to VB.Net coding? A
        > book, or website, or something? I've been crawing along with what
        > Microsoft has for online help, but it isn't really comprehensive enough
        > for me.
        >
        > Thanks again,
        >
        > MW
        >
        > *** Sent via Developersdex http://www.developersdex.com ***
        > Don't just participate in USENET...get rewarded for it![/color]

        Comment

        • Mason Wood

          #5
          Re: Word 2000 macro -&gt; VB.Net conversion question

          That worked beautifully, thanks a ton Siv. I didn't know how to make
          VB.Net force Word to run a macro - knowing how is an enormous help.

          I can't thank you enough,

          MW

          *** Sent via Developersdex http://www.developersdex.com ***
          Don't just participate in USENET...get rewarded for it!

          Comment

          • Siv

            #6
            Re: Word 2000 macro -&gt; VB.Net conversion question

            Glad it helped,
            I'm sure next time you'll be able to help me when I get stuck.
            Siv

            "Mason Wood" <mason@codemonk ey.cc> wrote in message
            news:%23YQorB2m EHA.1296@TK2MSF TNGP09.phx.gbl. ..[color=blue]
            > That worked beautifully, thanks a ton Siv. I didn't know how to make
            > VB.Net force Word to run a macro - knowing how is an enormous help.
            >
            > I can't thank you enough,
            >
            > MW
            >
            > *** Sent via Developersdex http://www.developersdex.com ***
            > Don't just participate in USENET...get rewarded for it![/color]


            Comment

            Working...