vbscript 添加Space for Equ

addSpaceForEqu
Sub addSpaceForEquation()
'word equation
Dim a As OMath
For Each a In ActiveDocument.OMaths
a.Range.Select
If Selection.Range.Previous <> ChrW(13) Then

    If Selection.Range.Previous <> " " Then
        Selection.Collapse wdCollapseStart
        Selection.TypeText " "
        
    End If
    a.Range.Select
    If Selection.Range.Next <> " " Then
        Selection.Collapse wdCollapseEnd
        Selection.TypeText " "
        
    End If
End If
Next
'mathtype
Dim b As Field
For Each b In ActiveDocument.Range.Fields
    If b.Type = 58 Then
        b.Select
        If Selection.Range.Previous <> ChrW(13) Then

    If Selection.Range.Previous <> " " Then
        Selection.Collapse wdCollapseStart
        Selection.TypeText " "
        
    End If
    b.Select
    If Selection.Range.Next <> " " Then
        Selection.Collapse wdCollapseEnd
        Selection.TypeText " "
        
    End If
End If
    End If
Next

End Sub

vbscript [向我显示工作表]单击导航工作表中的单元格,然后跳转到目标工作表并在导航工作表之后移动此工作表

[向我显示工作表]单击导航工作表中的单元格,然后跳转到目标工作表并在导航工作表之后移动此工作表

show_sheet.vb
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim tar_addr As String
    Dim tar_val As String
    On Error GoTo Oops
    
    tar_val = Target.Value
    If tar_val <> "" Then
        Worksheets(tar_val).Move _
            after:=Worksheets("Navigator")
        Sheets(tar_val).Activate
    Else
        Sheets("Navigator").Activate
    End If
Done:
    Exit Sub
Oops:
    Sheets("Navigator").Activate
End Sub

vbscript 检查文件是否存在的功能

.bas
Function Fileexists(fname) as boolean
   If Dir(fname) <> "" then _
   Fileexists = True _
   Else Fileexists = False
End Function

vbscript 使用文件对话框将工作簿导出为PDF

.bas
Sub PDFWorkbook()
    Dim strSheets() As String
    Dim strfile As String
    Dim sh As Worksheet
    Dim icount As Integer
    Dim myfile As Variant
    Dim startingPage As Integer
    
    ' Save Chart Sheet names to an Array
    For Each sh In ActiveWorkbook.Worksheets
    If sh.Visible = xlSheetVisible Then
        ReDim Preserve strSheets(icount)
        strSheets(icount) = sh.Name
        icount = icount + 1
    End If
    
    Next sh
    
    If icount = 0 Then
        MsgBox "A PDF cannot be created because no sheets were found.", , "No Sheets Found"
        Exit Sub
    End If
    
    ' Prompt for save location
    strfile = "Sheets" & "_" _
    & Format(Now(), "yyyymmdd_hhmmss") _
    & ".pdf"
    strfile = ThisWorkbook.path & "\" & strfile
    myfile = Application.GetSaveAsFilename _
    (InitialFileName:=strfile, _
    FileFilter:="PDF Files (*.pdf), *.pdf", _
    Title:="Select Folder and File Name to Save as PDF")
    
    If myfile <> "False" Then 'save as PDF
        
        ActiveWorkbook.ExportAsFixedFormat xlTypePDF, myfile & ".pdf", _
                                       xlQualityStandard, , , , , True
        
    Else
        MsgBox "No File Selected. PDF will not be saved", vbInformation, "No File Selected"
    End If
End Sub

vbscript 将整个工作簿导出为PDF

.bas
Sub PDFWorkbook()
    Dim fileName As String
    fileName = ActiveWorkbook.FullName
    If InStr(fileName, ".") > 0 Then fileName = Left(fileName, InStrRev(fileName, ".") - 1)
    ActiveWorkbook.ExportAsFixedFormat xlTypePDF, fileName & ".pdf", _
                                       xlQualityStandard, , , , , True
End Sub

vbscript 在Excel VBA中读取和写入文件

FileIO.bas
' Define global variables
Public userName As String

