Results 1 to 9 of 9

Thread: [RESOLVED] Easy Yes/No Newbie Questions: Can I do this?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Location
    Virginia, USA
    Posts
    17

    Resolved [RESOLVED] Easy Yes/No Newbie Questions: Can I do this?

    Hi

    I am new to VB... and before I start "barking up the wrong tree", I want to know if the following can be done when generating a Word document from Excel:

    *Convert data from Excel into a Word document formating multi-level headers (e.g. 1.1.1 Level 3 Header)? (Haven't figured out how to do this one yet... only know how to get "1. Subject" and not "1.1.1 Subject")
    *Create a header/footer? I believe the answer is yes.
    *Add a table of contents with the data in Excel (and, yes, those multi-level headers)? I believe the answer is yes.
    *Take cell data that has the path and filename of BMP images which would then be used to insert/link those images into the Word document? Not sure if this can be done... but I don't see why not.

    I'm sure the first three can be done... but that last one... I'm not sure about.

    Thanks!

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Easy Yes/No Newbie Questions: Can I do this?

    Assuming that you are using VBA within Excel, it's all possible

    You can even get some help on writing the code to do the things you want by "asking" Word, as you can perform the tasks you want manually while recording a Macro, and it will write VBA code for you.

    Admittedly you will need to modify the code a bit (especially for the bit about images), but it will point you in the right direction.

    If you record macros to do what you want, you can post the code for them here (along with your existing code) and we can help you to merge it in

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Location
    Virginia, USA
    Posts
    17

    Re: Easy Yes/No Newbie Questions: Can I do this?

    Oh wow, now there is a smart idea!

    Let me try that!



    And Yes, I am using VBA within Excel.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Location
    Virginia, USA
    Posts
    17

    Re: Easy Yes/No Newbie Questions: Can I do this?

    Okay, so I'm working on the "1.1.X" header.

    I create my header here:

    VB Code:
    1. Set oHeader1 = objDoc.Content.Paragraphs.Add
    2.             With oHeader1
    3.                 .Range.Text = STR_IOS_PAGE_NAME
    4.                 .Range.Font.AllCaps = True
    5.                 .Range.Underline = wdUnderlineThick
    6.                 .Range.ParagraphFormat.KeepWithNext = True
    7.                 .Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
    8.                 .Range.Style = "Heading 3"
    9.  
    10.                 .Range.ListFormat.ApplyNumberDefault  'e.g. 1.
    11.            
    12.                 .Range.Font.Bold = True
    13.                 .Format.SpaceAfter = 24    '24 pt spacing after paragraph.
    14.                 .Range.InsertParagraphAfter
    15.        
    16.                 .Range.Font.AllCaps = False
    17.                 .Range.ParagraphFormat.Alignment = wdAlignParagraphLeft
    18.                 .Range.ParagraphFormat.KeepWithNext = False
    19.             End With

    But then I took the VB code from Word here (I'm not taking all of it, just the one I need):

    VB Code:
    1. With ListGalleries(wdOutlineNumberGallery).ListTemplates(5).ListLevels(1)
    2.             'I only care about List Level 1 since Normal Text data will come after this.
    3.             'And, NumberFormat will be hardcoded since it will be merged into another larger document
    4.             ' at section 3.1
    5.                 .NumberFormat = "3.1.%1"
    6.                 .TrailingCharacter = wdTrailingTab
    7.                 .NumberStyle = wdListNumberStyleArabic
    8.                 .NumberPosition = InchesToPoints(0)
    9.                 .Alignment = wdListLevelAlignLeft
    10.                 .TextPosition = InchesToPoints(0.3)
    11.                 .TabPosition = InchesToPoints(0.3)
    12.                 .ResetOnHigher = 0
    13.                 .StartAt = 1
    14.                 With .Font
    15.                     .Bold = wdUndefined
    16.                     .Italic = wdUndefined
    17.                     .Strikethrough = wdUndefined
    18.                     .Subscript = wdUndefined
    19.                     .Superscript = wdUndefined
    20.                     .Shadow = wdUndefined
    21.                     .Outline = wdUndefined
    22.                     .Emboss = wdUndefined
    23.                     .Engrave = wdUndefined
    24.                     .AllCaps = wdUndefined
    25.                     .Hidden = wdUndefined
    26.                     .Underline = wdUndefined
    27.                     .Color = wdUndefined
    28.                     .Size = wdUndefined
    29.                     .Animation = wdUndefined
    30.                     .DoubleStrikeThrough = wdUndefined
    31.                     .Name = ""
    32.                 End With
    33.                 .LinkedStyle = ""
    34.             End With
    35.             ListGalleries(wdOutlineNumberGallery).ListTemplates(5).Name = ""
    36.             Selection.Range.ListFormat.ApplyListTemplate ListTemplate:=ListGalleries( _
    37.                 wdOutlineNumberGallery).ListTemplates(5), ContinuePreviousList:=True, _
    38.                 ApplyTo:=wdListApplyToWholeList, DefaultListBehavior:= _
    39.                 wdWord10ListBehavior


    Now, I changed "Selection" to "oHeader1" but have an error... Maybe I just have the wrong type, Word.Paragraph.

    Should be an easy one.

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Easy Yes/No Newbie Questions: Can I do this?

    Hmm, I've never seen ListGalleries before, oh well!!

    That means that you'll have to find out which of the following two items is correct, one of these should be used in place of both instances of ListGalleries:
    objDoc.ListGalleries
    objDoc.Application.ListGalleries

    And instead of Selection you should have:
    objDoc.Application.Selection


    If the Font for this section of the document is the same as that around it, you probably don't need the "With .Font ... End With" section.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Location
    Virginia, USA
    Posts
    17

    Re: Easy Yes/No Newbie Questions: Can I do this?

    ListGalleries is what the Word macro generated.

    Yes, it worked!

    Now, I'll try linking images... but only when I get back from vacation, so until next time!

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Location
    Virginia, USA
    Posts
    17

    Re: Easy Yes/No Newbie Questions: Can I do this?

    I'm back...

    And I was able to add images into my Word document that were listed in Excel. The "asking" Word idea has been a great help. It makes VBA easy for me to use... with some exceptions. I need the filename/path in Excel to be checked for validity (does the file really exist?) so the Macro doesn't "break".

    Working on that next...

    VB Code:
    1. If LEN_TEXT_IN_COL_G > 0 Then
    2.                 'Insert image for page
    3.                  Set oPara2 = objDoc.Content.Paragraphs.Add
    4.                  With oPara2
    5.                     'How do I verify that the filename is right?
    6.                     .Range.InlineShapes.AddPicture fileName:=STR_IMAGE_PATH, LinkToFile:=False, SaveWithDocument:=True
    7.                     .Range.InsertParagraphAfter
    8.                 End With
    9.             End If

    Thanks!

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Easy Yes/No Newbie Questions: Can I do this?

    Ok, to check that a filename is valid you need something that you can't get from Macro's.. you should be able to use the Dir function.

    You provide the Dir function with a full or partial file name (as well as the path) and it returns the name of a file that matches, if no matching files are found then you just get an empty string.

    Your code could be something like this (assuming that STR_IMAGE_PATH contains the path and filename):
    VB Code:
    1. If Dir(STR_IMAGE_PATH) = "" Then
    2.   'file not found
    3. Else
    4.   'file found - add it to the document
    5. End if

    In this case you can probably change the first line to this (if you only want to add the paragraph if the file exists):
    If (LEN_TEXT_IN_COL_G > 0) And (Dir(STR_IMAGE_PATH) <> "") Then

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Sep 2005
    Location
    Virginia, USA
    Posts
    17

    Re: Easy Yes/No Newbie Questions: Can I do this?

    Thanks.

Posting Permissions

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



Click Here to Expand Forum to Full Width