2016-11-26 65 views
1

我正嘗試使用VBA腳本運行3D CAD程序SolidWorks中的渲染列表。
我做了一個列表在MS Excel在列A從excel中提取時發生編譯錯誤

渲染部分工作的文件擴展名的含B列中的文件名,但現在我在得到一個錯誤:

Dim xlApp As Excel.Application 

的出現錯誤顯示消息:Compile error: User-defined type not defined.

下面你可以找到代碼:

Sub main() 
Dim swApp As Object 

Dim Part As Object 
Dim boolstatus As Boolean 
Dim longstatus As Long, longwarnings As Long 
Dim status As Boolean 
Set swApp = Application.SldWorks 
Dim i As String 
Dim j As String 
Dim y As Integer 
Dim n As Integer 
Dim m As Integer 
Dim swModel As SldWorks.ModelDoc2 
Dim swRayTraceRenderer As SldWorks.RayTraceRenderer 
Dim swRayTraceRenderOptions As SldWorks.RayTraceRendererOptions 
Dim errors As Long 
Dim warnings As Long 
Dim filePath As String 

'i = file name 
'j = file extention 

'i = "bp01p0006" example 
'j = "simbeton - Solidworks\bp - betonplaten\bp01 - simvlak\" example 

Dim xlApp As Excel.Application 
Dim xlWB As Excel.Workbook 
Set xlApp = New Excel.Application 

'Set xlWB = xlApp.Workbooks.Open(「C:\Users\Omar\Desktop\Renders Rob\Lijst.xlsx」) 
Set xlWB = xlApp.Workbooks.Open(「Lijst.xlsx」) 

y = 0 


n = 0 

Do While n < 5 
    If xlWB.Worksheets(1).Range("A1").offset(y, 0) = "" Then 
     y = y + 1 
     n = n + 1 
     Else 
      j = xlWB.Worksheets(1).Range("A1").offset(y, 0).Value 
      i = xlWB.Worksheets(1).Range("A1").offset(y, 1).Value 
      xlWB.Worksheets(1).Range("A1").offset(y, 0) = "" 
      y = y + 1 
    End If 



    Set xlWB = Nothing 
Set xlApp = Nothing 


filePath = "Z:\" & j & "" & i & ".SLDPRT" 
Set swModel = swApp.OpenDoc6(filePath, swDocPART, swOpenDocOptions_Silent, "", errors, warnings) 

Set swApp = _ 
Application.SldWorks 

Set Part = swApp.ActiveDoc 
Dim myModelView As Object 
Set myModelView = Part.ActiveView 
myModelView.AddPerspective 
Part.ViewZoomtofit2 
Part.ViewZoomtofit2 
Part.ViewZoomtofit2 
Part.ViewZoomtofit2 
Part.ViewZoomtofit2 
Part.ShowNamedView2 "*Isometric", 7 
Part.ViewZoomtofit2 

Part.ViewDisplayShaded 
Dim activeModelView As Object 
Set activeModelView = Part.ActiveView 
activeModelView.DisplayMode = swViewDisplayMode_e.swViewDisplayMode_ShadedWithEdges 
Part.ClearSelection2 True 
boolstatus = Part.Extension.SketchBoxSelect("0.000000", "0.000000", "0.000000", "0.000000", "0.000000", "0.000000") 
Part.ViewDisplayShaded 

