How to fix error when executing macro: "Compile Error Variable not defined"?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • James Lucero

    How to fix error when executing macro: "Compile Error Variable not defined"?

    Below is my code. The error is "Compile Error Variable not defined". wdOrientLandsca pe is highlighted. Again this the portion of the code from a VB module created in Excel. The previous lines of code are passing cell contents from a worksheet and then into the variable that are shown in the respective lines for each occurance of .TypeText:=
    By the way the next i is a counter to ensure all the rows in the active worksheet are passed to to the variables and thereby printed.

    Code:
    ' Send commands to Word
         With WordApp
          .Documents.Add
          .PageSetup.Orientation = wdOrientLandscape
             With .Selection
              .Font.Size = 14
              .Font.Bold = True
              .ParagraphFormat.Alignment = 1
              .TypeText Text:="E C R"
              .TypeParagraph
              .TypeParagraph
              .Font.Size = 12
              .ParagraphFormat.Alignment = 0
              .Font.Bold = False
              .TypeText Text:="Date Created:" & vbTab & DateCreated & vbTab
              .TypeText Text:="Initiated By:" & vbTab & _
                       Application.UserName
              .TypeParagraph
              .TypeText Text:="Serial Number:" & vbTab & SerialNumber
    '               .TypeText message
              .TypeText Text:="Tool Name:" & vbTab & ToolName
              .TypeText Text:="Drawing Number:" & vbTab & DrawingNumber
              .TypeText Text:="Drawing Title:" & vbTab & DrawingTitle
              .TypeText Text:="Revision:" & vbTab & Rev
              .TypeText Text:="Change Request:" & vbTab & ChangeRequest
              .TypeText Text:="Change Reason:" & vbTab & ChangeReason
              .TypeText Text:="Additional Information:" & vbTab & AddlInfo
             End With
                .ActiveDocument.SaveAs Filename:=SaveAsName
         End With
           
        Next i
     
    '   Kill the object
        WordApp.Quit
        Set WordApp = Nothing
    Last edited by Atli; Nov 19 '10, 05:17 PM. Reason: Please use [code] tags when posting code.
  • Guido Geurs
    Recognized Expert Contributor
    • Oct 2009
    • 767

    #2
    For word 2003 it's:
    Code:
    With Word
    .WdOrientation = wdOrientLandscape

    Comment

    • James lucero
      New Member
      • Nov 2010
      • 2

      #3
      Thank you VERY much for response, however I failed to specify that I am working with Excel 2007 and Word 2007. I mention this because I actually already tried the fix you had suggested.

      Comment

      • Stewart Ross
        Recognized Expert Moderator Specialist
        • Feb 2008
        • 2545

        #4
        The constant wdOrientLandsca pe remains the same in the Office 2007 version. It has the value 1 (the corresponding portrait version, wdOrientPortrai t, has the value 0).

        Constants like wdOrientLandsca pe will only be available to the compiler if you have explicitly declared a reference to the Word object library (using Tools, References from the VB editor). If you are using late binding where you don't need to reference the object library the constants will not be available.

        The equivalent statement without use of the symbolic constant is

        Code:
        .pagesetup.orientation = 1
        As an aside, I note that you have a With .Selection in your code. I am assuming that you actually have a Select statement somewhere before the segment you show, as otherwise the With .Selection is not going to work.

        -Stewart
        Last edited by Stewart Ross; Dec 2 '10, 09:44 PM.

        Comment

        • James lucero
          New Member
          • Nov 2010
          • 2

          #5
          Stewart,

          Thank you very much! Your code worked like a charm! I'm not too sure why the With .Selection is working because I do not have a Select statement anywhere in my code.
          P.S. I am using late binding. I am going to have study the late binding concept because I am not familiar with it. I borrowed the code from an example that a friend had and modified it considerably.

          Thanks a Million!

          Comment

          Working...