Search code examples
vb.netvbaexcelenumeration

PasteSpecial(): wdPasteOLEObject and wdInLine Enumerations


I have written code in Excel VBA and am currently trying to convert it to vb.NET on VS 2017.

I want to copy a table in Excel, and paste it into a Word document and also keep a live link between them so that any changes in the Excel table will be transferred to the table pasted in the Word document. I managed this in Excel VBA, however vb.NET does not recognise DataType:=wdPasteOLEObject and also Placement:=wdInLine, saying they are not declared.

The following is a sample from my code:

excelApp = New Excel.Application
excelWB = excelApp.Workbooks.Open(SurveyFormLoc)
excelApp.Visible = True

With excelApp
    .Sheets("Site Details").Select 
    .Range("B2:I11").Copy()
End With

wdApp = CreateObject("Word.Application")
wdApp.Visible = True
wdDoc = wdApp.Documents.Open(DesignReportLoc)

With wdDoc
    .Application.Selection.Find.Text = "INSERT FROM SURVEY FORM"
    .Application.Selection.Find.Execute()
    .Application.Selection.ParagraphFormat.Alignment = 0               
End With

wdApp.Selection.PasteSpecial(Link:=True, DataType:=wdPasteOLEObject, Placement:=wdInLine, DisplayAsIcon:=False)

Does anybody know what the vb.NET equivalent is to do this?


Solution

  • Replace wdPasteOLEObject with the number 0.

    Replace wdInLine with the number 0 also.

    PasteDataType Enumeration

    Placement Enumeration

    You could also define the Enumeration yourself if you want to preserve the readability:

    Enum WdOLEPlacement 
      wdFloatOverText = 1
      wdInLine = 0 
    End Enum
    
    Enum WdPasteDataType 
      wdPasteBitmap = 4 
      wdPasteDeviceIndependentBitmap = 5    
      wdPasteEnhancedMetafile = 9   
      wdPasteHTML = 10  
      wdPasteHyperlink = 7  
      wdPasteMetafilePicture = 3    
      wdPasteOLEObject = 0  
      wdPasteRTF = 1    
      wdPasteShape = 8  
      wdPasteText = 2   
    End Enum
    

    And in your code, reference like this:

    DataType:=WdPasteDataType.wdPasteOLEObject 
    Placement:=WdOLEPlacement.wdInLine 
    

    If you run into undefined Enumerations again, you can just go to google and paste the thing you want to look up and include the word Enum and it will usually be the first result.