' Change the user as needed in this function
Function CurrentUser() As String
    Dim fileName As String: fileName = ActiveWorkbook.path & "\config.txt"
    Dim fcontent As String
    Dim fileStream As Integer: fileStream = FreeFile
    
    ' File I/O stream
    Open fileName For Input As #fileStream
    fcontent = Input(LOF(fileStream), fileStream)
    Close #fileStream
    
    ' userName is equal to contents of txt file
    userName = fcontent
    CurrentUser = userName
End Function

' Sets the username in config file
Sub SetCurrentUser()
    Dim myInput As Variant: myInput = InputBox("Enter the current username")
    Dim myFile As String: myFile = ActiveWorkbook.path & "\config.txt"
    Dim fileStream As Integer: fileStream = FreeFile
    
    ' File I/O stream
    Open myFile For Output As #fileStream
    Print #fileStream, myInput
    Close #fileStream
End Sub

vbscript 检查Excel工作簿中是否存在工作表

WorksheetExists.bas
Function WorksheetExists(sName As String) As Boolean
    WorksheetExists = Evaluate("ISREF('" & sName & "'!A1)")
End Function

vbscript 在VBA中运行Shell命令

ShellCommands.bas
Sub OpenCamera()
'
' OpenCamera Macro
'

'
Dim path As String
Dim cmd As String

'
' Camera.lnk is a desktop shortcut for the Windows Camera app.
' To create a shortcut for this app.
'
' 1. Open File Explorer.
' 2. Paste the following in the address bar and press enter:  %windir%\explorer.exe shell:::{4234d49b-0245-4df3-b780-3893943456e1}
' 3. Right click on the ‘Camera’ app and select ‘Create shortcut’.
path = "C:\Users\" & CurrentUser & "\Desktop\Camera.lnk"
cmd = "RunDLL32.EXE shell32.dll,ShellExec_RunDLL "
Shell (cmd & path)
End Sub

vbscript 将整个工作簿导出为PDF

PDFEverything.bas
Sub PDFEverything()
    Dim fileName As String
    fileName = ActiveWorkbook.FullName

    If InStr(fileName, ".") > 0 Then fileName = Left(fileName, InStrRev(fileName, ".") - 1)
    
    ActiveWorkbook.ExportAsFixedFormat xlTypePDF, fileName & ".pdf", _
                                       xlQualityStandard, , , 1, , True
End Sub

vbscript 将活动表导出为PDF

PDFActiveSheet.bas
Sub PDFActiveSheet()

    Dim wsA As Worksheet
    Dim wbA As Workbook
    Dim strTime As String
    Dim strName As String
    Dim strPath As String
    Dim strFile As String
    Dim strPathFile As String
    Dim myFile As Variant
    On Error GoTo errHandler
    
    Set wbA = ActiveWorkbook
    Set wsA = ActiveSheet
    strTime = Format(Now(), "yyyymmdd\_hhmm")
    
        'get active workbook folder, if saved
        strPath = wbA.path
        If strPath = "" Then
          strPath = Application.DefaultFilePath
        End If
        strPath = strPath & "\"
        
        'replace spaces and periods in sheet name
        strName = Replace(wsA.Name, " ", "")
        strName = Replace(strName, ".", "_")
        
        'create default name for savng file
        strFile = strName & "_" & strTime & ".pdf"
        strPathFile = strPath & strFile
        
        'use can enter name and
        ' select folder for file
        myFile = Application.GetSaveAsFilename _
            (InitialFileName:=strPathFile, _
                FileFilter:="PDF Files (*.pdf), *.pdf", _
                Title:="Select Folder and FileName to save")
        
        'export to PDF if a folder was selected
        If myFile <> "False" Then
            wsA.ExportAsFixedFormat _
                Type:=xlTypePDF, _
                fileName:=myFile, _
                Quality:=xlQualityStandard, _
                IncludeDocProperties:=True, _
                IgnorePrintAreas:=False, _
                OpenAfterPublish:=False
            'confirmation message with file info
            MsgBox "PDF file has been created: " _
              & vbCrLf _
              & myFile
        End If
        
exitHandler:
            Exit Sub
errHandler:
            MsgBox "Could not create PDF file"
            Resume exitHandler
End Sub