Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 26

Thread: Solved: Word object question

  1. #1

    Question Solved: Word object question

    I have the following which works well and I would like to add new "With objApp" based on certain condition(s).
    How I could add new "With objApp" after the closing of the first one "End with".
    Thanks.

    [vba]Option Explicit

    Private Sub Form_Load()

    Dim objApp As Word.Application
    Dim strToday
    Dim strCustomer
    Dim strReference
    Dim variable1

    Set objApp = New Word.Application

    strToday = Date
    strCustomer = "Killian"
    strReference = "Order notice"
    variable1 = "blah blah"

    Clipboard.Clear
    Clipboard.SetData Picture1.Image, vbCFBitmap

    With objApp
    .Documents.Add , , , True
    .Visible = False
    .Selection.Paste
    .Selection.TypeText Text:=Chr(3)
    .Selection.TypeText Text:=strToday & vbCrLf
    .Selection.TypeText Text:=strCustomer & vbCrLf
    .Selection.TypeText Text:=strReference & vbCrLf
    .Selection.TypeText Text:="variable1" & vbCrLf

    With .ActiveDocument.Paragraphs(1)
    .Range.Font.Size = 12
    .Range.Font.Bold = True
    .Alignment = wdAlignParagraphLeft
    End With
    With .ActiveDocument.Paragraphs(2)
    .Range.Font.Size = 12
    .Range.Font.Bold = True
    .Alignment = wdAlignParagraphRight
    End With
    With .ActiveDocument.Paragraphs(3)
    .Range.Font.Size = 12
    .Range.Font.Bold = False
    .Alignment = wdAlignParagraphLeft
    End With
    With .ActiveDocument.Paragraphs(4)
    .Range.Font.Size = 14
    .Range.Font.Bold = True
    .Alignment = wdAlignParagraphCenter
    End With
    With .ActiveDocument.Paragraphs(5)
    .Range.Font.Size = 12
    .Range.Font.Bold = False
    .Alignment = wdAlignParagraphRight
    End With
    End With

    If condition1 is True then
    With objApp
    ......................
    End With
    End if

    If condition2 is True then
    With objApp
    ......................
    End With
    End if


    objApp.ActiveDocument.SaveAs FileName:="c:\test\doc1.doc"
    Set objApp = Nothing
    End Sub[/vba]

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi,

    Your question is a little vague.

    If you wish to do something to the same document that the other code between the with block is doing then you can put your If Then Else code between the existing with block. (Theres no need to start a new one)

    Perhaps you could explain a bit more what you're trying to do?

    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  3. #3
    Sorry for not being clear.
    The format is in pair(s) for "Selection" and "With".

    [vba]Do Until adors1.EOF


    .Selection.TypeText Text:="variable1" & vbCrLf
    .Selection.TypeText Text:="variable2" & vbCrLf
    .Selection..............

    With .ActiveDocument.Paragraphs(1)
    With .ActiveDocument.Paragraphs(2)
    Wtih ..........

    adors1.MoveNext
    Loop[/vba]
    How could I apply "If' condition within a loop and in between the pair(s)?
    Thanks!

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi,

    I'm very sorry but I still have a hard time understanding what you mean.

    But if I'm reading you correct you have to run repetitions of some code?

    If so Build a function that does the repetition for you and have as optional parameters the App on which you execute and the other parameters you need.

    You can call the UDF as many times in your code as you like.

    But for a better answer I need to no exactly what you doing.
    Perhaps someone else has a idea of what your trying to do...

    Later..
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  5. #5
    The top part is a header (name address, city, country), the second (with condition) is a flexible part that is based on the item(s) the customer orders. The condition put is based on the number of item(s) required ( one to thousands items therefore it probably turns out more than one page). ie. the same header could have more than one Word document to accomodate all the item(s) needed.

    Thanks and apologize for not being detailed.

  6. #6
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Hi,

    Please don't apologize sometimes people just don't understand each other..This is one of those times.

    I hope someone comes by who does get the picture..I'll leave it at this because this isn't helping you that much!
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  7. #7
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    OK, first of all, I am also trying to figure out exactly what is happening. So I am going to tightend up your code. I first have to comment that it is NOT a good idea to leave your declaration as undefined data types. ALL your variables are Variants.

    Further, normally a variable starting with "str" indicates a String Data type. Dates are normally indicated by dte - so dteToday is recognized by others as a Date data-type. For information purposes, here are the byte quantity assigned for each data type.

    Byte 1 byte
    Boolean 2 bytes
    Integer 2 bytes
    Long (long integer) 4 bytes
    Single (single-precision floating-point) 4 bytes
    Double (double-precision floating-point) 8 bytes
    Currency (scaled integer) 8 bytes
    Decimal 14 bytes
    Date 8 bytes
    Object 4 bytes
    String (variable-length) 10 bytes + string length
    String (fixed-length) Length of string
    Variant (with numbers) 16 bytes
    Variant (with characters) 22 bytes

    Total memory address assigned = 88 , total required = 36.

    [vba]Option Explicit

    Private Sub Form_Load()

    Dim objApp As Word.Application
    Dim strToday (22 bytes assigned - Date type w/b 8)
    Dim strCustomer (22 bytes assigned - String type w/b 7 bytes)
    Dim strReference (22 bytes assigned = 12 bytes needed)
    Dim variable1 (Also a String - 22 bytes asigned, 9 needed)

    Set objApp = New Word.Application

    strToday = Date
    strCustomer = "Killian"
    strReference = "Order notice"
    variable1 = "blah blah"

    Clipboard.Clear
    Clipboard.SetData Picture1.Image, vbCFBitmap

    With objApp
    .Documents.Add , , , True
    .Visible = False
    With .Selection
    .Paste
    .TypeText Text:=Chr(3) & strToday & vbCrLf & _
    strCustomer & vbCrLf & _
    strReference & vbCrLf & -
    "variable1" & vbCrLf
    end with

    With .ActiveDocument.Paragraphs(1)
    .Range.Font.Size = 12
    .Range.Font.Bold = True
    .Alignment = wdAlignParagraphLeft
    End With
    With .ActiveDocument.Paragraphs(2)
    .Range.Font.Size = 12
    .Range.Font.Bold = True
    .Alignment = wdAlignParagraphRight
    End With
    With .ActiveDocument.Paragraphs(3)
    .Range.Font.Size = 12
    .Range.Font.Bold = False
    .Alignment = wdAlignParagraphLeft
    End With
    With .ActiveDocument.Paragraphs(4)
    .Range.Font.Size = 14
    .Range.Font.Bold = True
    .Alignment = wdAlignParagraphCenter
    End With
    With .ActiveDocument.Paragraphs(5)
    .Range.Font.Size = 12
    .Range.Font.Bold = False
    .Alignment = wdAlignParagraphRight
    End With
    End With

    If condition1 is True then
    With objApp
    ......................
    End With
    End if


    If condition2 is True then
    With objApp
    ......................
    End With
    End if


    objApp.ActiveDocument.SaveAs FileName:="c:\test\doc1.doc"
    Set objApp = Nothing
    End Sub[/vba]

    OK, that being said, you could please clarify in better detail again what you are doing?

    You mention "header", but there is no action on a header object. You state"the same header could have more than one Word document to accomodate all the item(s) needed." I am quite confused by what you mean by the same header could have more than one Word document. Huh? You are putting whiole Word documents into the Header????? Thise seems very very odd.

  8. #8
    Thank you fumei for your help cause I scare MOS Master for NOT being clear.
    Your advice is good. The variables used are just examples, String variables should be declared as String, date .....as Date....
    I have two tables which are Customers and Orders. (just be simple in explanation)
    I would like to create Word document(s) the orders that customers place.
    My final documents' format would be -

    Customer1
    Address1

    Re: order #xxxxxx

    Sku1 Qty1 ....
    Sku2 Qty2 ....

    I have placed a loop to go through the database, and a condition to print only the "Sku Qty" line(s) if the order is the same, if not print header. The "header" means the address part.

    I just do not how to put condition within the "With objApp End With" to selectively print with " With .Selection and With .ActiveDocument.Paragraphs(x) ".
    Hopefully it is clearer. I have tried to put two "With objapp" but system does not like it.
    Thanks to all.





    .Se

  9. #9
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Quote Originally Posted by Mr Doubtfire
    I have placed a loop to go through the database, and a condition to print only the "Sku Qty" line(s) if the order is the same, if not print header. The "header" means the address part.

    I just do not how to put condition within the "With objApp End With" to selectively print with " With .Selection and With .ActiveDocument.Paragraphs(x) ".
    I think you must take greater care with your terms. Now you are using the word "print". You state "print the SKU line if the order is the same", if not, print header - which for some VERY strange reason means the address.

    Why on earth do you refer to the address as "header'?

    In any case, I am still not getting what you are trying to do. I am positive we can help with this...but I am not sure what "this" is!

    You want to do something With the Selection, and something With paragraphs.

    Please try again and explain exactly what you are trying to do, what it is you want to happen.

    I can see you are trying to do some conditional logic on something, but I can not quite get exactly what, or where. Walk it through step by step.

    And please clarify "selectively print ". Are you really printing???

  10. #10
    Thank you for the patience.
    "Print" does not mean going to printer. It means the content going to the Word document.
    It might better to give an example. (This example assumes the Word document has ten lines only)

    I have the format -

    Customer1
    Address1

    Re: Order #xxxxx

    Sku1 Qty1
    Sku2 Qty2
    ...

    Sku100 Qty100

    Thank you!

    From the above example, the part of the "Sku.... Qty" varies from order to order. Therefore some orders might form more than one page, which has the same Customer information but different "Sku ....Qty".
    Programatically, I use a "Do until .............................Loop" to go through the database for all the orders and customers' info.
    The problem I have is when the maximum lines of a page reaches its max per page I have to go to the second, third. .............last page of documents although the customer's info is the same, until the next order comes in place I would start a fresh document. Otherwise the same order would store its own contents NO matter how many skus are there.
    Hopefully, by now you get what I am saying.

    Thank you again for the time.

  11. #11
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Quote Originally Posted by Mr Doubtfire
    The problem I have is when the maximum lines of a page reaches its max per page I have to go to the second, third. .............last page of documents although the customer's info is the same, until the next order comes in place I would start a fresh document. Otherwise the same order would store its own contents NO matter how many skus are there.
    Hopefully, by now you get what I am saying.

    Thank you again for the time.
    Sorry, but you are still not being clear.

    I asked, a second time, for you to walk through what you want to happen, step by step. Your last post does not do that. It does not walk through step by step. It does not clearly state what you want to happen. I hope I am being clear myself.

    So, again. Walk it through step by step. State what it is you want to happen.

    Let's use your "example" above.

    1. There are a enough lines of "Sku....Qty", so it goes to another page. OK...so? Why is this a problem? maybe it is a problem for you...but you do NOT state what the problem is. You do NOT state what it is you want to happen.

    2. "the next order comes in place I would start a fresh document" What does this mean? Why is this a problem? It makes sense to me that the next order would be in a fresh document. Unless you want the order for a specific customer to be appended to a previous document. If so, you have never stated this.

    3. "Otherwise the same order would store its own contents NO matter how many skus are there." I have no clue whatsoever what you mean by this. Are you saying that an order with 4 SKU should be treated differently from an order with 90 SKU? Why is the number of SKU significant? Maybe it is for you....but you do NOT state why that is, or what you want to happen if it IS a large number of SKU. What is the cutoff. If there are 49 SKU...do you want it to process differently from an order with 50 SKU? "NO matter how many skus are there" implies that the number of sku means something....but you do NOT state what that something means, or what you want to happen.

    REPEAT!!!! State what you want to happen, step by step.

    Step by step. If a specific customer order is to go to a specific customer document....say so.

    We can not help unless you tell what is happening, what you want to happen, and what those things are in a logical step by step process.

    Again, I am positive that there is a (probably) easy solution to this...what ever it is. However, I am still not clear what the problem is. Or what you want to happen.

    Be patient. Perhaps write it down first. This is always a good idea. Think about what EXACTLY, no fuzzy assumptions...we can not read minds. You must state clearly EXACTLY what you want to happen.

    Sorry, but - "Otherwise the same order would store its own contents NO matter how many skus are there." is not clear.

    Same order? - Does that means there is a situation that you could get an order twice? Three times? During the same processing? At a later date? Do you need to check existing document to see if it is the same order?

    "Own contents"...Huh? I have no idea what you mean by that.

    Then there is the number of skus...my comments are above.

    Would like to help. Try again.

  12. #12
    Other than THANK YOU, nothing I could express myself for your patience.
    Hopefully the following is simple.

    I have the format for a Word document with ten lines ONLY (keep simple) -

    Customer1
    Address1

    Re: Order #xxxxx

    Sku1 Qty1
    Sku2 Qty2

    Thank you!

    From the above I do not have problem to create a document from the database as long as the number of skus ordered is always kept to two or one.
    My question is the maximum of lines per document is ten ONLY.
    When the number of skus ordered by the customer exceeds 2, then I need to go the second page (with same customer information).
    I have the following two pages Word doc.

    Page 1

    Customer1
    Address1

    Re: Order #12345

    Sku1 Qty1
    Sku2 Qty2

    Thank you!

    Page 2

    Customer1
    Address1

    Re: Order #12345

    Sku3 Qty3
    Sku4 Qty4

    Thank you!

    The above is what I want working with a "Word.Application" object, the same document in mulitple pages for the same order.
    How to put condition with that "objapp" AND within the database loop?
    Thx!

  13. #13
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    OK, first off, i will rant, then we can start to work on a solution.

    Your post is finally giving details that are....hmmmmm. rather important.

    10 lines per page - never mentioned before, or rather a vague thing of "ten lines only"...this does not mention that you need 10 lines only PER PAGE - rather critical point.
    2 SKU per page? - never mentioned before, and rather critical
    A REPEAT of customer information - never mentioned before, and absolutely critical.

    Do you see what I mean? You have to tell us EXACTLY what the situation is and what you want to happen. We are not mind readers.

    OK. I am not a database person much at all, but I do know Word. So I will concentrate on that.

    1. Start with the data. Pick up the data and dump it into public variables...maybe you could use private variables...have to check for that.

    3. Build arrays of the data, customer, address, and have a counter of the number of SKU determined for that customer. This counter is built up BEFORE doing anything with the document.

    4. Alternately (and I think this is preferable), have the text "Customer" and Addres - in other words the repetative stuff as AutoText. Say, "Customer" is an AutoText fired by the text "cus", and "Address" is fired by an AutoText ""adr".

    5. OK, say the counter is 13...there are 13 SKUs involved. Divide by 2. 6.5. What does that mean> You need 7 pages.

    (By the way, this seems to be poor document design...but that is another issue....)

    6. The counter is an declared integer named SKUCount. There is a declared variable of var

    [vba]Dim SKUCount() As Integer
    Dim i As Integer
    Dim var

    ReDim Preserve SKUCount(i)
    'skucount(i) = ...from database
    i = i + 1

    ' reset i
    i = 0
    On Error Resume Next
    for var = 1 to SKUCount
    With ActiveDocument.Range
    .Text = "cus"
    .InsertAutoText
    .Text = vbCrLf
    .Text = "adr"
    .InsertAutoText
    .Text = vbCrLf
    .Text = "SKU " & SKUCount(i)
    ' etc etc until you have completed that page
    .InsertBreak Type:=wdPageBreak
    i = i + 1
    End With
    Next[/vba]

    Something like that.

    With the autotext you can insert the text by code and complete it by code.

    The count of the loop is based on the number of SKU chosen, divided by 2.

    This is not complete but I have to run right now. Hopefully this may help to get you rolling. You loop based on a counter of the SKU.

  14. #14
    Thank you again.

    One of my problem is to put fonts (size, bold, italic....).
    How could it plug them in (between/among) the condition.
    Thanks!

  15. #15
    Sorry, I forget to ask you where I should insert the "Word.application" and "Documents.add" in that loop?
    Thhhhhhhhanks!!

  16. #16
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Back up! Back up!

    Whoa, wait a second. This is bouncing out of control.

    Uh, if you are asking about how to place Word.Application, and Document.Add....you are starting at the beginning; you are starting to ask about creaing a document in the first place.

    Let's start again.

    What are you trying to do, and when are you trying to do it?

    Word.Application, in the loop????? No, no, you are are going to make an instance, or USE an instance of Word once. You are not going to loop it!

    You are not going to loop adding documents either. Unless that is what you want to do. I am still not clear.

    On one hand it seems like you are trying to automate information from the database - on the other you seem to have user input.

    The SKUCount routine simply figured out what to do with the document based on the number of SKU chosen. Previously it seemed that this was one document, with multiple orders. Is it supposed to make individual files?

    Remember you can work with the SAME Word.Application object to create, write to, and save a number of documents. I think you are perhaps confusing the application object with a document object.

    Please try and walk it through again.

    The user makes their choices on a form, and clicks a button MakeDocument. This is ONE event. This event can be made to take the information on the form and format it however you want (really, just about any way you want). It can then write and save a document file.

    If this document making command does JUST THAT, then the form is still active, and when finished the user has still got the form. They go to the next recordand choose the number of SKU.

    When they are done, they click a Finish button and they are done, the form unloads.

    Does the user choose the record, or are you looping through the records? I am getting a headache, but here is what I think is happening.

    You have a form that displays database records. Number of which...I don't care. Say it is 10.

    1. Form loads.
    2. Word instance created.
    3. Record 1 is displayed on your form. Also on the form, are user choices of SKU.
    4. User chooses X number of SKU.
    5. User clicks a command to create the document.
    6. Routine figures out, based on the number of SKU how to write the document.
    7. Document is created (Add.New)
    8. Document is written
    9. Document is saved.
    8. Control is passed back to form, for the user to deal with the next record.

    Correct?

    If you are having user intervention for each record, then control has to be passed back and forth. Will EVERY record have a file written for it? If so, then yes, you could add that as a loop. But it would use the same objApp.

  17. #17
    With the many questions and hints I got from you, I finally got what I want.

    Thanks!
    But the only thing I do not get is

    Customer1
    Add1

    Re:Order confirmation xxxxxxxxxxxxx

    Sku1 Qty1
    Sku2 Qty2

    Thank you!

    On the first page the "Re:Order confirmation..." is aligned centrally. And on the second page it is "Left"-aligned.
    This is the only thing I could not get. Others are working perfectly in both retrieving and saving.
    Thank you! for the patienceeeeeeeeeeeeeeeeeeeeee........

  18. #18
    I have the following code further to my previous sample reply.

    [VBA]
    Option Explicit
    Dim strOrdernumber As String
    Dim strPREVIOUSOrdernumber As String
    Dim strCustomer As String
    Dim strAddress1 As String
    Dim strSKU As String
    Dim intQTY As Integer
    Dim strSTATUS As String
    Dim strPO As String
    Dim strReference As String
    Dim intSKUcount1 As Integer
    Dim intTOTALSKUcount1 As Integer
    Dim blnFRESHDocument As Boolean
    Dim objApp As Word.Application
    Private Sub Form_Load()

    Dim strquerySelect, strqueryFrom, strqueryWhere, strqueryOrderby, strQuery
    Dim adoconn1 As New ADODB.Connection
    Dim adors1 As New Recordset
    adoconn1.Open "DSN=dsn1"
    strquerySelect = "SELECT * "
    strqueryFrom = "FROM (.......) "
    strqueryWhere = "WHERE ............... "
    strqueryOrderby = "ORDER BY .....; "
    strQuery = strquerySelect & strqueryFrom & strqueryWhere & strqueryOrderby
    adors1.Open strQuery, adoconn1, adOpenDynamic, adLockReadOnly, adCmdText
    strReference = "ORDER CONFIRMATION"
    intSKUcount1 = 0
    blnFRESHDocument = True
    intTOTALSKUcount1 = 0

    Do Until adors1.EOF
    erase_header_variables
    erase_SKU_variables
    strOrdernumber = adors1("ordernum")
    strCustomer = adors1("customer")
    strAddress1 = adors1("address1")
    strPO = adors1("ponum")
    strSKU = adors1("item")
    intQTY = adors1("qty")
    strSTATUS = "good"
    'HEADER
    If strPREVIOUSOrdernumber = "" Or strOrdernumber <> strPREVIOUSOrdernumber Then
    If strPREVIOUSOrdernumber <> "" Then
    'END
    With objApp
    .Selection.TypeText Text:=vbCrLf
    .Selection.TypeText Text:="Thank you!"

    With .ActiveDocument.Paragraphs(1)
    .Range.Font.Size = 12
    .Range.Font.Bold = True
    .Alignment = wdAlignParagraphLeft
    End With
    With .ActiveDocument.Paragraphs(2)
    .Range.Font.Size = 12
    .Range.Font.Bold = True
    .Alignment = wdAlignParagraphLeft
    End With
    End With
    intSKUcount1 = 0
    objApp.ActiveDocument.SaveAs FileName:="c:\test\" & strPREVIOUSOrdernumber & ".doc"
    objApp.Quit False
    Set objApp = Nothing
    End If
    Set objApp = New Word.Application
    With objApp
    .Documents.Add , , , True
    .Visible = False
    End With

    blnFRESHDocument = True
    Else
    If intSKUcount1 = 2 Then
    With objApp
    .Selection.InsertBreak Type:=wdPageBreak
    End With
    intSKUcount1 = 0
    intPAGEPerorder = intPAGEPerorder + 1
    blnFRESHDocument = True
    End If
    End If

    If blnFRESHDocument = True Then

    With objApp
    .ActiveDocument.PageSetup.TopMargin = CentimetersToPoints(1)
    .Selection.TypeText Text:=strCustomer & vbCrLf
    .Selection.TypeText Text:=strAddress1 & vbCrLf
    .Selection.TypeText Text:=vbCrLf
    .Selection.TypeText Text:=strReference & " : " & strPO & vbCrLf

    With .ActiveDocument.Paragraphs(1)
    .Range.Font.Size = 12
    .Range.Font.Bold = True
    .Alignment = wdAlignParagraphLeft
    End With
    With .ActiveDocument.Paragraphs(2)
    .Range.Font.Size = 12
    .Range.Font.Bold = True
    .Alignment = wdAlignParagraphLeft
    End With
    With .ActiveDocument.Paragraphs(3)
    .Range.Font.Size = 12
    .Range.Font.Bold = True
    .Alignment = wdAlignParagraphLeft
    End With
    With .ActiveDocument.Paragraphs(4)
    .Range.Font.Size = 12
    .Range.Font.Bold = True
    .Alignment = wdAlignParagraphCenter
    End With
    End With
    blnFRESHDocument = False
    End If

    'CONTENT
    With objApp
    .Selection.TypeText Text:=" ITEM QUANTITY STATUS" & vbCrLf
    .Selection.TypeText Text:=strSKU & " " & intQTY & " " & vbCrLf

    With .ActiveDocument.Paragraphs(5)
    .Range.Font.Size = 12
    .Range.Font.Bold = True
    .Alignment = wdAlignParagraphLeft
    End With
    With .ActiveDocument.Paragraphs(6)
    .Range.Font.Size = 12
    .Range.Font.Bold = True
    .Alignment = wdAlignParagraphLeft
    End With
    End With

    intSKUcount1 = intSKUcount1 + 1
    intTOTALSKUcount1 = intTOTALSKUcount1 + 1
    strPREVIOUSOrdernumber = strOrdernumber
    adors1.MoveNext

    Loop
    If intTOTALSKUcount1 > 0 Then
    With objApp
    .Selection.TypeText Text:=vbCrLf
    .Selection.TypeText Text:="Thank you!"

    With .ActiveDocument.Paragraphs(1)
    .Range.Font.Size = 12
    .Range.Font.Bold = True
    .Alignment = wdAlignParagraphLeft
    End With
    With .ActiveDocument.Paragraphs(2)
    .Range.Font.Size = 12
    .Range.Font.Bold = True
    .Alignment = wdAlignParagraphLeft
    End With
    End With
    objApp.ActiveDocument.SaveAs FileName:="c:\test\" & strPREVIOUSOrdernumber & ".doc"
    objApp.Quit False
    Set objApp = Nothing
    End If
    Set adors1 = Nothing
    Set adoconn1 = Nothing
    End Sub
    Function erase_header_variables()
    strOrdernumber = ""
    strCustomer = ""
    strAddress1 = ""
    strPO = ""

    End Function
    Function erase_SKU_variables()
    strSKU = ""
    intQTY = 0
    strSTATUS = ""

    End Function

    [/VBA]

    The above example is supposed to print only two skus per page (keep simple). Therefore, for every two skus one page..eg. five skus has three pages...
    The only thing that does not work properly is the reference "Re: Order confirmation ............." which is supposed to be center-alligned is correctly alligned on the first page, BUT not after the first page (where applied).
    Please comment.
    Thanks.

  19. #19
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Aaaaaacccckkkkkk! First off....

    Please use the underscore character to truncated your lines of code better. Having the code stretch out across the screen makes things difficult to read. For example, rather than making one long line (thus stretching the screen so we have to scroll...)

    [vba]If strPREVIOUSOrdernumber = "" _
    Or strOrdernumber <> strPREVIOUSOrdernumber Then [/vba]
    OR:
    [vba]objApp.ActiveDocument.SaveAs _
    FileName:="c:\test\" & _
    strPREVIOUSOrdernumber & ".doc" [/vba]

    It makes itmuch easier for the rest of us. Thank you.

    2. What is with:
    Selection.TypeText Text:=" ITEM QUANTITY STATUS" & vbCrLf

    ????? Inserting text with lots of spaces like is bad form. Hard coding spaces like that is poor design, and can easily get skewed out of whack. Design, and set up a table, and use it.

    Further, if you used a properly dsigned table, INCLUDING Styles to fit the text within the table, there would be no issue with paragraph alignment. That is what Styles are for.

    Regarding for alignment...well, that is what you get when you hard code pargraph formats. Use Styles. Have one style for Order#_FirstPage, another for Order#_OtherPages.

    I look at:
    [vba]With .ActiveDocument.Paragraphs(5)
    .Range.Font.Size = 12
    .Range.Font.Bold = True
    .Alignment = wdAlignParagraphLeft
    End With [/vba]
    and I know nothing. I have no idea what the fifth paragraph is, and if I understand correctly, depending on the number of SKU, paragraph 5 could, in fact be different.

    Use Styles!

    3. You have a comment ' HEADER. This is still confusing for me. Do you mean a heading? As in, text at the top of a list? Again, use a table.

    4. It would really really help if you commented your code, stating what you think is happening. Obviously, you are not totally inexperienced, but it would make things easier to point out improvements, if you commented your code. Even the obvious stuff.

    For example you have:
    [vba] intTOTALSKUcount1 = intTOTALSKUcount1 + 1
    strPREVIOUSOrdernumber = strOrdernumber
    adors1.MoveNext

    Loop
    If intTOTALSKUcount1 > 0 Then[/vba]
    I am not clear on how intTOTALSKUcount1 could EVER be 0. If you have intTOTALSKUcount1 = intTOTALSKUcount1 + 1 , how can If intTOTALSKUcount1 > 0 ever be false?. it will always be true. Perhaps if you commented more it would be clearer what is going on.

    You asked specifically about Order # alignment on page 2. I can't tell, because you are hardcoding paragraph formats. Is it #5, #6, # 2?

    5. I do not understand why you are doing:
    [vba]objApp.Quit = False
    objApp = Nothing[/vba]
    You are NOT quitting the application, butyou ARE killing the instance of it. Then you, of course, have to make another instance with Set objApp = New Word.Application.

    You do not have to do this.

  20. #20
    Thank you for the comments.
    1. I would use "_" for long statements.
    2. The first four paragraph are four the "Header"/"Heading" - customer name, address....and empty lines.. That's why I use Paragraph(5) for the first line of "Content" - Sku, qty, status. "Header" means order header information.
    4. intTOTALSKUcount1 COULD be zero when there is nothing to create - NO orders at all. I would add comments in the future.
    "hardcoding paragraph" - Please tell me how NOT to hardcode still with styles and fonts.
    5.
    objApp.Quit = False
    objApp = Nothing
    I have put them there just because without them I found out the Word file(s) were still opened.

    I am here to learn from Everyone.
    Please comment!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •