Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create a word document, sort is not ok 1

Status
Not open for further replies.

myrgir

Programmer
Feb 7, 2006
62
CA
Hi
I'm trying to create a word document and put text in it. but it seems like it print the end at the beginning and the beginning at the end.
This is my example:

Code:
Private Sub PrintTest()
        Dim StringDv As DataView
        Dim StringFilter As String
        Dim StringDr As DataRowView
        Dim wrdtest As Word.Application

        wrd = New Word.Application
        wrd.Visible = False

        doctest = wrd.Documents.Add()
        rngtest = doctest.Range()

        StringDv = m_DataLayer.DsString._String.DefaultView
        StringFilter = ""
        StringDv.RowFilter = StringFilter
        StringDv.Sort = "No"

        For Each StringDr In StringDv
            Select Case StringDr("No")
                Case "700" '1
                    TopicNumberTest("1")
                Case "701" '2
                    TopicNumberTest("2")
                Case "702" '3
                    TopicNumberTest("3")
            End Select
        Next
        DocTest.SaveAs(Path & "\" & "Test.doc")

        wrd.Quit()
        wrd = Nothing
    End Sub

Private Sub TopicNumberTest(ByVal sText As String)
        rngtest = doctest.Range(Start:=0, End:=0)
        With rngtest
            .InsertAfter(Text:=sText)
            With .Font
                .Name = "Arial"
                .Size = 12
                .Bold = True
            End With
        End With
    End Sub
For this example, it prints:
3
2
1
Can anyone tell me what I'm doing wrong?
Thanks
 
OK, Now I posted it in the vb.net forum but they told me to post it here.

So now I don't have the order issue anymore as I took off this line in "TopicNumberTest"
"rngtest = doctest.Range(Start:=0, End:=0)"
But I don't have my Bold anymore.
Can anyone tell me why?
 
First off, you have
Code:
Dim wrdtest As Word.Application
wrd = New Word.Application
I presume this is a typo. One, or the other, is correct. wrdtest is never used, and wrd is never declared. Are you using Option Explicit?

You are inserting text as InsertAfter. So assuming the text is inserted:

1, 2, 3

that comes out 3, 2, 1. The 1 is inserted first, followed by 2, followed by 3. 1 then 2 (so now 21), then 3 (so now 321).

As for the Bold. Explain please. My text comes out bold.

Gerry
 
First
Code:
wrd = New Word.Application
wrd.Visible = False
The wrd is a public variable. Of course it shouldn't be there. Thanks for that.

Now my sort is ok, I put this line in commentary
Code:
rngtest = doctest.Range(Start:=0, End:=0)

I have many procedure like "TopicNumberTest". Some put my text in bold, some not, some put a blank line (.InsertParagraphAfter()) after the text, and so one.

My problem is that I don't see anymore text in bold since I took off the line
Code:
rngtest = doctest.Range(Start:=0, End:=0)
I would like to have my Bold Text back.

Thanks
 
Private Sub TopicNumberTest(ByVal sText As String)
With wrd.Selection
With .Font
.Name = "Arial"
.Size = 12
.Bold = True
End With
.TypeText Text:=sText
End With
End Sub

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
well, it seems like it doesn't work.
I don't have bold in my document :-/
Any other ideas?
 
Well....PHV's code will definitely work. If it is not working there is something very very wonky here. PH's code is using the Selection object. It turns Bold on, and actually types text. There is no way that will not make the text typed not bold.

Are you saying you have tried PH's code, and it is NOT typing bold text?

I'm sorry, but I run the following - using your original Range object, rather than PH's Selection....and it has the text bold.
Code:
Sub Test()
TopicNumberTest "bob"
End Sub

Sub TopicNumberTest(ByVal sText As String)
Dim rngtest As Range
Set rngtest = ActiveDocument.Range(Start:=0, End:=0)
        With rngtest
            .InsertAfter Text:=sText
            With .Font
                .Name = "Arial"
                .Size = 12
                .Bold = True
            End With
        End With
Set rngtest = Nothing
End Sub
You imply that it worked before. OK. What have you changed?

Gerry
 
fumei
When I execute exacly your code I got an error on this line "rngtest = ActiveDocument.Range(Start:=0, End:=0)"
The error I have is "Object reference not set to an instance of an object."

The only things that I change is I replace the "ActiveDocument" for "doctest" that is declare as Word.Document

I change also this line:
Dim rngtest As Range for
Dim rngtest As Word.Range
as it said that Type Range is not defined.

so now this is my code
Code:
Sub Test()
        TopicNumberTest("bob")
    End Sub

    Sub TopicNumberTest(ByVal sText As String)
        Dim rngtest As Word.Range
        rngtest = doctest.Range(Start:=0, End:=0)
        With rngtest
            .InsertAfter(Text:=sText)
            With .Font
                .Name = "Arial"
                .Size = 12
                .Bold = True
            End With
        End With
        rngtest = Nothing
    End Sub

It works before I want to change the sort of my StringDv.
It was printing the end first and then it finish by the first point.
so It was printing
1
2
3
but the StringDv.Sort was desc. I wanted to print the first point at the beginning like it normally should. To make this work I took off the line "rngtest = doctest.Range(Start:=0, End:=0)" and change the InsertBefore to InsertAfter.
The output is the same but the order that it puts the text in my document is not as I wanted it.

Now the sort is ok. but since I took of the line with the Range I don't see my bold.

I don't know if it is important but I call other Procedure like TopicNumberTest but without Bold. Because I need some text in bold but some text without bold
Here is my other Procedure
Code:
Private Sub SimpleNumberMaster(ByVal sText As String)
      With rngMaster
          .InsertAfter(Text:=sText)
          .InsertParagraphAfter()
          .InsertParagraphAfter()
          With .Font
            .Name = "Arial"
            .Size = 12
            .Bold = False
          End With
     End With
End Sub
If you need more explanation please just ask me
Thanks
 
OK now look what I've done
Code:
 Sub Test()
        wrdtest = New Word.Application
        doctest = wrdtest.Documents.Add()
        rngtest = doctest.Range()
        TopicNumberTest("bob")
        TopicNumberWithoutBoldTest("Bob2")
        doctest.SaveAs(Path & "\" & "Test3.doc")
    End Sub

    Sub TopicNumberTest(ByVal sText As String)

        rngtest = doctest.Range(Start:=0, End:=0)
        With rngtest
            .InsertBefore(Text:=sText)
            With .Font
                .Name = "Arial"
                .Size = 12
                .Bold = True
            End With
        End With
        rngtest = Nothing
    End Sub

    Sub TopicNumberWithoutBoldTest(ByVal sText As String)

        rngtest = doctest.Range(Start:=0, End:=0)
        With rngtest
            .InsertBefore(Text:=sText)
            With .Font
                .Name = "Arial"
                .Size = 12
                .Bold = False
            End With
        End With
        rngtest = Nothing
    End Sub

it prints "Bob2bob"
I tried to replace InsertBefore by InsertAfter, it doesn't change anything.
Can someone tell me what's wrong?
 
OK now look what I've done
Code:
 Sub Test()
        wrdtest = New Word.Application
        doctest = wrdtest.Documents.Add()
        rngtest = doctest.Range()
        TopicNumberTest("bob")
        TopicNumberWithoutBoldTest("Bob2")
        doctest.SaveAs(Path & "\" & "Test3.doc")
    End Sub

    Sub TopicNumberTest(ByVal sText As String)

        rngtest = doctest.Range(Start:=0, End:=0)
        With rngtest
            .InsertBefore(Text:=sText)
            With .Font
                .Name = "Arial"
                .Size = 12
                .Bold = True
            End With
        End With
        rngtest = Nothing
    End Sub

    Sub TopicNumberWithoutBoldTest(ByVal sText As String)

        rngtest = doctest.Range(Start:=0, End:=0)
        With rngtest
            .InsertBefore(Text:=sText)
            With .Font
                .Name = "Arial"
                .Size = 12
                .Bold = False
            End With
        End With
        rngtest = Nothing
    End Sub

it prints "Bob2bob"
I tried to replace InsertBefore by InsertAfter, it doesn't change anything.
Can someone tell me what's wrong?
 
ok
I got it,
I don't know what I did wrong yesterday but today this is working.
PHV your code was right. Sorry about that
so here is my code
Code:
Private Sub SimpleBoldNumberMaster(ByVal sText As String)
        With wrd.Selection
            With .Font
                .Name = "Arial"
                .Size = 12
                .Bold = True
            End With
            .TypeText(Text:=sText)
            .TypeParagraph()
            .TypeParagraph()
        End With
End Sub

Private Sub SimpleWithoutInsertNumberMaster(ByVal sText As String)
        With wrd.Selection
            With .Font
                .Name = "Arial"
                .Size = 12
                .Bold = False
            End With
            .TypeText(Text:=sText)
        End With
End Sub

For Each StringDr In StringDv
      Select Case StringDr("No")
          Case "700" '1.0
              SimpleBoldNumberMaster(StringDr("Message"))
          Case "701" '1.1
              SimpleWithoutInsertNumberMaster(StringDr("Message"))
          ...
      End Case
Next

Thank you all for your help!
 
Again - PH's code uses the Selection object. So it in essense writing "directly" into the document. It is kind of like using the cursor. There is interaction with the UI (user interface).

Your code was using a Range object. It can be done with the Range. You need to understand how a Range works with InsertBefore, or InsertAfter.

But hey, if you have it working...good.

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top