Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 病人“ '.Selection.PasteAndFormat WDFORMAT纯文本 wdDoc.Bookmarks(“MalePatients”).Range.Paste和Format WDFormat纯文本 Application.CutCopyMod_Excel_Vba_Loops_Pdf_Ms Word - Fatal编程技术网

Excel 病人“ '.Selection.PasteAndFormat WDFORMAT纯文本 wdDoc.Bookmarks(“MalePatients”).Range.Paste和Format WDFormat纯文本 Application.CutCopyMod

Excel 病人“ '.Selection.PasteAndFormat WDFORMAT纯文本 wdDoc.Bookmarks(“MalePatients”).Range.Paste和Format WDFormat纯文本 Application.CutCopyMod,excel,vba,loops,pdf,ms-word,Excel,Vba,Loops,Pdf,Ms Word,病人“ '.Selection.PasteAndFormat WDFORMAT纯文本 wdDoc.Bookmarks(“MalePatients”).Range.Paste和Format WDFormat纯文本 Application.CutCopyMode=False Chart2.ChartArea.Copy “.Selection.GoTo什么:=-1,名称:=“ChartLocation” '.Selection.Paste wdDoc.Bookmarks(“ChartLocation”

病人“ '.Selection.PasteAndFormat WDFORMAT纯文本 wdDoc.Bookmarks(“MalePatients”).Range.Paste和Format WDFormat纯文本 Application.CutCopyMode=False Chart2.ChartArea.Copy “.Selection.GoTo什么:=-1,名称:=“ChartLocation” '.Selection.Paste wdDoc.Bookmarks(“ChartLocation”).Range.Paste
If.Version第一步-您在循环中做了很多事情,这些事情应该在循环之外,例如
Set-WdApp=CreateObject(“Word.Application”)
。退出
,和
设置WdApp=Nothing
。是的,我觉得我每次都在打开和关闭word应用程序,而不仅仅是关闭一个文档。现在开始有意义了,我的大部分编码都是用SQL编写的,所以这对我来说是一种新的思维方式。感谢您的帮助。这已经完成了技巧,非常感谢。我为了进行一些更改,它似乎不喜欢。版本我也注意到它使用版本设置保存了一个.doc而不是.docx。是否应该切换?对不起,应该是WdApp.version刚刚运行了。我认为这已经解决了问题。我似乎偶尔会得到一些奇怪的结果。表格正在将文本发送到组织位置,而图表正在被复制复杂。有点像循环没有保持每次运行相同的步骤数。@dcfretwell-正如BigBen之前评论的,您需要将此作为另一个问题发布
Sub CreateBasicWordReport()
   Dim WdApp As Word.Application
   Dim wdDoc As Word.document
   Dim SaveName As String
   Dim FileExt As String
   Dim LstObj1 As ListObject
   Dim MaxValue As Integer
   Dim FilterValue As Integer
   Dim Organisation As String
   Dim Rng As Range
   Dim WS As Worksheet
   
   Set LstObj1 = Worksheets("Sheet1").ListObjects("Table1")
   
   MaxValue = WorksheetFunction.Max(LstObj1.ListColumns(1).Range)
    
   FilterValue = MaxValue
    
   Set WdApp = CreateObject("Word.Application")
   Do Until FilterValue = 0
    
      Sheets.Add(After:=Sheets("Sheet1")).Name = "Static"
      Sheets("Sheet1").Select
    
      'moved outside of loop
      ' Set WdApp = CreateObject("Word.Application")
   
      With WdApp
         .Visible = True
         .Activate
         'create new document and assign to object variable
         Set wdDoc = .Documents.Add("C:\Users\david\Documents\Custom Office Templates\IBD Registry Quarterly Report Template2.dotx")
      'now mostly finished with WdApp as from here wdDoc is used
      End With
      ActiveSheet.ListObjects("Table1").Range.AutoFilter Field:=1, Criteria1:=FilterValue
      Range("F11").Select
              
      Range("A1", Range("A1").End(xlDown).End(xlToRight)).Copy
    
      '         .Selection.GoTo what:=-1, Name:="TableLocation"
      '         .Selection.Paste
      wdDoc.Bookmarks("TableLocation").Range.Paste
    
      For Each Row In Range("Table1[#All]").Rows
         If Row.EntireRow.Hidden = False Then
            If Rng Is Nothing Then Set Rng = Row
            Set Rng = Union(Row, Rng)
         End If
      Next Row
      Set WS = Sheets("Static")
      Rng.Copy Destination:=WS.Range("A1")

      '      Sheets("Static").Select
      '      Sheets("Static").Activate
      Organisation = WS.Range("D2").Value
    
      '      Sheets("Static").Select
      '      Range("D2").Copy
      WS.Range("D2").Copy
      
      '         .Selection.GoTo what:=-1, Name:="Organisation"
      '         .Selection.PasteAndFormat wdFormatPlainText
      wdDoc.Bookmarks("Organisation").Range.PasteAndFormat wdFormatPlainText
      Application.CutCopyMode = False
    
      '      Sheets("Static").Select
      '      Range("F2").Copy
      WS.Range("F2").Copy
      
    
      '         .Selection.GoTo what:=-1, Name:="MalePatients"
      '         .Selection.PasteAndFormat wdFormatPlainText
      wdDoc.Bookmarks("MalePatients").Range.PasteAndFormat wdFormatPlainText
         
      Application.CutCopyMode = False
    
      Chart2.ChartArea.Copy
    
      '         .Selection.GoTo what:=-1, Name:="ChartLocation"
      '         .Selection.Paste
      wdDoc.Bookmarks("ChartLocation").Range.Paste
    
      If .Version <= 11 Then
         FileExt = ".doc"
      Else
         FileExt = ".docx"
      End If
    
      SaveName = Environ("UserProfile") & "\Desktop\IBD Registry Quarterly Report for " & _
         Organisation & " " & _
         Format(Now, "yyyy-mm-dd hh-mm-ss") & FileExt
        
      If .Version <= 12 Then
         ' .ActiveDocument.SaveAs SaveName
         wdDoc.SaveAs SaveName
      Else
         ' .ActiveDocument.SaveAs2 SaveName
         wdDoc.SaveAs2 SaveName
      End If
    
      SaveNamePDF = Environ("UserProfile") & "\Desktop\IBD Registry Quarterly Report for " & _
         Organisation & " " & _
         Format(Now, "yyyy-mm-dd hh-mm-ss") & ".pdf"

      wdDoc.ExportAsFixedFormat _
         OutputFileName:=SaveNamePDF, _
         ExportFormat:=wdExportFormatPDF _

    
         wdDoc.Close
         'moved outside of loop
    
         'are you sure that these need to be inside the loop?
         FilterValue = FilterValue - 1
         Sheets("Static").Delete
    
   Loop

   WdApp.Quit
    
   Set WdApp = Nothing
   Application.DisplayAlerts = False
   Application.DisplayAlerts = True
    
End Sub