' Access PhotoView 360 
Set swRayTraceRenderer = swApp.GetRayTraceRenderer(swPhotoView) 
' Get and set rendering options 
Set swRayTraceRenderOptions = swRayTraceRenderer.RayTraceRendererOptions 
'Get current rendering values 
Debug.Print "Current rendering values" 
Debug.Print " ImageHeight   = " & swRayTraceRenderOptions.ImageHeight 
Debug.Print " ImageWidth   = " & swRayTraceRenderOptions.ImageWidth 
Debug.Print " ImageFormat   = " & swRayTraceRenderOptions.ImageFormat 
Debug.Print " PreviewRenderQuality = " & swRayTraceRenderOptions.PreviewRenderQuality 
Debug.Print " FinalRenderQuality = " & swRayTraceRenderOptions.FinalRenderQuality 
Debug.Print " BloomEnabled   = " & swRayTraceRenderOptions.BloomEnabled 
Debug.Print " BloomThreshold  = " & swRayTraceRenderOptions.BloomThreshold 
Debug.Print " BloomRadius   = " & swRayTraceRenderOptions.BloomRadius 
Debug.Print " ContourEnabled  = " & swRayTraceRenderOptions.ContourEnabled 
Debug.Print " ShadedContour  = " & swRayTraceRenderOptions.ShadedContour 
Debug.Print " ContourLineThickness = " & swRayTraceRenderOptions.ContourLineThickness 
Debug.Print " ContourLineColor  = " & swRayTraceRenderOptions.ContourLineColor 
Debug.Print " " 
' Change rendering values 
Debug.Print "New rendering values" 
swRayTraceRenderOptions.ImageHeight = 720 
Debug.Print " ImageHeight   = " & swRayTraceRenderOptions.ImageHeight 
swRayTraceRenderOptions.ImageWidth = 405 
Debug.Print " ImageWidth   = " & swRayTraceRenderOptions.ImageWidth 
swRayTraceRenderOptions.ImageFormat = swImageFormat_PNG 
Debug.Print " ImageFormat   = " & swRayTraceRenderOptions.ImageFormat 
swRayTraceRenderOptions.PreviewRenderQuality = swRenderQuality_Better 
Debug.Print " PreviewRenderQuality = " & swRayTraceRenderOptions.PreviewRenderQuality 
swRayTraceRenderOptions.FinalRenderQuality = swRenderQuality_Best 
Debug.Print " FinalRenderQuality = " & swRayTraceRenderOptions.FinalRenderQuality 
swRayTraceRenderOptions.BloomEnabled = False 
Debug.Print " BloomEnabled   = " & swRayTraceRenderOptions.BloomEnabled 
swRayTraceRenderOptions.BloomThreshold = 0 
Debug.Print " BloomThreshold  = " & swRayTraceRenderOptions.BloomThreshold 
swRayTraceRenderOptions.BloomRadius = 0 
Debug.Print " BloomRadius   = " & swRayTraceRenderOptions.BloomRadius 
swRayTraceRenderOptions.ContourEnabled = False 
Debug.Print " ContourEnabled  = " & swRayTraceRenderOptions.ContourEnabled 
swRayTraceRenderOptions.ShadedContour = False 
Debug.Print " ShadedContour  = " & swRayTraceRenderOptions.ShadedContour 
swRayTraceRenderOptions.ContourLineThickness = 0 
Debug.Print " ContourLineThickness = " & swRayTraceRenderOptions.ContourLineThickness 
swRayTraceRenderOptions.ContourLineColor = 255 
Debug.Print " ContourLineColor  = " & swRayTraceRenderOptions.ContourLineColor 
' Display the preview window 
status = swRayTraceRenderer.DisplayPreviewWindow 
' Close render 
status = swRayTraceRenderer.CloseRayTraceRender 
' Invoke final render window 
status = swRayTraceRenderer.InvokeFinalRender 
' Abort final render window 
status = swRayTraceRenderer.AbortFinalRender 
' Render to Windows Bitmap format 
status = swRayTraceRenderer.RenderToFile("C:\Users\Omar\Desktop\Renders Rob\" & i & ".png", 0, 0) 
swRayTraceRenderOptions.FinalRenderQuality = swRenderQuality_Good 
' Render to HDR format (format extension omitted) 
status = swRayTraceRenderer.RenderToFile("C:\Users\Omar\Desktop\Renders Rob\" & i, 0, 0) 
Set swRayTraceRenderOptions = Nothing 
' Close render 
status = swRayTraceRenderer.CloseRayTraceRender 

swApp.QuitDoc i 

Loop 

End Sub 

爲什麼會這樣一個nd我該如何解決它?

+0

調試幫助問題需要[mcve]根據[help]。你的代碼不是最小的。請[編輯]你的問題,以確保你的代碼是最小的(只有在你的問題中重現你的問題所需的代碼),完整(用戶不需要其他任何東西來重現你的問題),和可驗證的(提供的代碼重現你正面臨的確切問題)。因爲這是你的問題是堆棧溢出的主題。請注意,這也是一個常見的[downvote reason](http://idownvotedyoubecause.com/so/TooMuchCode)。 –

回答

2

您正在使用「早期綁定」到Excel對象模型,那麼你必須在你的VBE編輯器中打開「引用」對話框(在Excel VBE這下工具 - >參考,沒準會丟失的Excel庫參考

SolidWorks VBA IDE相似),將列表框向下滾動到「Microsoft Excel XX.0 Library」(「XX」是Excel版本號)條目,選中其複選框並單擊確定。

或者你可能想使用 「後期綁定」:

Dim xlApp As Object, xlWB As Object 
Set xlApp = CreateObject("Excel.Application") 

其中:

  • 你沒有任何的Excel versioning問題遭受

  • ,但你失去Intellisense

    ,讓你必須知道Excel對象模型非常好,才能正常使用其對象和相應的方法或屬性

    但在你的情況下,你只能使用Workbooks對象Open()方法,Worksheet對象的Range特性,因此Range對象Offset()方法和Value屬性,以便你已經完成。

+0

不客氣。如果我的答案解決了您的問題,請將其標記爲已接受。謝謝! – user3598756

+0

感謝您的快速回復! 我嘗試了你的第一個溶劑(Excel livrary參考),它的工作! 但現在在哪裏我嘗試打開該部分發生第二個錯誤的Excel文件 它說的「需要選用對象」在這一行 我是否需要添加EXEL文件的extention?(因爲我在互聯網上找到的代碼沒有) 當我嘗試這種方式時,該行發出紅色並在「:」右後的雙點出現錯誤C 設置xlWB = xlApp.Workbooks.Open (「C:\用戶\奧馬爾\桌面\渲染羅布\ Lijst.xlsx」) 它說,它預期列表分隔符) 親切的問候,並感謝您的幫助迄今! – Cornelis

+0

謝謝!我肯定會,如果你能幫我完成最後一部分,那會很好嗎? – Cornelis

相關問題